Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "import math\n",
- "\n",
- "def sig(x):\n",
- " return 1 / (1 + math.exp(-x))\n",
- "\n",
- "def mse(ideal, actual):\n",
- " if len(ideal) == len(actual):\n",
- " result = 0\n",
- " for i in range(len(ideal)):\n",
- " result += (ideal[i] - actual[i])**2\n",
- " return result / len(ideal)\n",
- " else:\n",
- " raise ValueError"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 86,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "class Neuron:\n",
- " def __init__(self, inp_w=None, out_w=None, name=''):\n",
- " self.name = name\n",
- " self.inp_w = inp_w\n",
- " self.out_w = out_w\n",
- " \n",
- " def grad(self, n):\n",
- " return self.out * n.delta\n",
- " \n",
- " def activate(self):\n",
- " return sig(self.inp)\n",
- " \n",
- " def add_inp_w(self, neuron, w):\n",
- " self.inp_w.append([neuron, w])\n",
- " neuron.add_out_w(self, w)\n",
- " \n",
- " def add_out_w(self, neuron, w):\n",
- " self.out_w.append([neuron, w])\n",
- " \n",
- " def show_w(self):\n",
- " print('Inputs: ')\n",
- " for iw in self.inp_w:\n",
- " print(iw[0].name, end=\" \")\n",
- " \n",
- " print('Outputs: ')\n",
- " for ow in self.out_w:\n",
- " print(ow[0].name, end=\" \")\n",
- "\n",
- "\n",
- "class I(Neuron):\n",
- " def __init__(self, inp=0, inp_w=None, out_w=None, name=''):\n",
- " super().__init__(inp_w or [], out_w or [], name)\n",
- " self.inp = inp\n",
- " self.out = inp\n",
- "\n",
- "\n",
- "class H(Neuron):\n",
- " def __init__(self, inp_w=None, out_w=None, name=''):\n",
- " super().__init__(inp_w or [], out_w or [], name)\n",
- " \n",
- " @property\n",
- " def inp(self):\n",
- " result = 0\n",
- " for w in self.inp_w:\n",
- " result += w[0].out * w[1]\n",
- " return result\n",
- " \n",
- " @property\n",
- " def out(self):\n",
- " return self.activate()\n",
- " \n",
- " @property\n",
- " def delta(self):\n",
- " sigmoid_derivative = (1 - self.out) * self.out\n",
- " sigma = 0\n",
- " \n",
- " for w in self.out_w:\n",
- " print(w[0].name)\n",
- " sigma += w[0].delta * w[1]\n",
- " \n",
- " return sigma * sigmoid_derivative\n",
- "\n",
- "\n",
- "class O(Neuron):\n",
- " def __init__(self, inp_w=None, out_w=None, delta=0, name=''):\n",
- " super().__init__(inp_w or [], out_w or [], name)\n",
- " self.delta = delta\n",
- " \n",
- " @property\n",
- " def inp(self):\n",
- " result = 0\n",
- " for w in self.inp_w:\n",
- " result += w[0].out * w[1]\n",
- " return result\n",
- " \n",
- " @property\n",
- " def out(self):\n",
- " return self.activate()\n",
- "\n",
- " def calc_delta(self, ideal):\n",
- " sigmoid_derivative = (1 - self.out) * self.out\n",
- " self.delta = (ideal - self.out) * sigmoid_derivative\n",
- " return self.delta"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 87,
- "metadata": {},
- "outputs": [],
- "source": [
- "I1 = I(inp=1, name='I1')\n",
- "I2 = I(inp=0, name='I2')\n",
- "\n",
- "w1=0.45\n",
- "w2=0.78\n",
- "w3=-0.12\n",
- "w4=0.13\n",
- "w5=1.5\n",
- "w6=-2.3\n",
- "\n",
- "H1 = H(name='H1')\n",
- "H2 = H(name='H2')\n",
- "\n",
- "O1 = O(name='O1')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 88,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "H1.add_inp_w(I1, w1)\n",
- "H1.add_inp_w(I2, w3)\n",
- "\n",
- "H2.add_inp_w(I1, w2)\n",
- "H2.add_inp_w(I2, w4)\n",
- "\n",
- "O1.add_inp_w(H1, w5)\n",
- "O1.add_inp_w(H2, w6)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 89,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "O1\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "0.052817182118740695"
- ]
- },
- "execution_count": 89,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "O1.calc_delta(1)\n",
- "H1.delta"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "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.1"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement