在图中每组运算放大器及其相关的电阻、电容组成的网络代表一个神经元。每个神经元有两组输入,一组是恒定的外部电流,另一组是来自其他运算放大器输出的正向或反向的反馈连接。假设第i个神经元的内部膜电位为Ui(i=1,2,…,n),细胞膜的输入电容和传递电阻分别为Ci和Ri,神经元的输出电位为Vi,外部输入电流为Ii,并用电阻Rij(i,j=1,2,…,n)来模拟第i个和第j个神经元之间的突触特性。由基尔霍夫电流定律(Kirchhoff’s Cureent Law ,KCL)可知,放大器输入节点处的流入电流和流出电流保持平衡,亦即有下式成立:
defset_weights(self, weights): """Update the weights array""" if weights.shape != (self._num_inputs, self._num_inputs): raise InvalidWeightsException()
self._weights = weights
defget_weights(self): """Return the weights array""" return self._weights defcalculate_neuron_output(self, neuron, input_pattern): """Calculate the output of the given neuron""" num_neurons = len(input_pattern)
s = 0.0
for j inrange(num_neurons): s += self._weights[neuron][j] * input_pattern[j]
return1.0if s > 0.0else -1.0
defrun_once(self, update_list, input_pattern): """Iterate over every neuron and update it's output""" result = input_pattern.copy()
changed = False for neuron in update_list: neuron_output = self.calculate_neuron_output(neuron, result)
if neuron_output != result[neuron]: result[neuron] = neuron_output changed = True
return changed, result
defrun(self, input_pattern, max_iterations=10): """Run the network using the input data until the output state doesn't change or a maximum number of iteration has been reached.""" iteration_count = 0