我正在尝试运行一个简单的Keras脚本,并将Google Colab与TensorBoard结合使用。这是我的代码:
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)])
这是我看到的将TensorBoard与Colab结合使用的建议,例如:Can I use Tensorboard with Google Colab?
但是,添加回调时出现错误:
FailedPreconditionError:读取资源变量时出错 来自Container:localhost的conv_dw_8_2 / depthwise_kernel。这可能 表示该变量未初始化。找不到:资源 本地主机/ conv_dw_8_2 / depthwise_kernel / N10tensorflow3VarE不 存在。 [[节点:conv_dw_8_2 / depthwise / ReadVariableOp = ReadVariableOpdtype = DT_FLOAT, _device =“ / job:localhost /副本:0 / task:0 / device:GPU:0”]] [[节点:loss_2 / mul / _147 = _Recvclient_terminated = false, recv_device =“ / job:localhost /副本:0 / task:0 / device:CPU:0”, send_device =“ / job:localhost /副本:0 / task:0 / device:GPU:0”, send_device_incarnation = 1,tensor_name =“ edge_6752_loss_2 / mul”, tensor_type = DT_FLOAT, _device =“ / job:localhost /副本:0 /任务:0 /设备:CPU:0”]]
有人知道我在做什么错吗?如果我可以使它在Colab上运行TensorBoard,这似乎是一种非常有用的方法。
答案 0 :(得分:1)
这是由于Keras版本冲突造成的。导入Keras API的Tensorboardcolab实现时,keras library使用完整的tf.keras。因此,当您拟合模型时,最终会使用两种不同版本的keras。
您有几种选择:
import tensorflow as tf
import keras
from keras.datasets import cifar10
from keras.applications.mobilenet import MobileNet
from tensorboardcolab import TensorBoardColab, TensorBoardColabCallback
尽管代码在这些更改下运行良好,但是您可以考虑使用Keras's version of the Adam optimizer,因此您不再需要显式导入tensorflow。
model.compile(loss='categorical_crossentropy',
optimizer=keras.optimizers.Adam(lr=0.001),
metrics=['accuracy'])`
如果您修补callbacks.py和core.py并在那里修复导入,您的代码将运行良好:
from keras.callbacks import TensorBoard
from tensorflow.keras.callbacks import TensorBoard
您也可以在进行这些更改的地方使用this fork。