{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "### Simple code to verify that my neural_nets directory is okay\n",
    "\n",
    "import torch \n",
    "import numpy as np \n",
    "import sys \n",
    "sys.path.append('..')\n",
    "import neural_nets.train as train \n",
    "import neural_nets.data_loaders as dl\n",
    "from relu_nets import ReLUNet\n",
    "import utilities as utils\n",
    "import matplotlib.pyplot as plt\n",
    "import torch.nn as nn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "test_network = ReLUNet(layer_sizes=[784, 10, 20, 40, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "mnist_trainset = dl.load_mnist_data('train', digits=[1, 7])\n",
    "mnist_valset = dl.load_mnist_data('val', digits=[1,7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train.training_loop(test_network, mnist_trainset, mnist_valset, 10, loss_functional=lfxn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lfxn = train.LossFunctional(test_network)\n",
    "lfxn.attach_regularizer(train.XEntropyReg(test_network, scalar=2.0))\n",
    "lfxn.attach_regularizer(train.LipschitzReg(test_network, scalar=0.5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lfxn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train.test_validation(test_network, mnist_valset, lfxn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train.test_validation(test_network, mnist_valset, lfxn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs_reg = train.ReluStability(test_network, l_inf_radius=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TVLipschitzReg: (scalar: 5.00e-01), (lp: 1)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lip_reg = train.LipschitzReg(test_network, scalar=0.5)\n",
    "lip_reg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "mnist_valset = dl.load_mnist_data('val', digits=[1,7], batch_size=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(torch.Size([8, 1, 28, 28]), torch.Size([8]))"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ex, lab = next(iter(mnist_valset))\n",
    "ex.shape, lab.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "loss = lip_reg.forward(ex, lab)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(0.0423, grad_fn=<MulBackward0>)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "loss.backward()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.0000e+00,  0.0000e+00,  0.0000e+00,  ...,  0.0000e+00,\n",
       "          0.0000e+00,  0.0000e+00],\n",
       "        [-2.9134e-04,  2.9134e-04, -2.9134e-04,  ..., -2.9134e-04,\n",
       "         -2.9134e-04, -2.9134e-04],\n",
       "        [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  ...,  0.0000e+00,\n",
       "          0.0000e+00,  0.0000e+00],\n",
       "        ...,\n",
       "        [-3.8794e-05,  3.8794e-05,  2.7043e-04,  ...,  2.7043e-04,\n",
       "          2.7043e-04,  2.7043e-04],\n",
       "        [ 9.6554e-05, -1.0485e-04,  9.6147e-05,  ...,  9.6147e-05,\n",
       "          9.6147e-05,  9.6147e-05],\n",
       "        [-7.4802e-04,  6.9537e-04,  1.1523e-03,  ...,  1.1523e-03,\n",
       "          1.1523e-03,  1.1523e-03]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_network.fcs[0].weight.grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "els = rs_reg._naive_ia(ex)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sum([-torch.tanh(1.0 + el[:,:,0]*el[:,:,1]).sum() for el in els])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class TestFxn(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(TestFxn, self).__init__()\n",
    "        self.y = nn.Parameter(torch.rand(4))\n",
    "        print(self.y)\n",
    "    def forward(self, x):\n",
    "        return (self.y * self.y).dot(x)\n",
    "\n",
    "    \n",
    "f = TestFxn()\n",
    "x = torch.ones(4)\n",
    "x.requires_grad_(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def grad_norm(x, lam):\n",
    "    gradx = torch.autograd.grad(f(x), x, create_graph=True)\n",
    "    return lam * gradx[0].norm(p=1)\n",
    "\n",
    "def loss_fxn(x):\n",
    "    return f(x) * f(x) + grad_norm(x, 0.5)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "loss_fxn(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "loss.backward()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f.y.grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "torch.mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "z = torch.rand(4)\n",
    "z * z, z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grad_params = torch.autograd.grad(loss, model.parameters(), create_graph=True)\n",
    "grad_norm = 0\n",
    "for grad in grad_params:\n",
    "grad_norm += grad.pow(2).sum()\n",
    "grad_norm = grad_norm.sqrt()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
