gpt4 book ai didi

keras - Keras:将Tensorboard与train_on_batch()结合使用

转载 作者:行者123 更新时间:2023-12-03 22:31:05 24 4
gpt4 key购买 nike

对于keras函数fit()fit_generator(),可以通过将keras.callbacks.TensorBoard对象传递给函数来实现张量板可视化。对于train_on_batch()函数,显然没有可用的回调。在这种情况下,keras中还有其他选项可用来创建Tensorboard吗?

最佳答案

创建TensorBoard回调并手动驱动的一种可能方法:

# This example shows how to use keras TensorBoard callback
# with model.train_on_batch

import tensorflow.keras as keras

# Setup the model
model = keras.models.Sequential()
model.add(...) # Add your layers
model.compile(...) # Compile as usual

batch_size=256

# Create the TensorBoard callback,
# which we will drive manually
tensorboard = keras.callbacks.TensorBoard(
log_dir='/tmp/my_tf_logs',
histogram_freq=0,
batch_size=batch_size,
write_graph=True,
write_grads=True
)
tensorboard.set_model(model)

# Transform train_on_batch return value
# to dict expected by on_batch_end callback
def named_logs(model, logs):
result = {}
for l in zip(model.metrics_names, logs):
result[l[0]] = l[1]
return result

# Run training batches, notify tensorboard at the end of each epoch
for batch_id in range(1000):
x_train,y_train = create_training_data(batch_size)
logs = model.train_on_batch(x_train, y_train)
tensorboard.on_epoch_end(batch_id, named_logs(model, logs))

tensorboard.on_train_end(None)

关于keras - Keras:将Tensorboard与train_on_batch()结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44861149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com