Python制作神经网络,代码如下:
import numpy
import scipy.special
#neural network class definition
class neuralNetwork:
#initialise the neural network
def __init__(self , inputnodes, hiddennodes, outputnodes,learningrate):
# set number of nodes in each input, hidden, output layer
self.inodes = inputnodes
self.hnodes = hiddennodes
self.onodes = outputnodes
# link weight matrices
self.wih = numpy.random.normal(0.0, pow(self.hnodes,-0.5),
(self.hnodes, self.inodes))
self.who = numpy.random.normal(0.0, pow(self.onodes,-0.5),
(self.onodes, self.hnodes))
# learning rate
self.lr = learningrate
# activation function is the sigmoid function
self.activation_function = lambda x:scipy.special.expit(x)
print("初始化权重矩阵wih