def test(data_loader, model, device): # set model in validating mode. model.eval() # model.to(device) correct = 0 n = 0 for (batch_x, batch_len, batch_y, batch_x_tag) in data_loader: n += len(batch_y) bx, bx_len, bx_mask, by, bx_tag = prepare_input( batch_x, batch_len, batch_y, batch_x_tag) batch_x = bx.to(device) batch_len = bx_len.to(device) batch_y = by.to(device) batch_max = bx_mask.to(device) try: output, _ = model(batch_x, batch_len, batch_max) # get the index of the max log-probability _, y_p = output.max(1) correct += (y_p == batch_y).sum().item() except: print("in test ..... ") print(batch_x.shape) print(batch_len.shape) print(batch_y.shape) input() pass eval_acc = correct / n # set model back to training mode. model.train() return eval_acc
def test(data_loader, model, device): # set model in validating mode. model.eval() # model.to(device) correct = 0 n = 0 for batch_x, batch_len, batch_y in data_loader: n += len(batch_y) bx, bx_len, bx_mask, by = prepare_input(batch_x, batch_len, batch_y) batch_x = bx.to(device) batch_x_mask = bx_mask.to(device) batch_len = bx_len.to(device) batch_y = by.to(device) output = model(batch_x, batch_x_mask, batch_len) # get the index of the max log-probability y_p = output.max(1)[1] correct += (y_p == batch_y).sum().item() eval_acc = correct / n # set model back to training mode. model.train() return eval_acc
def train_epoch(self, data_loader, report_step=100): stop_next_epoch = False train_loss = 0. train_acc = 0. n_sample = 0. for step, (batch_x, batch_len, batch_y, batch_x_tag) in enumerate(data_loader): n_sample += len(batch_y) bx, bx_len, bx_mask, by, bx_tag = prepare_input( batch_x, batch_len, batch_y, batch_x_tag) batch_x = bx.to(self.device) batch_x_mask = bx_mask.to(self.device) batch_len = bx_len.to(self.device) batch_y = by.to(self.device) batch_x_tag = bx_tag.to(self.device) self.pred, tags_logits = self.model(batch_x, batch_len, batch_x_mask) # Step 4. Compute the loss, gradients, and update the parameters by # calling optimizer.step() loss = self.criterion(tags_logits, batch_x_tag, self.pred, batch_y) self.optimizer.zero_grad() loss.backward() torch.nn.utils.clip_grad_norm_(self.parameters, max_norm=self.clip) self.optimizer.step() train_loss += loss.item() y_p = self.pred.max(dim=1)[1] train_correct = (y_p == batch_y).sum().item() train_acc += train_correct if (step + 1) % report_step == 0: train_loss /= n_sample train_acc /= n_sample if train_loss < 1.e-4: stop_next_epoch = True print("[step_%05d] loss: %0.4f, acc: %0.4f" % (step + 1, train_loss, train_acc)) train_loss = 0. train_acc = 0. n_sample = 0. return stop_next_epoch
def get_attention_scores(data_loader, model, device): # set model in validating mode. model.eval() # model.to(device) scores = [] data_lens = [] n = 0 for (batch_x, batch_len, batch_y, batch_x_tag) in data_loader: n += len(batch_y) bx, bx_len, bx_mask, by, bx_tag = prepare_input( batch_x, batch_len, batch_y, batch_x_tag) batch_x = bx.to(device) batch_len = bx_len.to(device) batch_max = bx_mask.to(device) batch_scores = model.get_attention(batch_x, batch_len, batch_max) batch_scores = batch_scores.cpu().detach().numpy() scores.append(batch_scores) data_lens.append(bx_len) return scores, data_lens