1import numpy
2import math
3import random
4from matplotlib import pyplot
56classNeural:78# constructor9def__init__(self, n_input, n_hidden, n_output):10 self.hidden_weight = numpy.random.random_sample((n_hidden, n_input +1))11 self.output_weight = numpy.random.random_sample((n_output, n_hidden +1))12 self.hidden_momentum = numpy.zeros((n_hidden, n_input +1))13 self.output_momentum = numpy.zeros((n_output, n_hidden +1))141516# public method17deftrain(self, X, T, epsilon, mu, epoch):18 self.error = numpy.zeros(epoch)19 N = X.shape[0]20for epo inrange(epoch):21for i inrange(N):22 x = X[i,:]23 t = T[i,:]2425 self.__update_weight(x, t, epsilon, mu)2627 self.error[epo]= self.__calc_error(X, T)282930defpredict(self, X):31 N = X.shape[0]32 C = numpy.zeros(N).astype('int')33 Y = numpy.zeros((N, X.shape[1]))34for i inrange(N):35 x = X[i,:]36 z, y = self.__forward(x)3738 Y[i]= y
39 C[i]= y.argmax()4041return(C, Y)424344deferror_graph(self):45 pyplot.ylim(0.0,2.0)46 pyplot.plot(numpy.arange(0, self.error.shape[0]), self.error)47 pyplot.show()484950# private method51def__sigmoid(self, arr):52return numpy.vectorize(lambda x:1.0/(1.0+ math.exp(-x)))(arr)535455def__forward(self, x):56# z: output in hidden layer, y: output in output layer57 z = self.__sigmoid(self.hidden_weight.dot(numpy.r_[numpy.array([1]), x]))58 y = self.__sigmoid(self.output_weight.dot(numpy.r_[numpy.array([1]), z]))5960return(z, y)6162def__update_weight(self, x, t, epsilon, mu):63 z, y = self.__forward(x)6465# update output_weight66 output_delta =(y - t)* y *(1.0- y)67 _output_weight = self.output_weight
68 self.output_weight -= epsilon * output_delta.reshape((-1,1))* numpy.r_[numpy.array([1]), z]- mu * self.output_momentum
69 self.output_momentum = self.output_weight - _output_weight
7071# update hidden_weight72 hidden_delta =(self.output_weight[:,1:].T.dot(output_delta))* z *(1.0- z)73 _hidden_weight = self.hidden_weight
74 self.hidden_weight -= epsilon * hidden_delta.reshape((-1,1))* numpy.r_[numpy.array([1]), x]75 self.hidden_momentum = self.hidden_weight - _hidden_weight
767778def__calc_error(self, X, T):79 N = X.shape[0]80 err =0.081for i inrange(N):82 x = X[i,:]83 t = T[i,:]8485 z, y = self.__forward(x)86 err +=(y - t).dot((y - t).reshape((-1,1)))/2.08788return err
89
main.py
python
1from neuralnetwork import*23if __name__ =='__main__':45 X = numpy.array([[0,0],[0,1],[1,0],[1,1]])6 T = numpy.array([[1,0],[0,1],[0,1],[1,0]])7 N = X.shape[0]# number of data89 input_size = X.shape[1]10 hidden_size =211 output_size =212 epsilon =0.113 mu =0.914 epoch =100001516 nn = Neural(input_size, hidden_size, output_size)17 nn.train(X, T, epsilon, mu, epoch)18 nn.error_graph()1920 C, Y = nn.predict(X)2122for i inrange(N):23 x = X[i,:]24 y = Y[i,:]25 c = C[i]2627print(x)28print(y)29print(c)30print("")
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/17 10:30
2017/11/18 15:31
2017/11/20 07:18
2017/11/20 09:55
2017/11/20 10:32
2017/11/20 11:06
2017/11/26 08:01 編集
2017/11/26 13:40
2017/11/27 01:17
2017/11/28 11:20
2017/12/02 06:59