在 google colab 中使用 TPU 时,出现以下错误
InvalidArgumentError:无法为操作 Adam/iterations/IsInitialized/VarIsInitializedOp 分配设备:{{node Adam/iterations/IsInitialized/VarIsInitializedOp}} 已明确分配给 /job:worker/replica:0/task:0/device:TPU:0但可用的设备是 [ /job:localhost/replica:0/task:0/device:CPU:0, /job:localhost/replica:0/task:0/device:XLA_CPU:0 ]。确保设备规范引用了有效的设备。[[亚当/迭代/IsInitialized/VarIsInitializedOp]]
TPU_WORKER = 'grpc://' + os.environ['COLAB_TPU_ADDR']
resolver = tf.contrib.cluster_resolver.TPUClusterResolver(TPU_WORKER)
tf.contrib.distribute.initialize_tpu_system(resolver)
strategy = tf.contrib.distribute.TPUStrategy(resolver)
with strategy.scope():
# Setup the model inputs / outputs
model = Model(inputs=[inputs_img, inputs_mask], outputs=outputs)
# Compile the model
model.compile(
optimizer = Adam(lr=lr),
loss=self.loss_total(inputs_mask)
)