Python 如何查找Tensorflow中的训练损耗和Valu损失,注意神经机器翻译 我从本教程中学习神经机器翻译。

Python 如何查找Tensorflow中的训练损耗和Valu损失,注意神经机器翻译 我从本教程中学习神经机器翻译。 ,python,tensorflow,machine-learning,deep-learning,Python,Tensorflow,Machine Learning,Deep Learning,但在本教程中似乎没有train_loss和val_loss(仅batch_loss) 有没有办法像我们用另一个模型那样获得损失值历史记录 前 在这些教程中,实际上有。当他们使用 for epoch in range(EPOCHS): start = time.time() enc_hidden = encoder.initialize_hidden_state() total_loss = 0 for (batch, (inp, targ)) in enumerate(dat

但在本教程中似乎没有
train_loss
val_loss
(仅
batch_loss

有没有办法像我们用另一个模型那样获得损失值历史记录


在这些教程中,实际上有。当他们使用

for epoch in range(EPOCHS):
  start = time.time()

  enc_hidden = encoder.initialize_hidden_state()
  total_loss = 0

  for (batch, (inp, targ)) in enumerate(dataset.take(steps_per_epoch)):
    batch_loss = train_step(inp, targ, enc_hidden)
    total_loss += batch_loss
这样,他们就可以计算出
train\u step
方法产生的训练损失。但没有验证集,因此不会显示验证丢失


根据您的评论,您需要编写
test\u步骤
函数,并在训练循环中使用它。下面是获得验证损失的最小表示

@tf.function
def test_step(inp, targ, enc_hidden):
    loss = 0 
    enc_output, enc_hidden = encoder(inp, enc_hidden, training=False)
    dec_hidden = enc_hidden
    dec_input = tf.expand_dims([targ_lang.word_index['<start>']] * BATCH_SIZE, 1)

    for t in range(1, targ.shape[1]):
      predictions, dec_hidden, _ = decoder(dec_input, dec_hidden, 
                                           enc_output, training=False)
      loss += loss_function(targ[:, t], predictions)
      dec_input = tf.expand_dims(targ[:, t], 1)
      
    batch_loss = (loss / int(targ.shape[1]))
    return batch_loss
其次,


如果你是这个领域的新手,我建议你从一些简单的例子开始。因为那篇教程使用了
自定义训练循环
,我认为这对初学者来说可能很难。因此,最好从以下示例开始:-混合复杂性。非常感谢您的帮助,但是在这种情况下有没有找到验证丢失的方法?更新,请查看。
@tf.function
def test_step(inp, targ, enc_hidden):
    loss = 0 
    enc_output, enc_hidden = encoder(inp, enc_hidden, training=False)
    dec_hidden = enc_hidden
    dec_input = tf.expand_dims([targ_lang.word_index['<start>']] * BATCH_SIZE, 1)

    for t in range(1, targ.shape[1]):
      predictions, dec_hidden, _ = decoder(dec_input, dec_hidden, 
                                           enc_output, training=False)
      loss += loss_function(targ[:, t], predictions)
      dec_input = tf.expand_dims(targ[:, t], 1)
      
    batch_loss = (loss / int(targ.shape[1]))
    return batch_loss
EPOCHS = 5
history = {'loss':[], 'val_loss':[]}

for epoch in range(EPOCHS):
    start = time.time()
    
    enc_hidden = encoder.initialize_hidden_state()
    total_loss = 0
    for (batch, (inp, targ)) in enumerate(dataset.take(steps_per_epoch)):
        batch_loss = train_step(inp, targ, enc_hidden)
        total_loss += batch_loss
    if (epoch + 1) % 2 == 0:
        checkpoint.save(file_prefix=checkpoint_prefix)
    history['loss'].append(total_loss.numpy()/steps_per_epoch)
    print(f'Epoch {epoch+1} Loss {total_loss/steps_per_epoch:.4f}')

    total_loss = 0
    for (batch, (inp, targ)) in enumerate(dataset.take(steps_per_epoch)):
        batch_loss = test_step(inp, targ, enc_hidden)
        total_loss += batch_loss
    history['val_loss'].append(total_loss.numpy()/steps_per_epoch)
    print(f'Epoch {epoch+1} Val Loss {total_loss/steps_per_epoch:.4f}')
        
    print(f'Time taken for 1 epoch {time.time()-start:.2f} sec\n')
history['loss']
history['val_loss']