简体   繁体   English

使用功能 API 和 tf.GradientTape() 的组合在 Tensorflow 2.0 中进行训练时,如何将模型图记录到张量板?

[英]How to log model graph to tensorboard when using combination to Functional API and tf.GradientTape() to train in Tensorflow 2.0?

Can some please guide me on how to log model graph to tensorboard when I am using Keras Functional API or the model sub calling API to create the model and tf.GradientTape() to train the model?当我使用 Keras Functional API 或模型子调用 API 来创建模型和 tf.GradientTape() 来训练模型时,有人可以指导我如何将模型图记录到张量板吗?

# Get the model.
inputs = keras.Input(shape=(784,), name='digits')
x = layers.Dense(64, activation='relu', name='dense_1')(inputs)
x = layers.Dense(64, activation='relu', name='dense_2')(x)
outputs = layers.Dense(10, activation='softmax', name='predictions')(x)
model = keras.Model(inputs=inputs, outputs=outputs)


optimizer = keras.optimizers.SGD(learning_rate=1e-3)
loss_fn = keras.losses.SparseCategoricalCrossentropy()


batch_size = 64
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_dataset = train_dataset.shuffle(buffer_size=1024).batch(batch_size)

# Iterate over epochs.
epochs = 3
for epoch in range(epochs):
    print('Start of epoch %d' % (epoch,))

    for step, (x_batch_train, y_batch_train) in enumerate(train_dataset):   

    with tf.GradientTape() as tape:
        logits = model(x_batch_train)
        loss_value = loss_fn(y_batch_train, logits)

    grads = tape.gradient(loss_value, model.trainable_weights)


    optimizer.apply_gradients(zip(grads, model.trainable_weights))

    if step % 200 == 0:
        print('Training loss (for one batch) at step %s: %s' % (step, float(loss_value)))
        print('Seen so far: %s samples' % ((step + 1) * 64))

Where should I insert the tensorboard logging for the model graph?我应该在哪里插入模型图的张量板日志记录?

The best way to do this (TF 2.3.0, TB 2.3.0) is to use tf.summary and pass the model through a wrapper function with the @tf.function decorator.执行此操作的最佳方法(TF 2.3.0,TB 2.3.0)是使用tf.summary并通过带有@tf.function装饰器的包装函数传递模型。

In your case, to export the model graph to TensorBoard for inspection:在您的情况下,要将模型图导出到 TensorBoard 进行检查:

inputs = keras.Input(shape=(784,), name='digits')
x = layers.Dense(64, activation='relu', name='dense_1')(inputs)
x = layers.Dense(64, activation='relu', name='dense_2')(x)
outputs = layers.Dense(10, activation='softmax', name='predictions')(x)
model = keras.Model(inputs=inputs, outputs=outputs)


# Solution Code:
writer = tf.summary.create_file_writer('./logs/')

@tf.function
def init_model(data, model):
    model(data)

tf.summary.trace_on(graph=True)
init_model(tf.zeros((1,784), model)

with writer.as_default():
    tf.summary.trace_export('name', step=0)

Looking at the logs files in TensorBoard you should see a model graph as such.查看 TensorBoard 中的日志文件,您应该会看到这样的模型图。 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 tensorflow 2.0:tf.GradientTape()。gradient()返回None - tensorflow 2.0: tf.GradientTape().gradient() returns None 为什么在 tensorflow 2 中使用 tf.GradientTape 进行训练与使用 fit API 进行训练有不同的行为? - Why does training using tf.GradientTape in tensorflow 2 have different behavior to training using fit API? 使用 tf.GradientTape() wrt 输入的梯度是 None (Tensorflow 2.4) - gradient using tf.GradientTape() wrt inputs is None (Tensorflow 2.4) TF 2.0中的tf.GradientTape是否相当于tf.gradients? - Is tf.GradientTape in TF 2.0 equivalent to tf.gradients? Tensorflow tf.GradientTape() 应该只使用 Tf.variables? - Tensorflow tf.GradientTape() should only use Tf.variables? 子类 API 模型在 tf.gradienttape() 中不起作用(没有为操作“IteratorGetNext”定义梯度(操作类型:IteratorGetNext)) - Subclass API Model Not work in tf.gradienttape() (No gradient defined for operation 'IteratorGetNext' (op type: IteratorGetNext)) 使用 TensorFlow 2.0 Alpha 时无法在 Tensorboard 中看到 keras 模型图 - Unable to see keras model graph in Tensorboard when using TensorFlow 2.0 Alpha 使用tf的预训练模型进行转移学习.GradientTape无法收敛 - Transfer learning with pretrained model by tf.GradientTape can't converge tf.GradientTape() 位置对模型训练时间的影响 - Effect of the position of tf.GradientTape() in model training time tf.GradientTape 如何记录 with 语句内部的操作? - How does tf.GradientTape record operations inside the with statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM