我知道如何在本地计算机上查看张量板图,同时我的神经网络使用以下代码在本地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中本地查看的解决方案,但这并没有显示任何内容。我的代码中缺少什么让它在本地的tensorboard上工作吗?和/或其他解决方案,可以在Google Colab的Tensorboard中查看日志数据?
如果它对于解决方案的细节很重要,那么我使用的是Mac。另外,我在网上看到的教程显示了在使用fit
代码时如何将Tensorboard与Google Colab结合使用,但看不到如何修改不使用fit
而是train_on_batch
的代码。
!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
!unzip ngrok-stable-linux-amd64.zip
get_ipython().system_raw('tensorboard --logdir /content/trainingdata/objectdetection/ckpt_output/trainingImatges/ --host 0.0.0.0 --port 6006 &')
get_ipython().system_raw('./ngrok http 6006 &')
! curl -s http://localhost:4040/api/tunnels | python3 -c \
"import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
这将为您创建的日志文件提供一个张量板。这将为colab上的张量板创建一个隧道,并使其可通过ngrok提供的公共URL进行访问。运行最终命令时,将打印公共URL。它与TF1.13一起使用。我想您也可以对TF2使用相同的方法。