def train(self, session): """ 模型训练函数 :param session: :return: """ training_set = SequenceData(filename=self.conf.training_data, max_seq_len=self.seq_max_len) for step in range(1, self.conf.training_steps + 1): batch_x, batch_y, batch_seqlen = training_set.next(self.conf.batch_size) # Run optimization op (backprop) session.run(self.optimizer_op, feed_dict={self.X: batch_x, self.Y: batch_y, self.seqlen: batch_seqlen, self.dropout_keep_prob: self.conf.dropout_keep_prob}) if step % self.conf.display_steps == 0 or step == 1: # Calculate batch accuracy & loss acc, loss = session.run([self.accuracy_op, self.loss_op], feed_dict={self.X: batch_x, self.Y: batch_y, self.seqlen: batch_seqlen, self.dropout_keep_prob: self.conf.dropout_keep_prob}) print("Step " + str(step) + ", Minibatch Loss= " + \ "{:.6f}".format(loss) + ", Training Accuracy= " + \ "{:.5f}".format(acc)) print("Optimization Finished!") print('Start to save model.') saver = tf.train.Saver() saver.save(session, self.conf.save_model_path)
def train(self): """ train :return: """ print 'Start training model.' training_set = SequenceData(filename=self.config.training_file, max_seq_len=self.max_seq_len) for i in range(self.config.training_steps): batch_x, batch_y, batch_seqlen, batch_words = training_set.next(self.config.batch_size) batch_x = batch_x.to(device) _, batch_y = torch.max(batch_y, 1) # 元组第一个维度为最大值,第二个维度为最大值的索引 batch_y = batch_y.to(device) batch_seqlen = batch_seqlen.to(device) # Forward pass outputs, all_outputs = self.model(batch_x, batch_seqlen) # loss = self.cross_entropy_loss(outputs, batch_y) loss = self.cross_entropy_loss(outputs, batch_y) + \ self.regularized_loss(all_outputs, batch_seqlen, batch_words) # loss = self.criterion(outputs, batch_y) # Backward and optimize self.optimizer.zero_grad() # 清空梯度缓存 loss.backward() # 反向传播,计算梯度 self.optimizer.step() # 利用梯度更新模型参数 if (i + 1) % 100 == 0: print 'Step [{}/{}], Loss: {:.4f}'\ .format(i + 1, self.config.training_steps, loss.item()) # Save the model checkpoint print 'Start saving model to "%s".' % self.config.save_model_path torch.save(self.model.state_dict(), self.config.save_model_path)