(self, xdata, ydata, train_round, accuracy)
| 124 | print("bias.shape ", np.shape(layer.bias)) |
| 125 | |
| 126 | def train(self, xdata, ydata, train_round, accuracy): |
| 127 | self.train_round = train_round |
| 128 | self.accuracy = accuracy |
| 129 | |
| 130 | self.ax_loss.hlines(self.accuracy, 0, self.train_round * 1.1) |
| 131 | |
| 132 | x_shape = np.shape(xdata) |
| 133 | for _ in range(train_round): |
| 134 | all_loss = 0 |
| 135 | for row in range(x_shape[0]): |
| 136 | _xdata = np.asmatrix(xdata[row, :]).T |
| 137 | _ydata = np.asmatrix(ydata[row, :]).T |
| 138 | |
| 139 | # forward propagation |
| 140 | for layer in self.layers: |
| 141 | _xdata = layer.forward_propagation(_xdata) |
| 142 | |
| 143 | loss, gradient = self.cal_loss(_ydata, _xdata) |
| 144 | all_loss = all_loss + loss |
| 145 | |
| 146 | # back propagation: the input_layer does not upgrade |
| 147 | for layer in self.layers[:0:-1]: |
| 148 | gradient = layer.back_propagation(gradient) |
| 149 | |
| 150 | mse = all_loss / x_shape[0] |
| 151 | self.train_mse.append(mse) |
| 152 | |
| 153 | self.plot_loss() |
| 154 | |
| 155 | if mse < self.accuracy: |
| 156 | print("----达到精度----") |
| 157 | return mse |
| 158 | return None |
| 159 | |
| 160 | def cal_loss(self, ydata, ydata_): |
| 161 | self.loss = np.sum(np.power((ydata - ydata_), 2)) |
no test coverage detected