Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 5.49 KB | None | 0 0
  1. {
  2.  "cells": [
  3.   {
  4.    "cell_type": "code",
  5.    "execution_count": 1,
  6.    "metadata": {
  7.     "collapsed": true
  8.    },
  9.    "outputs": [],
  10.    "source": [
  11.     "import math\n",
  12.     "\n",
  13.     "def sig(x):\n",
  14.     "    return 1 / (1 + math.exp(-x))\n",
  15.     "\n",
  16.     "def mse(ideal, actual):\n",
  17.     "    if len(ideal) == len(actual):\n",
  18.     "        result = 0\n",
  19.     "        for i in range(len(ideal)):\n",
  20.     "            result += (ideal[i] - actual[i])**2\n",
  21.     "        return result / len(ideal)\n",
  22.     "    else:\n",
  23.     "        raise ValueError"
  24.    ]
  25.   },
  26.   {
  27.    "cell_type": "code",
  28.    "execution_count": 86,
  29.    "metadata": {
  30.     "collapsed": true
  31.    },
  32.    "outputs": [],
  33.    "source": [
  34.     "class Neuron:\n",
  35.     "    def __init__(self, inp_w=None, out_w=None, name=''):\n",
  36.     "        self.name = name\n",
  37.     "        self.inp_w = inp_w\n",
  38.     "        self.out_w = out_w\n",
  39.     "    \n",
  40.     "    def grad(self, n):\n",
  41.     "        return self.out * n.delta\n",
  42.     "    \n",
  43.     "    def activate(self):\n",
  44.     "        return sig(self.inp)\n",
  45.     "    \n",
  46.     "    def add_inp_w(self, neuron, w):\n",
  47.     "        self.inp_w.append([neuron, w])\n",
  48.     "        neuron.add_out_w(self, w)\n",
  49.     "        \n",
  50.     "    def add_out_w(self, neuron, w):\n",
  51.     "        self.out_w.append([neuron, w])\n",
  52.     "        \n",
  53.     "    def show_w(self):\n",
  54.     "        print('Inputs: ')\n",
  55.     "        for iw in self.inp_w:\n",
  56.     "            print(iw[0].name, end=\" \")\n",
  57.     "        \n",
  58.     "        print('Outputs: ')\n",
  59.     "        for ow in self.out_w:\n",
  60.     "            print(ow[0].name, end=\" \")\n",
  61.     "\n",
  62.     "\n",
  63.     "class I(Neuron):\n",
  64.     "    def __init__(self, inp=0, inp_w=None, out_w=None, name=''):\n",
  65.     "        super().__init__(inp_w or [], out_w or [], name)\n",
  66.     "        self.inp = inp\n",
  67.     "        self.out = inp\n",
  68.     "\n",
  69.     "\n",
  70.     "class H(Neuron):\n",
  71.     "    def __init__(self, inp_w=None, out_w=None, name=''):\n",
  72.     "        super().__init__(inp_w or [], out_w or [], name)\n",
  73.     "    \n",
  74.     "    @property\n",
  75.     "    def inp(self):\n",
  76.     "        result = 0\n",
  77.     "        for w in self.inp_w:\n",
  78.     "            result += w[0].out * w[1]\n",
  79.     "        return result\n",
  80.     "    \n",
  81.     "    @property\n",
  82.     "    def out(self):\n",
  83.     "        return self.activate()\n",
  84.     "    \n",
  85.     "    @property\n",
  86.     "    def delta(self):\n",
  87.     "        sigmoid_derivative = (1 - self.out) * self.out\n",
  88.     "        sigma = 0\n",
  89.     "        \n",
  90.     "        for w in self.out_w:\n",
  91.     "            print(w[0].name)\n",
  92.     "            sigma += w[0].delta * w[1]\n",
  93.     "            \n",
  94.     "        return sigma * sigmoid_derivative\n",
  95.     "\n",
  96.     "\n",
  97.     "class O(Neuron):\n",
  98.     "    def __init__(self, inp_w=None, out_w=None, delta=0, name=''):\n",
  99.     "        super().__init__(inp_w or [], out_w or [], name)\n",
  100.     "        self.delta = delta\n",
  101.     "    \n",
  102.     "    @property\n",
  103.     "    def inp(self):\n",
  104.     "        result = 0\n",
  105.     "        for w in self.inp_w:\n",
  106.     "            result += w[0].out * w[1]\n",
  107.     "        return result\n",
  108.     "    \n",
  109.     "    @property\n",
  110.     "    def out(self):\n",
  111.     "        return self.activate()\n",
  112.     "\n",
  113.     "    def calc_delta(self, ideal):\n",
  114.     "        sigmoid_derivative = (1 - self.out) * self.out\n",
  115.     "        self.delta = (ideal - self.out) * sigmoid_derivative\n",
  116.     "        return self.delta"
  117.    ]
  118.   },
  119.   {
  120.    "cell_type": "code",
  121.    "execution_count": 87,
  122.    "metadata": {},
  123.    "outputs": [],
  124.    "source": [
  125.     "I1 = I(inp=1, name='I1')\n",
  126.     "I2 = I(inp=0, name='I2')\n",
  127.     "\n",
  128.     "w1=0.45\n",
  129.     "w2=0.78\n",
  130.     "w3=-0.12\n",
  131.     "w4=0.13\n",
  132.     "w5=1.5\n",
  133.     "w6=-2.3\n",
  134.     "\n",
  135.     "H1 = H(name='H1')\n",
  136.     "H2 = H(name='H2')\n",
  137.     "\n",
  138.     "O1 = O(name='O1')"
  139.    ]
  140.   },
  141.   {
  142.    "cell_type": "code",
  143.    "execution_count": 88,
  144.    "metadata": {
  145.     "collapsed": true
  146.    },
  147.    "outputs": [],
  148.    "source": [
  149.     "H1.add_inp_w(I1, w1)\n",
  150.     "H1.add_inp_w(I2, w3)\n",
  151.     "\n",
  152.     "H2.add_inp_w(I1, w2)\n",
  153.     "H2.add_inp_w(I2, w4)\n",
  154.     "\n",
  155.     "O1.add_inp_w(H1, w5)\n",
  156.     "O1.add_inp_w(H2, w6)"
  157.    ]
  158.   },
  159.   {
  160.    "cell_type": "code",
  161.    "execution_count": 89,
  162.    "metadata": {},
  163.    "outputs": [
  164.     {
  165.      "name": "stdout",
  166.      "output_type": "stream",
  167.      "text": [
  168.       "O1\n"
  169.      ]
  170.     },
  171.     {
  172.      "data": {
  173.       "text/plain": [
  174.        "0.052817182118740695"
  175.       ]
  176.      },
  177.      "execution_count": 89,
  178.      "metadata": {},
  179.      "output_type": "execute_result"
  180.     }
  181.    ],
  182.    "source": [
  183.     "O1.calc_delta(1)\n",
  184.     "H1.delta"
  185.    ]
  186.   },
  187.   {
  188.    "cell_type": "code",
  189.    "execution_count": null,
  190.    "metadata": {
  191.     "collapsed": true
  192.    },
  193.    "outputs": [],
  194.    "source": []
  195.   },
  196.   {
  197.    "cell_type": "code",
  198.    "execution_count": null,
  199.    "metadata": {
  200.     "collapsed": true
  201.    },
  202.    "outputs": [],
  203.    "source": []
  204.   }
  205.  ],
  206.  "metadata": {
  207.   "kernelspec": {
  208.    "display_name": "Python 3",
  209.    "language": "python",
  210.    "name": "python3"
  211.   },
  212.   "language_info": {
  213.    "codemirror_mode": {
  214.     "name": "ipython",
  215.     "version": 3
  216.    },
  217.    "file_extension": ".py",
  218.    "mimetype": "text/x-python",
  219.    "name": "python",
  220.    "nbconvert_exporter": "python",
  221.    "pygments_lexer": "ipython3",
  222.    "version": "3.6.1"
  223.   }
  224.  },
  225.  "nbformat": 4,
  226.  "nbformat_minor": 2
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement