class Perceptron(object): def __init__(self, input_size, output_size): W1 = 0.01 * np.random.randn(input_size, output_size) b1 = np.zeros(output_size) self.loss_layer = SigmoidWithLoss() self.layers = [Affine(W1, b1)] self.params, self.grads = [], [] for layer in self.layers: self.params += layer.params self.grads += layer.grads return def predict(self, X): for layer in self.layers: X = layer.forward(X) return X def forward(self, X, y): score = self.predict(X) loss = self.loss_layer.forward(score, y) return loss def backward(self, dout=1): dout = self.loss_layer.backward(dout) for layer in reversed(self.layers): dout = layer.backward(dout) return dout
def forward(self, xs, ts): _, T = xs.shape self.xs_shape = xs.shape self.layers = [] loss = 0 for t in range(T): layer = SigmoidWithLoss() loss += layer.forward(xs[:, t], ts[:, t]) self.layers.append(layer) return loss / T