{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "import random\n", "\n", "import numpy as np\n", "from src.functional import sigmoid\n", "\n", "from jax import grad\n" ] }, { "cell_type": "code", "execution_count": 106, "outputs": [], "source": [ "class DenseLayer:\n", " def __init__(self, total_nodes, input_size, activation=sigmoid):\n", " self.total_nodes = total_nodes\n", " self.input_size = input_size\n", " self.weights = np.random.rand(total_nodes, self.input_size + 1)\n", " self.activation = activation\n", "\n", " def forward(self, x):\n", " x_biased = np.concatenate((x, np.ones((len(x), 1))), axis=1)\n", " y = self.weights @ x_biased.T\n", " return self.activation(y)\n", "\n", " def backprop(self, gradient):\n", " " ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 107, "outputs": [], "source": [ "l = DenseLayer(5, 5)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 108, "outputs": [ { "data": { "text/plain": "Array([[0.7291562 ],\n [0.84321564],\n [0.8657799 ],\n [0.8525716 ],\n [0.89164424]], dtype=float32)" }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.forward(np.random.rand(1, 5))" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 2, "outputs": [], "source": [ "import torch\n", "from torch import nn" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 4, "outputs": [], "source": [ "class CNN(nn.Module):\n", " def __init__(self, input_channels, num_classes):\n", " super().__init__()\n", "\n", " self.feature_layers = [input_channels, 6, 16, 120]\n", " self.kernels = [5, 5, 5]\n", " self.pools = [2, 2]\n", " self.feature_activations = [nn.Tanh for _ in range(len(self.channels) - 1)]\n", "\n", " self.classifier_layers = [120, num_classes]\n", " self.classifier_activations = [nn.Tanh for _ in range(len(self.classifier_layers))]\n", "\n", " feature_layers = []\n", " for idx, layer in enumerate(list(zip(self.feature_layers[:-1], self.feature_layers[1:]))):\n", " feature_layers.append(\n", " nn.Conv2d(in_channels=layer[0], out_channels=layer[1], kernel_size=self.kernels[idx])\n", " )\n", " feature_layers.append(self.feature_activations[idx])\n", "\n", " if idx != len(self.feature_activations):\n", " feature_layers.append(nn.MaxPool2d(kernel_size=self.pools[2]))\n", "\n", "\n", " classifier_layers = []\n", " for idx, layer in enumerate(list(zip(self.classifier_layers[:-1], self.classifier_layers[1:]))):\n", " classifier_layers.append(\n", " nn.Linear(in_features=layer[0], out_features=layer[1])\n", " )\n", "\n", " if idx != len(self.classifier_activations):\n", " classifier_layers.append(self.classifier_activations[idx])\n", "\n", "\n", " self.feature_extractor = nn.Sequential(*feature_layers)\n", " self.classifier = nn.Sequential(*classifier_layers)\n", "\n", " def forward(self, x):\n", " x = self.feature_extractor(x)\n", " y = self.classifier_layers(torch.flatten(x, 1))\n", " p = nn.functional.softmax(y, dim=1)\n", " return y, p" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }