def intermediate_step(self, net: NeuralNet, m, param) -> None: #for param, grad in net.params_and_grads(): param = np.add(param, self.gamma * m) predicted = net.forward(net.curr_batch.inputs) grad = net.loss_f.grad(predicted, net.curr_batch.targets) net.backward(grad)
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: NeuralNet, inputs: Tensor, targets: Tensor, num_epochs: int = 5000, iterator: DataIterator = BatchIterator(), loss: Loss = MSE(), optimizer: Optimizer = SGD(0.001), eps: float = -1) -> [float]: loss_list = [] eval_list = [] net.n_eval = 0 for epoch in range(num_epochs): n_iter = 0 epoch_loss = 0.0 print(f'================ EPOCH NUMBER {epoch + 1} ================') for batch in iterator(inputs, targets): #print(f'batch: \n{batch}') net.n_iter = n_iter net.curr_batch = batch net.loss_f = loss predicted = net.forward(batch.inputs) curr_loss = loss.loss(predicted, batch.targets) epoch_loss += curr_loss grad = loss.grad(predicted, batch.targets) net.backward(grad) optimizer.step(net) n_iter = n_iter + 1 eval_list.append(net.n_eval) #eval_list.append(net.n_eval) # () / iterator.batch_size print(epoch, epoch_loss) loss_list.append(epoch_loss) if eps > 0 and epoch_loss < eps: print('precisão atingida') break return loss_list, eval_list