{
 "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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "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": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'lfxn' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-4-26a18dab1b7a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtrain\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtraining_loop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtest_network\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmnist_trainset\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmnist_valset\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mloss_functional\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlfxn\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'lfxn' is not defined"
     ]
    }
   ],
   "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.L1WeightReg(test_network, scalar=1e-4))"
   ]
  },
  {
   "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": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs_reg = train.ReluStability(test_network, l_inf_radius=0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "mnist_valset = dl.load_mnist_data('val', digits=[1,7], batch_size=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(torch.Size([8, 1, 28, 28]), torch.Size([8]))"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ex, lab = next(iter(mnist_valset))\n",
    "ex.shape, lab.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LAYER0SHAPE torch.Size([8, 784, 2])\n",
      "layer 00 torch.Size([8, 20]) torch.Size([20, 784]) torch.Size([8, 784])\n",
      "layer 01 torch.Size([8, 20]) torch.Size([20, 20]) torch.Size([8, 20])\n",
      "layer 02 torch.Size([8, 20]) torch.Size([20, 20]) torch.Size([8, 20])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[torch.Size([8, 20, 2]), torch.Size([8, 20, 2]), torch.Size([8, 20, 2])]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[_.shape for _ in rs_reg._naive_ia(ex)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
