【问题标题】:Tensorflow 2: Getting Tensor ValueTensorflow 2:获取张量值
【发布时间】:2019-07-05 09:47:23
【问题描述】:

我正在切换到 TF2,我只是关注了这个tutorial, 现在将 train 和 step 函数定义为“@tf.function”。

如何打印张量 y_pred 和 loss 的值?

@tf.function
def train_step(images, labels):
  with tf.GradientTape() as tape:
    predictions = model(images)
    loss = loss_object(labels, predictions)

    print("train preds: ", y_pred)
    print("train loss: ", loss)

  gradients = tape.gradient(loss, model.trainable_variables)
  optimizer.apply_gradients(zip(gradients, model.trainable_variables))

  train_loss(loss)
  train_accuracy(labels, predictions)

【问题讨论】:

    标签: python tensorflow tensorflow2.0


    【解决方案1】:

    print 在 Python 世界中执行(不在图中),因此它只会在 tf.function 跟踪您的函数以构造图时打印张量一次。如果要在图中打印,请使用tf.print

    【讨论】:

      猜你喜欢
      • 2020-02-03
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多