如何在张量流中运行 model.fit() 期间输出一些数据? [英] How can I output some data during a model.fit() run in tensorflow?

查看:78
本文介绍了如何在张量流中运行 model.fit() 期间输出一些数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 model.fit() 运行期间而不是之前打印张量的值和/或形状.在 PyTorch 中,我可以将 print(input.shape) 语句放入 model.forward() 函数中.

I would like to print the value and/or the shape of a tensor during a model.fit() run and not before. In PyTorch I can just put a print(input.shape) statement into the model.forward() function.

TensorFlow 中是否有类似的东西?

Is there something similar in TensorFlow?

推荐答案

您可以将 callback 对象传递给 model.fit() 方法,然后在拟合过程中的不同阶段.

You can pass a callback object to the model.fit() method and then perform actions at different stages during fitting.

https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/Callback

class MyCustomCallback(tf.keras.callbacks.Callback):

  def on_train_batch_begin(self, batch, logs=None):
    print('Training: batch {} begins at {}'.format(batch, datetime.datetime.now().time()))

  def on_train_batch_end(self, batch, logs=None):
    print('Training: batch {} ends at {}'.format(batch, datetime.datetime.now().time()))

  def on_test_batch_begin(self, batch, logs=None):
    print('Evaluating: batch {} begins at {}'.format(batch, datetime.datetime.now().time()))

  def on_test_batch_end(self, batch, logs=None):
    print('Evaluating: batch {} ends at {}'.format(batch, datetime.datetime.now().time()))


model = get_model()
model.fit(x_train, y_train, callbacks=[MyCustomCallback()])

https://www.tensorflow.org/guide/keras/custom_callback

这篇关于如何在张量流中运行 model.fit() 期间输出一些数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆