(self,xdata,ydata,train_round,accuracy)
| 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('----达到精度----') |
| 154 | return mse |
| 155 | |
| 156 | def cal_loss(self,ydata,ydata_): |
| 157 | self.loss = np.sum(np.power((ydata - ydata_),2)) |
no test coverage detected