def validate(self): self.model.eval() val_total_loss = 0.0 mes = "Epoch {}, validation average loss:{:.4f}, Perplexity:{:.4f}" with torch.no_grad(): for imgs, tgt4training, tgt4cal_loss in self.val_loader: imgs = imgs.to(self.device) tgt4training = tgt4training.to(self.device) tgt4cal_loss = tgt4cal_loss.to(self.device) epsilon = cal_epsilon(self.args.decay_k, self.total_step, self.args.sample_method) logits = self.model(imgs, tgt4training, epsilon) loss = cal_loss(logits, tgt4cal_loss) val_total_loss += loss try: avg_loss = val_total_loss / len(self.val_loader) except: avg_loss = val_total_loss print(mes.format(self.epoch, avg_loss, 2**avg_loss)) wandb.log({ "epoch": self.epoch, "val avrg loss": avg_loss, "perplexit": 2**avg_loss }) if avg_loss < self.best_val_loss: self.best_val_loss = avg_loss self.save_model('best_ckpt') return avg_loss
def train_step(self, imgs, tgt4training, tgt4cal_loss): self.optimizer.zero_grad() imgs = imgs.to(self.device) tgt4training = tgt4training.to(self.device) tgt4cal_loss = tgt4cal_loss.to(self.device) epsilon = cal_epsilon(self.args.decay_k, self.total_step, self.args.sample_method) logits = self.model(imgs, tgt4training, epsilon) # calculate loss loss = cal_loss(logits, tgt4cal_loss) self.step += 1 self.total_step += 1 loss.backward() clip_grad_norm_(self.model.parameters(), self.args.clip) self.optimizer.step() return loss.item()