def createNetwork(learnConst, momentum): ne.Neuron.talk = networkTalk la.Layer.talk = networkTalk say("Creating Layers") say("----------------------------------------------------------") layers.append(la.Layer("Int Layer", learnConst, momentum)) layers.append(la.Layer("Hid Layer1", learnConst, momentum)) # layers.append(la.Layer("Hid Layer2", learnConst, momentum)) layers.append(la.Layer("Out Layer", learnConst, momentum)) layers[2].isOutput() say("----------------------------------------------------------") say("Done creating layers! \n") say("Adding nodes to layers") say("----------------------------------------------------------") layers[0].addNeuron(ne.Neuron(linear, "Inp neuron", False)) for i in range(20): layers[1].addNeuron(ne.Neuron(nonLinear, "Hid neuron" + str(i), False)) layers[2].addNeuron(ne.Neuron(linear, "Out neuron", False)) say("----------------------------------------------------------") say("Done adding! \n") say("Adding connections between layers") say("----------------------------------------------------------") addConnection(layers[0], layers[1]) addConnection(layers[1], layers[2]) # addConnection(layers[2], layers[3]) say("----------------------------------------------------------") say("Done building! \n") return (layers[0], layers[2])
def __init__(self): self.__m_layer1 = ( neuron.Neuron([1, 2, 3], 5), neuron.Neuron([1, 3, 4], 5), neuron.Neuron([1, 2, 3], 5), ) self.__m_layer2 = [neuron.Neuron([1, 2, 3], 3)]
def grow_neurons(self): #kappa = [] for n in self.neuron_group: #re = [abs(s.weight) for s in neuron.axon_synapses] #kappa = kappa + [sum(re)] for syn in n.axon_synapses: if syn.weight > self.weight_grow_threshold: syn.weight = syn.weight / 2.0 new_neuron = neuron.Neuron() new_neuron.connect(syn.postsynaptic_neuron, random.random() - 0.1) n.connect(new_neuron, random.random() - 0.1) self.neuron_group = self.neuron_group + [new_neuron] elif syn.weight < -self.weight_grow_threshold: syn.weight = syn.weight / 2.0 new_neuron = neuron.Neuron() new_neuron.connect(syn.postsynaptic_neuron, -random.random() + 0.1) n.connect(new_neuron, -random.random() + 0.1) self.neuron_group = self.neuron_group + [new_neuron] elif random.random() < self.r_neuron_grow: syn.weight = syn.weight / 2.0 new_neuron = neuron.Neuron() new_neuron.connect(syn.postsynaptic_neuron, random.random() - 0.5) n.connect(new_neuron, random.random() - 0.5) self.neuron_group = self.neuron_group + [new_neuron]
def __init__(self): weights = np.array([0, 1]) bias = 0 self.h1 = n.Neuron(weights, bias) self.h2 = n.Neuron(weights, bias) self.o1 = n.Neuron(weights, bias)
def __init__(self): weights = np.array([0, 1]) bias = 0 # The Neuron class here is from the previous section self.h1 = N.Neuron(weights, bias) self.h2 = N.Neuron(weights, bias) self.o1 = N.Neuron(weights, bias)
def __init__(self): #initialize all layers self.sensorLayer = [neuron.Neuron(0) for i in range(10)] self.hiddenLayer = [ neuron.Neuron(len(self.sensorLayer)) for i in range(10) ] self.actionLayer = [ neuron.Neuron(len(self.hiddenLayer)) for i in range(6) ]
def __init__(self, numOfNeurons, activation, input_num, lr, weights=None): if weights is None: self.neurons = [ neuron.Neuron(activation, input_num, lr) for i in range(numOfNeurons) ] else: self.neurons = [ neuron.Neuron(activation, input_num, lr, weights[i]) for i in range(numOfNeurons) ] self.lr = lr
def testSteady(self): # test toverify that Neuron evolves to steady state, # and verify that this is predicted by steady_state method # work with Yamada0 first Y0mpars = {"P": 0.9, "gamma": 1e-1, "kappa": 2, "beta": 1e-2} #use completely random initial state Y0params = { "model": "Yamada_0", "y0": np.random.random(2), "dt": 1.e-2, 'mpar': Y0mpars } Y0Neuron = neuron.Neuron(Y0params) # have state decay a bunch N = int(np.ceil(100 / Y0Neuron.dt)) x = np.zeros(N) y_out = Y0Neuron.solve(x) # also tests that steady state solver works y_steady = Y0Neuron.steady_state( [Y0mpars['beta'] / Y0mpars['kappa'], Y0mpars['P']]) npt.assert_array_almost_equal(y_out[-1, :], y_steady) #try with another neuron model Y1mpars = { "a": 2, "A": 6.3, "B": -6., "gamma1": 1e-1, "gamma2": 1e-1, "kappa": 2, "beta": 1e-3 } #use completely random initial state Y1params = { "model": "Yamada_1", "y0": np.random.random(3), "dt": 1.e-2, 'mpar': Y1mpars } Y1Neuron = neuron.Neuron(Y1params) # have state decay a bunch N1 = int(np.ceil(500 / Y1Neuron.dt)) x1 = np.zeros(N1) y1_out = Y1Neuron.solve(x1) # this should be close to the steady state, # note that Yamada neuron has 3 fixed points (2 unstable) in this region y1_steady_est = [ Y1mpars['beta'] / Y1mpars['kappa'], Y1mpars['A'], Y1mpars['B'] ] y1_steady = Y1Neuron.steady_state(y1_steady_est) npt.assert_array_almost_equal(y1_out[-1, :], y1_steady, decimal=3)
def makeMutant(self, potatoIndex): new = self.makeCopy() for i in range(potatoIndex): if random.randint(0, 1) == 0: #hidden layer n = random.randint(0, len(new.hiddenLayer) - 1) new.hiddenLayer[n] = neuron.Neuron(len(new.sensorLayer)) else: #action layer n = random.randint(0, len(new.actionLayer) - 1) new.actionLayer[n] = neuron.Neuron(len(new.hiddenLayer)) return new
def __createLayer(self, layerSize, flag=False): # flag - create 0 layer with img values arrLayer = [] for i in range(layerSize): # create all neurones for layer if flag == True: arrLayer.append( neuron.Neuron( len(self.layers), self.imgList[i], self.iterationOfCreation)) # load img to 0 layer else: arrLayer.append( neuron.Neuron(len(self.layers), 0, self.iterationOfCreation )) # leyer's number beginning from 0 self.layers.append(arrLayer) # add layer
def addNeuron(self, layerNumber): if layerNumber == 0: self.layers[0].append(_.X(self.speed)) elif layerNumber == len(self.layers)-1 or layerNumber == -1: self.layers[-1].append(_.Y(self.speed)) elif layerNumber > 0 and layerNumber < len(self.layers)-1: self.layers[layerNumber].append(_.Neuron(self.speed))
def test_set_weights(self): neuron1 = neuron.Neuron(3) weights = [.5] weights.append(-.2) weights.append(.3) neuron1.setWeights(weights) self.assertEqual(neuron1.getWeights(), weights)
def test_set_weight_bad_index(self, mock): neuron1 = neuron.Neuron(2) neuron1.setWeight(-1, [.5]) mock.assert_called_with('Cannot set the weight of the non existent input num: -1') neuron1.setWeight(3, [.5]) mock.assert_called_with('Cannot set the weight of the non existent input num: 3')
def test_RK4_vs_Euler(self): # check if RK4 stepper works in the same way as the Euler stepper Gaussian_pulse = lambda x, mu, sig: np.exp(-np.power(x - mu, 2.) / ( 2 * np.power(sig, 2.))) / (np.sqrt(2 * np.pi) * sig) Y1mpars = { "a": 2, "A": 6.5, "B": -6., "gamma1": 1e-1, "gamma2": 1e-1, "kappa": 2, "beta": 1e-2 } y1_steady_est = [ Y1mpars['beta'] / Y1mpars['kappa'], Y1mpars['A'], Y1mpars['B'] ] Y1params = { "model": "Yamada_1", "y0": y1_steady_est, "dt": 1.e-2, 'mpar': Y1mpars, 'solver': 'Euler' } #close enough to steady state Y1Neuron = neuron.Neuron(Y1params) y1_steady = Y1Neuron.steady_state(y1_steady_est) Y1params['solver'] = 'RK4' Y2Neuron = neuron.Neuron(Y1params) y2_steady = Y1Neuron.steady_state(y1_steady_est) #create time signal t1_end = 10. / Y1mpars["gamma1"] #atleast this long N1 = int(np.ceil(t1_end / Y1Neuron.dt)) time1 = np.linspace(0., (N1 - 1) * Y1Neuron.dt, num=N1) x1 = Gaussian_pulse(time1, 0.5 / Y1mpars["gamma1"], 1.) # create neuron, solve y1_out = Y1Neuron.solve(x1) y2_out = Y2Neuron.solve(x1) # calculate L2 norm of the difference of the two L2_err = np.sum((y1_out[3:] - y2_out[:-3])**2) / np.sum((y1_out)**2) # should throw an error if the outputs are significantly different self.assertTrue(L2_err < 1e-5)
def testYamadaPulsing(self): # test to verify Yamada pulses if given continuous input above threshold # also increase input and verify pulse period decreases Y1mpars = { "a": 1.8, "A": 5.7, "B": -5., "gamma1": 1e-2, "gamma2": 1e-2, "kappa": 1, "beta": 1e-3 } y1_steady_est = [ Y1mpars['beta'] / Y1mpars['kappa'], Y1mpars['A'], Y1mpars['B'] ] Y1params = { "model": "Yamada_1", "y0": y1_steady_est, "dt": 1.e-2, 'mpar': Y1mpars } #close enough to steady state Y1Neuron = neuron.Neuron(Y1params) y1_steady = Y1Neuron.steady_state(y1_steady_est) #create time signal t1_end = 10. / Y1mpars["gamma1"] #atleast this long N1 = int(np.ceil(t1_end / Y1Neuron.dt)) time1 = np.linspace(0., (N1 - 1) * Y1Neuron.dt, num=N1) x1 = np.zeros(N1) #make sure x1 amplitude is sufficient for spiking switchtime = 8. / Y1mpars["gamma1"] #increase drive at this point x1 += (0.5 * Y1mpars["gamma1"]) * np.heaviside( time1 - 0.5 / Y1mpars["gamma1"], 0.5) x1 += (1.5 * Y1mpars["gamma1"]) * np.heaviside(time1 - switchtime, 0.5) y1_out = Y1Neuron.solve(x1) (peaks, props) = sig.find_peaks(y1_out[:, 0], height=1e-2 * Y1mpars["kappa"] / Y1mpars["gamma1"]) peaktimes = time1[peaks] self.assertGreaterEqual(peaktimes.size, 2) #assert spiked atleast twice (peaktimes1, peaktimes2) = (np.array([]), np.array([])) for (i, time) in enumerate(peaktimes): if time <= switchtime: peaktimes1 = np.append(peaktimes1, time) else: peaktimes2 = np.append(peaktimes2, time) if peaktimes1.size < 2 or peaktimes2.size < 2: raise Exception("Not enough spikes to determine period") (per1, per2) = (np.mean(np.diff(peaktimes1)), np.mean(np.diff(peaktimes2))) self.assertGreaterEqual(peaktimes.size, 2) #assert spiked faster in part 2
def main(): print("Machine Learning Tutorial 1 - Making a Neurone") n = neuron.Neuron(numpy.array([0, 1]), 4) inputs = numpy.array([2, 3]) output = n.think(inputs) print(output)
def __init__(self, num_neurons, input_size): """ Initialzes the layer with the specified number of neurons, each of the given size. @param num_neurons - number of neurons in this layer @param input_size - length of signal each neuron must be able to process """ self._neurons = [neuron.Neuron(input_size) for _ in range(num_neurons)]
def __init__(self, alpha=.1, delta=.2): # Set the basic logger config. logging.basicConfig() self.alpha = alpha self.delta = delta # create a neuron that takes 2 inputs, has random starting weights from # -.5 to .5, and uses the default sign activation function. self.neuron = neuron.Neuron(2, [], 'step')
def __init__(self, name, learnConst, momentum): self.mom = momentum self.learnConst = learnConst self.weights = [] self.name = name self.neurons = [] bias = ne.Neuron(self.linear, name + "bias", True) bias.setValue(1) self.neurons.append(bias) self.say("Hello World!")
def __init__(self, length, numLayers, nodeCounts): temp = [length] + nodeCounts #initializing the layers with specified amount of nodes per layer self.layers = [[ neuron.Neuron(temp[i + 1]) for n in range(nodeCounts[i]) ] for i in range(numLayers)] self.pred = 0 self.arr = [] self.target = 0 self.errorList = []
def create_n1_double_burst(): n1 = nrn.Neuron( 'N1', { 'ach': 0, 'glu': -1 }, { 'ach': 1, 'glu': 0 }, (0, 1), { #thresholds -1: (MINUS_INFINITY, -1), 0: (-1, 1), 1: (1, INFINITY) }, ['charge-0', 'burst', 'burst-2'], { #state_trans_matrix 'charge-0': { -1: 'charge-0', 0: 'burst', 1: 'burst' }, 'burst': { -1: 'burst', 0: 'burst-2', 1: 'burst-2' }, 'burst-2': { -1: 'charge-0', 0: 'charge-0', 1: 'burst-2' } }, { #output_trans_matrix 'charge-0': { -1: 0, 0: 0, 1: 0 }, #'charge-1':{-1:0, 0:0, 1:0}, 'burst': { -1: 0, 0: 1, 1: 1 }, 'burst-2': { -1: 0, 0: 1, 1: 1 } }) return n1
def __init__(self, activation_funs, weights): if self._compare_lengths(activation_funs, weights): self.neurons = [] for f, W in zip(activation_funs, weights): neuron = n.Neuron(f, W, 0) self.neurons.append(neuron) self.neurons = np.array(self.neurons) self.input = None self.output = None self.delta = None self.error = None
def testIdentity(self): # test to make sure neuron step and neuron solve works for identity neuron # input=output Idparams = {"model": "identity", "y0": 0., "dt": 1.e-6} IdNeuron = neuron.Neuron(Idparams) DCin = 2. DCout = IdNeuron.step(DCin) self.assertAlmostEqual(DCin, DCout) #this should work for any step size or initial state Idparams2 = {"model": "identity", "y0": np.pi, "dt": 1.e2} IdNeuron2 = neuron.Neuron(Idparams2) DCin = 2. DCout2 = IdNeuron2.step(DCin) self.assertAlmostEqual(DCout, DCout2) #test neuron.solve for IdNeuron Inlength = 1e5 Idin = np.sin(np.linspace(0, 2. * np.pi, int(Inlength))) Idout = IdNeuron.solve(Idin) npt.assert_array_almost_equal(Idin[:-1, np.newaxis], Idout[1:])
def __init__(self, topology, learningRate=None): self.__layers = [] self.__error = 0.0 nLayers = len(topology) for layerNum in range(nLayers): self.__layers.append([]) nNeurons = topology[layerNum] + 1 # add a bias neuron for i in range(nNeurons): if layerNum == len(topology) - 1: # There is no forward link at the last layer self.__layers[layerNum].append( neuron.Neuron(0, i, neuron.Function.SIGMOID, learningRate)) else: self.__layers[layerNum].append( neuron.Neuron(topology[layerNum + 1], i, neuron.Function.HYPERBOLIC_TANGENT, learningRate)) # Force the bias node's output to 1.0 (it was the last neuron pushed in this layer) self.__layers[layerNum][-1].setOutput(1.0)
def create_hco(): n_tonic_pir = nrn.Neuron(name='N1', receptor_weights={ 'ACH': -1, 'GLU': 0 }, transmitter_emission={ 'ACH': 0, 'GLU': 1 }, activity_levels=(0, 1, 2), thresholds={ -2: (MINUS_INFINITY, -1), -1: (-1, -0.5), 0: (-0.5, 0.5), 1: (0.5, INFINITY) }, states=['act', 'inh'], state_trans_matrix={ 'act': { -2: 'inh', -1: 'inh', 0: 'act', 1: 'act' }, 'inh': { -2: 'inh', -1: 'act', 0: 'act', 1: 'act' } }, output_matrix={ 'act': { -2: 0, -1: 0, 0: 1, 1: 2 }, 'inh': { -2: 0, -1: 2, 0: 2, 1: 2 } }) n_tonic_pir_second = copy.deepcopy(n_tonic_pir) n_tonic_pir_second.name = 'N2' n_tonic_pir_second.receptor_weights = {'ACH': 0, 'GLU': -1} n_tonic_pir_second.transmitter_emission = {'ACH': 1, 'GLU': 0} return [n_tonic_pir, n_tonic_pir_second]
def __init__(self): # Create the initial layer of inputs neurons self.inputs = [] self.neurons = [] self.weights = [] self.history = [] for i in range(0, 256): n1 = neuron.SensoryNeuron() n2 = neuron.Neuron() w = neuron.Weight(n1, n2) self.inputs.append(n1) self.neurons.append(n2) self.weights.append(w)
def create_n3(): n3 = nrn.Neuron( 'N3', #receptors { 'ach': -1, 'glu': -0.25 }, { 'ach': 0, 'glu': 0.5 }, #output (0, 1, 2), { #thresholds -2: (MINUS_INFINITY, -1), -1: (-1, -0.5), 0: (-0.5, 0.5), 1: (0.5, INFINITY) }, ['act', 'inh'], #states { #state_trans_matrix 'act': { -2: 'inh', -1: 'act', 0: 'act', 1: 'act' }, 'inh': { -2: 'inh', -1: 'act', 0: 'act', 1: 'act' } }, { #output matrix 'act': { -2: 0, -1: 0, 0: 1, 1: 2 }, 'inh': { -2: 0, -1: 2, 0: 2, 1: 2 } }) return n3
class NeuronTests(unittest.TestCase): NEURON_TEST_DATA = [3, 4] neuron = neuron.Neuron(np.random.random((1, 2))) def testNeuronOutput(self): neuronOutput = self.neuron.computeWeightedSum( np.array(self.NEURON_TEST_DATA)) self.assertEqual(neuronOutput, self._computeNeuronWeightedSum()) def _computeNeuronWeightedSum(self): weightedSum = self.NEURON_TEST_DATA[0] * self.neuron.getWeight( 0) + self.NEURON_TEST_DATA[1] * self.neuron.getWeight(1) return weightedSum def testNeuronWeightsValidation(self): self.assertRaises(ValueError, neuron.Neuron, np.random.random((5, 2))) self.assertRaises(ValueError, neuron.Neuron, np.random.random( (1, 1, 1)))
def __init__(self, size=4, weight=5, speedCoeff=0.2, selfOrganized=False, layerCritical=100, modCritical=100, modified=False): self.size = size self.__speedCoeff = speedCoeff self.__modCritical = modCritical self.__rCritical = layerCritical self.__dimension = weight self.__isModified = modified self.__isSelfOrganized = selfOrganized for i in range(0, size): self.neuronList.append( Neu.Neuron(random_weight(weight), speedCoeff, modified, modCritical, 3, False))
def create_n2(): n2 = nrn.Neuron( 'N2', #receptors { 'ach': 1, 'glu': 0 }, { 'ach': 0, 'glu': 4 }, #output (0, 1), { #thresholds -1: (MINUS_INFINITY, -1), 0: (-1, 1), 1: (1, INFINITY) }, ['rest', 'burst'], { #state_trans_matrix 'rest': { -1: 'rest', 0: 'rest', 1: 'burst' }, 'burst': { -1: 'rest', 0: 'rest', 1: 'burst' } }, { #output_trans_matrix 'rest': { -1: 0, 0: 0, 1: 0 }, 'burst': { -1: 0, 0: 1, 1: 1 } }) return n2