|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "%load_ext autoreload\n", |
| 10 | + "%autoreload 2\n", |
| 11 | + "\n", |
| 12 | + "import numpy as np\n", |
| 13 | + "import matplotlib.pyplot as plt\n", |
| 14 | + "\n", |
| 15 | + "from pim.models.network2 import RecurrentNetwork, Layer, InputLayer, Connection, WeightedConnection\n", |
| 16 | + "from pim.models.new.stone import tb1_output, tn1_output, tn2_output, cpu4_output, cpu1_output, motor_output, CentralComplex" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": null, |
| 22 | + "metadata": {}, |
| 23 | + "outputs": [], |
| 24 | + "source": [ |
| 25 | + "def linear_neuron(gain, gain_noise, noise):\n", |
| 26 | + " gain = np.random.normal(gain, gain_noise)\n", |
| 27 | + " def f(x):\n", |
| 28 | + " return gain * x + np.random.normal(0, noise)\n", |
| 29 | + " return f\n", |
| 30 | + "\n", |
| 31 | + "def bistable_neuron(Idown, Iup, gain, gain_noise, noise):\n", |
| 32 | + " gain = np.random.normal(gain, gain_noise)\n", |
| 33 | + " state = 0\n", |
| 34 | + " def f(x):\n", |
| 35 | + " nonlocal state\n", |
| 36 | + " if x >= Iup:\n", |
| 37 | + " state = 1\n", |
| 38 | + " elif x <= Idown:\n", |
| 39 | + " state = 0 \n", |
| 40 | + " return state * gain * x + np.random.normal(0, noise)\n", |
| 41 | + " return f" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "code", |
| 46 | + "execution_count": null, |
| 47 | + "metadata": {}, |
| 48 | + "outputs": [], |
| 49 | + "source": [ |
| 50 | + "class BistableLayer(Layer):\n", |
| 51 | + " def __init__(self, name, N, dI, mI, gain, gain_noise, noise):\n", |
| 52 | + " super().__init__(name)\n", |
| 53 | + " self.N = N\n", |
| 54 | + " self.mI = mI\n", |
| 55 | + " self.gain = np.random.normal(gain, gain_noise)\n", |
| 56 | + " self.noise = noise\n", |
| 57 | + " self.neurons = [bistable_neuron(I_up-dI, I_up, 1.0, 0.0, noise) for I_up in np.linspace((mI-dI)/N, mI, N)]\n", |
| 58 | + " \n", |
| 59 | + " def update(self, x):\n", |
| 60 | + " activity = np.array([neuron(x) for neuron in self.neurons])\n", |
| 61 | + " num_active = np.sum(activity > 0.5)\n", |
| 62 | + " return num_active / self.N * self.mI * self.gain + np.random.normal(0.0, self.noise)\n", |
| 63 | + "\n", |
| 64 | + "class LinearLayer(Layer):\n", |
| 65 | + " def __init__(self, name, gain, gain_noise, noise):\n", |
| 66 | + " super().__init__(name)\n", |
| 67 | + " self.neuron = linear_neuron(gain, gain_noise, noise)\n", |
| 68 | + " \n", |
| 69 | + " def update(self, x):\n", |
| 70 | + " return self.neuron(x)\n", |
| 71 | + " \n", |
| 72 | + "def step(network, input_layer, memory, i):\n", |
| 73 | + " input_layer.set(i)\n", |
| 74 | + " network.step()\n", |
| 75 | + " return memory.output\n" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": null, |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [], |
| 83 | + "source": [ |
| 84 | + "T = np.linspace(0, 20, 100)\n", |
| 85 | + "I = np.zeros(T.size)\n", |
| 86 | + "I[(0.2 < T) & (T < 0.3)] = 1.0\n", |
| 87 | + "I[(2.5 < T) & (T < 3.5)] = 1.0\n", |
| 88 | + "I[(7.5 < T) & (T < 8)] = -1.0\n", |
| 89 | + "I[(12 < T) & (T < 15)] = (T[(12 < T) & (T < 15)]-12)*0.05\n", |
| 90 | + "\n", |
| 91 | + "plt.figure()\n", |
| 92 | + "plt.plot(T, I)\n", |
| 93 | + "plt.plot(T, np.cumsum(I))\n", |
| 94 | + "plt.show()" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": null, |
| 100 | + "metadata": {}, |
| 101 | + "outputs": [], |
| 102 | + "source": [ |
| 103 | + "network = RecurrentNetwork()\n", |
| 104 | + "\n", |
| 105 | + "input_layer = network.add_layer(InputLayer(\"in\"))\n", |
| 106 | + "memory = network.add_layer(BistableLayer(\"mem\", 40, 0.1, 10, 1.00, 0.00, 0.05))\n", |
| 107 | + "#memory = network.add_layer(LinearLayer(\"mem\", 1.0, 0.00, 0.05))\n", |
| 108 | + "\n", |
| 109 | + "network.add_connection(WeightedConnection(\"in\", \"mem\", 1.00))\n", |
| 110 | + "network.add_connection(WeightedConnection(\"mem\", \"mem\", 1.03))\n", |
| 111 | + "\n", |
| 112 | + "output = np.array([step(network, input_layer, memory, i) for i in I])\n", |
| 113 | + "\n", |
| 114 | + "plt.figure()\n", |
| 115 | + "plt.plot(T, I, label=\"input\")\n", |
| 116 | + "plt.plot(T, np.cumsum(I), label=\"true integral\")\n", |
| 117 | + "plt.plot(T, output, label=\"memory\")\n", |
| 118 | + "plt.legend()" |
| 119 | + ] |
| 120 | + } |
| 121 | + ], |
| 122 | + "metadata": { |
| 123 | + "kernelspec": { |
| 124 | + "display_name": "pim", |
| 125 | + "language": "python", |
| 126 | + "name": "pim" |
| 127 | + }, |
| 128 | + "language_info": { |
| 129 | + "codemirror_mode": { |
| 130 | + "name": "ipython", |
| 131 | + "version": 3 |
| 132 | + }, |
| 133 | + "file_extension": ".py", |
| 134 | + "mimetype": "text/x-python", |
| 135 | + "name": "python", |
| 136 | + "nbconvert_exporter": "python", |
| 137 | + "pygments_lexer": "ipython3", |
| 138 | + "version": "3.10.4" |
| 139 | + } |
| 140 | + }, |
| 141 | + "nbformat": 4, |
| 142 | + "nbformat_minor": 4 |
| 143 | +} |
0 commit comments