def valid_on_epoch(self, epoch, filename, memory): self.logger.info("Epoch {:02} {} begins validing ...................".format(epoch, self.tag)) self.model.eval() start_time = time.time() score = Fscore(self.tag) datas = ActDataset.read_file(filename, memory) for pair in datas: cnet = pair['cnet'] class_string = pair['label'] gold_classes = ActDataset.class_info(class_string) pred_classes = decode_act(self.model, cnet, memory, self.cuda) score.update_tp_fp_fn(pred_classes, gold_classes) fscore = score.output_fscore(self.logger, epoch) elapsed_time = time.time() - start_time self.logger.info("Epoch {:02} {} end validing elapsed_time: {:6.0f}s".format( epoch, self.tag, elapsed_time) ) self.logger.info('*****************************************************') return fscore
def valid_on_epoch(self, epoch, filename, memory): self.logger.info( "Epoch {:02} {} begins validing ...................".format( epoch, self.tag)) self.model.eval() start_time = time.time() score = Fscore(self.tag) lines = SLUDataset.read_file(filename) for (utterance, class_string) in lines: gold_classes = SLUDataset.class_info(class_string) pred_classes = decode_slu(self.model, utterance, memory, self.cuda) score.update_tp_fp_fn(pred_classes, gold_classes) fscore = score.output_fscore(self.logger, epoch) elapsed_time = time.time() - start_time self.logger.info( "Epoch {:02} {} ends validing elapsed_time: {:6.0f}s".format( epoch, self.tag, elapsed_time)) self.logger.info( '*****************************************************') return fscore
def valid_on_epoch(self, data_iter, epoch): self.logger.info("Epoch {:02} begins validation .......................".format(epoch)) self.model.eval() valid_score = Fscore('Valid') start_time = time.time() for (i, data_batch) in enumerate(data_iter): datas, lengths, labels, raw_classes, _ = data_batch probs = self.model(datas, lengths) valid_score = self.update_fscore(valid_score, probs.data, raw_classes, data_iter.idx2class) elapsed_time = time.time() - start_time self.logger.info("Epoch {:02} Valid, elapsed time: {:6.0f}s".format( epoch, elapsed_time )) fscore = valid_score.output_fscore(self.logger, epoch) return fscore
def train_on_epoch(self, data_iter, epoch): self.logger.info("Epoch {:02} begins training .......................".format(epoch)) self.model.train() train_score = Fscore('Train') total_loss = 0. total_sent = 0. start_time = time.time() for (i, data_batch) in enumerate(data_iter): # data batch definition datas, lengths, labels, raw_classes, _ = data_batch probs = self.model(datas, lengths) # loss calculation loss = self.criterion(probs, labels) # statistics loss_data = loss.data.clone() total_loss += loss_data.item() total_sent += datas.size(0) train_score = self.update_fscore(train_score, probs.data, raw_classes, data_iter.idx2class) # update parameters self.model.zero_grad() loss.backward() self.optimizer.step() elapsed_time = time.time() - start_time avg_loss = total_loss / total_sent self.logger.info("Epoch {:02} Train, loss: {:6.2f}; elapsed time: {:6.0f}s".format( epoch, avg_loss, elapsed_time )) fscore = train_score.output_fscore(self.logger, epoch) return fscore