-
StackOverflow 文件
-
neural-network 教程
-
神經網路入門
-
Python Neuron 類
import numpy as np #There is a lot of math in neurons, so use numpy to speed things up in python; in other languages, use an efficient array type for that language
import random #Initial neuron weights should be random
class Neuron:
def __init__(self, nbr_inputs, weight_array = None):
if (weight_array != None): #you might already have a trained neuron, and wish to recreate it by passing in a weight array h ere
self.weight_array = weight_array
else: #...but more often, you generate random, small numbers for the input weights. DO NOT USE ALL ZEROES, or you increase the odds of getting stuck when learning
self.weight_array = np.zeros(nbr_inputs+1)
for el in range(nbr_inputs+1): #+1 to account for bias weight
self.weight_array[el] = random.uniform((-2.4/nbr_inputs),(2.4/nbr_inputs))
self.nbr_inputs = nbr_inputs
def neuron_output(self,input_array):
input_array_with_bias = np.insert(input_array,0,-1)
weighted_sum = np.dot(input_array_with_bias,self.weight_array)
#Here we are using a hyperbolic tangent output; there are several output functions which could be used, with different max and min values and shapes
self.output = 1.716 * np.tanh(0.67*weighted_sum)
return self.output