MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / BPNN

Class BPNN

neural_network/bpnn.py:96–170  ·  view source on GitHub ↗

Back Propagation Neural Network model

Source from the content-addressed store, hash-verified

94
95
96class BPNN():
97 '''
98 Back Propagation Neural Network model
99 '''
100 def __init__(self):
101 self.layers = []
102 self.train_mse = []
103 self.fig_loss = plt.figure()
104 self.ax_loss = self.fig_loss.add_subplot(1,1,1)
105
106 def add_layer(self,layer):
107 self.layers.append(layer)
108
109 def build(self):
110 for i,layer in enumerate(self.layers[:]):
111 if i < 1:
112 layer.is_input_layer = True
113 else:
114 layer.initializer(self.layers[i-1].units)
115
116 def summary(self):
117 for i,layer in enumerate(self.layers[:]):
118 print('------- layer %d -------'%i)
119 print('weight.shape ',np.shape(layer.weight))
120 print('bias.shape ',np.shape(layer.bias))
121
122 def train(self,xdata,ydata,train_round,accuracy):
123 self.train_round = train_round
124 self.accuracy = accuracy
125
126 self.ax_loss.hlines(self.accuracy, 0, self.train_round * 1.1)
127
128 x_shape = np.shape(xdata)
129 for round_i in range(train_round):
130 all_loss = 0
131 for row in range(x_shape[0]):
132 _xdata = np.asmatrix(xdata[row,:]).T
133 _ydata = np.asmatrix(ydata[row,:]).T
134
135 # forward propagation
136 for layer in self.layers:
137 _xdata = layer.forward_propagation(_xdata)
138
139 loss, gradient = self.cal_loss(_ydata, _xdata)
140 all_loss = all_loss + loss
141
142 # back propagation
143 # the input_layer does not upgrade
144 for layer in self.layers[:0:-1]:
145 gradient = layer.back_propagation(gradient)
146
147 mse = all_loss/x_shape[0]
148 self.train_mse.append(mse)
149
150 self.plot_loss()
151
152 if mse < self.accuracy:
153 print('----达到精度----')

Callers 1

exampleFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected