简体   繁体   English

Colab+Keras+TensorBoard FailedPreconditionError

[英]Colab+Keras+TensorBoard FailedPreconditionError

I'm trying to run a simple Keras script and use Google Colab with TensorBoard.我正在尝试运行一个简单的 Keras 脚本并将 Google Colab 与 TensorBoard 结合使用。 Here's my code:这是我的代码:

import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.applications.mobilenet import MobileNet
from tensorboardcolab import TensorBoardColab, TensorBoardColabCallback

# Settings
num_classes = 10
batch_size = 16
epochs = 1

# Data setup
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

# Select model
model = MobileNet(weights=None, input_shape=x_train.shape[1:], classes=num_classes)

# Select loss, optimizer, metric
model.compile(loss='categorical_crossentropy',
                            optimizer=tf.train.AdamOptimizer(0.001),
                            metrics=['accuracy'])    
# Train
tbc=TensorBoardColab()
model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, y_test), 
                    callbacks=[TensorBoardColabCallback(tbc)])

This is a suggestion I saw to use TensorBoard with Colab as referenced here: Can I use Tensorboard with Google Colab?这是我看到的将 TensorBoard 与 Colab 结合使用的建议,如下所述: Can I use Tensorboard with Google Colab?

However, when adding the callback I get the error:但是,在添加回调时出现错误:

FailedPreconditionError: Error while reading resource variable conv_dw_8_2/depthwise_kernel from Container: localhost. FailedPreconditionError:从容器读取资源变量 conv_dw_8_2/depthwise_kernel 时出错:localhost。 This could mean that the variable was uninitialized.这可能意味着该变量未初始化。 Not found: Resource localhost/conv_dw_8_2/depthwise_kernel/N10tensorflow3VarE does not exist.未找到:资源 localhost/conv_dw_8_2/depthwise_kernel/N10tensorflow3VarE 不存在。 [[Node: conv_dw_8_2/depthwise/ReadVariableOp = ReadVariableOpdtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"]] [[Node: loss_2/mul/_147 = _Recvclient_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_6752_loss_2/mul", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]] [[节点:conv_dw_8_2/depthwise/ReadVariableOp = ReadVariableOpdtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"]] [[节点: loss_2/mul/_147 = _Recvclient_terminated= false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation= 1、tensor_name="edge_6752_loss_2/mul", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

Does anybody know what I'm doing wrong?有谁知道我做错了什么? This seems like a very useful way to run TensorBoard on Colab if I could get it working.如果可以的话,这似乎是在 Colab 上运行 TensorBoard 的一种非常有用的方法。

This is caused by conflicting versions of Keras.这是由 Keras 版本冲突引起的。 Tensorboardcolab uses the full keras library while you import the tf.keras implementation of the Keras API.在您导入Keras API 的tf.keras实现时, Tensorboardcolab使用完整的keras 库 So when you fit the model you end up using two different versions of keras.因此,当您拟合模型时,您最终会使用两个不同版本的 keras。

You have a few options:您有几个选择:

Use Keras libary and change your imports使用 Keras 库并更改您的导入

import tensorflow as tf
import keras
from keras.datasets import cifar10
from keras.applications.mobilenet import MobileNet
from tensorboardcolab import TensorBoardColab, TensorBoardColabCallback

Although the code runs fine with these changes, you might consider using Keras's version of the Adam optimizer , so you don't need to import tensorflow explicitly anymore.尽管代码在这些更改后运行良好,但您可以考虑使用Keras 版本的 Adam 优化器,因此您不再需要显式导入 tensorflow。

model.compile(loss='categorical_crossentropy', 
                    optimizer=keras.optimizers.Adam(lr=0.001), 
                    metrics=['accuracy'])`

Use tf.keras and patch TensorBoardColab使用 tf.keras 并修补 TensorBoardColab

Your code runs fine, if you'd patch callbacks.py and core.py and fix the imports there:如果您修补callbacks.pycore.py并修复那里的导入,您的代码运行良好:

from keras.callbacks import TensorBoard from tensorflow.keras.callbacks import TensorBoard

You could also use this fork where I made these changes.您也可以在我进行这些更改的地方使用这个 fork

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM