Ejemplo n.º 1
0
def train():
    # 创建训练、验证 批数据
    sp_input_tensor, en_target_tensor, sp_input_language_tokenizer, en_target_language_tokenizer = data_util.load_dataset(
        resource_data_file)
    sp_input_tensor_train, sp_input_tensor_val, en_target_tensor_train, en_target_tensor_val = data_util.train_test_split(
        en_input_tensor, sp_target_tensor, test_size=0.2)
    dataset = tf.data.Dataset.from_tensor_slices(
        (sp_input_tensor_train,
         en_target_tensor_train)).shuffle(len(sp_input_tensor_train))
    dataset = dataset.batch(config['batch_size'], drop_remainder=True)
    for epoch in range(config['epochs']):
        start = time.time()
        enc_hidden = tf.zeros(
            (config["batch_size"], config["enc_hidden_size"]))
        total_loss = 0
        for (batch, (input_lang, target_lang)) in enumerate(dataset):
            # input_lang--> [batch_size, max_input_length], target_lang-->[batch_size, max_target_length]
            batch_loss = seq2seqModel.train_step(input_lang, target_lang,
                                                 en_target_language_tokenizer,
                                                 enc_hidden)
            total_loss += batch_loss
            if batch % 100 == 0:
                print('Epoch %d, Batch %d, Loss %.4f' %
                      (epoch + 1, batch, batch_loss.numpy()))
        # 2 个 epoch, 保存一个 checkpoint
        if (epoch + 1) % 2 == 0:
            seq2seqModel.checkpoint.save(file_prefix=config['model_dir'])
        print("Epoch %d, Loss %.4f" %
              (epoch + 1, total_loss /
               (len(sp_input_tensor_train) // config['batch_size'])))
        print("Time taken for one epoch %f sec" % (time.time() - start))
Ejemplo n.º 2
0
def train():
    print("Preparing data in %s" % gConfig['train_data'])
    steps_per_epoch = len(input_tensor) // gConfig['batch_size']
    print(steps_per_epoch)
    #enc_hidden = seq2seqModel.Encoder.initHidden()
    checkpoint_dir = gConfig['model_data']
    #ckpt=io.gfile.listdir(checkpoint_dir)
    # if ckpt:
    #  print("reload pretrained model")
    #seq2seqModel.checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))

    checkpoint_dir = gConfig['model_data']
    checkpoint_prefix = os.path.join(checkpoint_dir, ".pt")
    start_time = time.time()
    encoder1 = seq2seqModel.Encoder(input_lang.n_words, hidden_size).to(device)
    attn_decoder1 = seq2seqModel.AttentionDencoder(hidden_size,
                                                   target_lang.n_words,
                                                   dropout_p=0.1).to(device)
    #print(target_tensor)
    #print(input_tensor)
    max_data = gConfig['max_train_data_size']
    while True:
        start_time_epoch = time.time()
        total_loss = 0
        for i in range(0, max_data - 1):
            inp = input_tensor[i]
            targ = target_tensor[i]
            batch_loss = seq2seqModel.train_step(
                inp, targ, encoder1, attn_decoder1,
                optim.SGD(encoder1.parameters(), lr=0.001),
                optim.SGD(encoder1.parameters(), lr=0.001))
            total_loss += batch_loss

            print('训练总步数:{} 最新每步loss {:.4f}'.format(i, batch_loss))

        step_time_epoch = (time.time() - start_time_epoch) / steps_per_epoch
        step_loss = total_loss / steps_per_epoch
        current_steps = +steps_per_epoch
        step_time_total = (time.time() - start_time) / current_steps

        print('训练总步数: {} 每步耗时: {}  最新每步耗时: {} 最新每步loss {:.4f}'.format(
            current_steps, step_time_total, step_time_epoch, step_loss))
        torch.save(
            {
                'modelA_state_dict': encoder1.state_dict(),
                'modelB_state_dict': attn_decoder1.state_dict()
            }, checkpoint_prefix)

        sys.stdout.flush()
Ejemplo n.º 3
0
def train():
    print("Preparing data in %s" % gConfig['train_data'])
    steps_per_epoch = len(input_tensor) // gConfig['batch_size']
    print(steps_per_epoch)
    enc_hidden = seq2seqModel.encoder.initialize_hidden_state()
    checkpoint_dir = gConfig['model_data']
    ckpt = tf.io.gfile.listdir(checkpoint_dir)

    manager = tf.train.CheckpointManager(seq2seqModel.checkpoint,
                                         directory=checkpoint_dir,
                                         max_to_keep=5)

    if ckpt:
        print("reload pretrained model")
        # seq2seqModel.checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))
        seq2seqModel.checkpoint.restore(manager.latest_checkpoint)

    BUFFER_SIZE = len(input_tensor)
    dataset = tf.data.Dataset.from_tensor_slices(
        (input_tensor, target_tensor)).shuffle(BUFFER_SIZE)
    dataset = dataset.batch(BATCH_SIZE, drop_remainder=True)
    checkpoint_dir = gConfig['model_data']
    checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
    start_time = time.time()

    while True:
        start_time_epoch = time.time()
        total_loss = 0
        for (batch, (inp, targ)) in enumerate(dataset.take(steps_per_epoch)):
            batch_loss = seq2seqModel.train_step(inp, targ, target_token,
                                                 enc_hidden)
            total_loss += batch_loss
            print(batch_loss.numpy())

        step_time_epoch = (time.time() - start_time_epoch) / steps_per_epoch
        step_loss = total_loss / steps_per_epoch
        current_steps = +steps_per_epoch
        step_time_total = (time.time() - start_time) / current_steps

        print('训练总步数: {} 每步耗时: {}  最新每步耗时: {} 最新每步loss {:.4f}'.format(
            current_steps, step_time_total, step_time_epoch,
            step_loss.numpy()))
        # seq2seqModel.checkpoint.save(file_prefix=checkpoint_prefix)
        manager.save()

        sys.stdout.flush()
Ejemplo n.º 4
0
def train():
    print("Preparing data in %s" % CONFIG['train_data'])
    steps_per_epoch = len(input_tensor) // CONFIG['batch_size']
    print(steps_per_epoch)
    enc_hidden = seq2seqModel.encoder.initialize_hidden_state()
    checkpoint_dir = CONFIG['model_data']
    ckpt = tf.io.gfile.listdir(checkpoint_dir)
    if ckpt:
        print("reload pretrained model")
        seq2seqModel.checkpoint.restore(
            tf.train.latest_checkpoint(checkpoint_dir))
    dataset = tf.data.Dataset.from_tensor_slices(
        (input_tensor, target_tensor)).shuffle(len(input_tensor))
    dataset = dataset.batch(batch_size, drop_remainder=True)
    checkpoint_dir = CONFIG['model_data']
    checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
    start_time = time.time()

    while True:
        start_time_epoch = time.time()
        total_loss = 0

        for (batch, (inp, target)) in enumerate(dataset.take(steps_per_epoch)):
            batch_loss = seq2seqModel.train_step(inp, target, target_lang,
                                                 enc_hidden)
            total_loss += batch_loss
            print(batch_loss.numpy())

        step_time_epoch = (time.time() - start_time_epoch) / steps_per_epoch
        step_loss = total_loss / steps_per_epoch

        current_steps = +steps_per_epoch
        step_time_total = (time.time() - start_time) / current_steps
        print('训练总步数: {}, 每步耗时: {}, 最新每步耗时: {}, 最新每步loss值 {:.4f}'.format(
            current_steps, step_time_total, step_time_epoch,
            step_loss.numpy()))

        seq2seqModel.checkpoint.save(file_prefix=checkpoint_prefix)

        sys.stdout.flush()