【发布时间】:2020-06-23 22:36:26
【问题描述】:
我知道如何在我的本地机器上查看张量板图,同时我的神经网络使用本地 Jupyter Notebook 中的代码进行训练,使用以下代码。当我使用 Google Colab 来训练神经网络时,我需要做些什么不同的事情?使用 train_on_batch 时,我在网上看不到任何教程/示例。
定义我的模型(convnet)之后...
convnet.compile(loss='categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(0.001),
metrics=['accuracy']
)
# create tensorboard graph data for the model
tb = tf.keras.callbacks.TensorBoard(log_dir='Logs/Exp_15',
histogram_freq=0,
batch_size=batch_size,
write_graph=True,
write_grads=False)
tb.set_model(convnet)
num_epochs = 3
batches_processed_counter = 0
for epoch in range(num_epochs):
for batch in range(int(train_img.samples/batch_size)):
batches_processed_counter = batches_processed_counter + 1
# get next batch of images & labels
X_imgs, X_labels = next(train_img)
#train model, get cross entropy & accuracy for batch
train_CE, train_acc = convnet.train_on_batch(X_imgs, X_labels)
# validation images - just predict
X_imgs_val, X_labels_val = next(val_img)
val_CE, val_acc = convnet.test_on_batch(X_imgs_val, X_labels_val)
# create tensorboard graph info for the cross entropy loss and training accuracies
# for every batch in every epoch (so if 5 epochs and 10 batches there should be 50 accuracies )
tb.on_epoch_end(batches_processed_counter, {'train_loss': train_CE, 'train_acc': train_acc})
# create tensorboard graph info for the cross entropy loss and VALIDATION accuracies
# for every batch in every epoch (so if 5 epochs and 10 batches there should be 50 accuracies )
tb.on_epoch_end(batches_processed_counter, {'val_loss': val_CE, 'val_acc': val_acc})
print('epoch', epoch, 'batch', batch, 'train_CE:', train_CE, 'train_acc:', train_acc)
print('epoch', epoch, 'batch', batch, 'val_CE:', val_CE, 'val_acc:', val_acc)
tb.on_train_end(None)
我可以看到日志文件已在 Google Colab 运行时中成功生成。如何在 Tensorboard 中查看?我已经看到了描述将日志文件下载到本地机器并在本地 tensorboard 中查看的解决方案,但这并没有显示任何内容。我的代码中是否缺少某些内容以允许它在本地的张量板上工作?和/或在 Google Colab 的 Tensorboard 中查看日志数据的替代解决方案?
如果它对解决方案的细节很重要,我在 Mac 上。此外,我在网上看到的教程展示了如何在使用 fit 代码时将 Tensorboard 与 Google Colab 一起使用,但看不到如何修改我不使用 fit 而是使用 train_on_batch 的代码。
【问题讨论】:
标签: tensorflow keras google-colaboratory tensorboard