def train(net: NeuralNet, inputs: Tensor, targets: Tensor, num_epochs: int = 5000, iterator: DataIterator = BatchIterator(), loss: Loss = MSE(), optimizer: Optimizer = SGD() ) -> None: for epoch in range(num_epochs): epoch_loss = 0.0 for batch in iterator(inputs, targets): predicted = net.forward(batch.inputs) epoch_loss += loss.loss(predicted, batch.targets) grad = loss.grad(predicted, batch.targets) net.backward(grad) optimizer.step(net) print(epoch, epoch_loss)
def train(net: NetWork, inputs: Tensor, targets: Tensor, epochs: int = 500, loss: Loss = MSE(), optimizer: Optimizer = SGD(), iterator: DataIterator = BatchIterator(), show_info: bool = False): for epoch in range(epochs): epoch_loss = .0 for batch_inputs, batch_targets in iterator(inputs, targets): predictions = net.forward(batch_inputs) epoch_loss += loss.loss(predictions, batch_targets) grad = loss.grad(predictions, batch_targets) net.backward(grad) optimizer.step(net) if show_info: print('epoch:{}, loss:{}'.format(epoch, epoch_loss))