python - 当我尝试转换 Keras MLP 时,为什么 Google Colab 会给出 "unknown device"错误?

标签 python tensorflow keras google-colaboratory

我正在尝试使用 TPU 在 google colab 上训练一个简单的 MLP 模型。 但是,当我尝试使用

转换模型时
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from keras.constraints import NonNeg
model = Sequential()
model.add(Dense(57,input_shape=(57,)))
model.add(Dense(60,kernel_constraint=NonNeg(),activation="relu"))
model.add(Dense(100,kernel_constraint=NonNeg(),activation="relu"))
model.add(Dense(50,kernel_constraint=NonNeg(),activation="relu"))
model.add(Dense(3,activation="linear"))
model.compile(optimizer='adam',loss='mse')

TPU_WORKER = 'grpc://' + os.environ['COLAB_TPU_ADDR']
tpu_model = tf.contrib.tpu.keras_to_tpu_model(
    model,
    strategy=tf.contrib.tpu.TPUDistributionStrategy(
        tf.contrib.cluster_resolver.TPUClusterResolver(TPU_WORKER)))

给出错误InvalidArgumentError:/job:localhost/replica:0/task:0/device:TPU_SYSTEM:0未知设备。 full error here .

这对我来说没有意义,因为我直接从谷歌教程复制了模型转换代码。当我运行测试代码时:

import os
import pprint
import tensorflow as tf

if 'COLAB_TPU_ADDR' not in os.environ:
  print('ERROR: Not connected to a TPU runtime; please see the first cell in this notebook for instructions!')
else:
  tpu_address = 'grpc://' + os.environ['COLAB_TPU_ADDR']
  print ('TPU address is', tpu_address)

  with tf.Session(tpu_address) as session:
    devices = session.list_devices()

  print('TPU devices:')
  pprint.pprint(devices)

我得到了预期的结果:

TPU address is grpc://10.0.203.10:8470
TPU devices:
[_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:CPU:0, CPU, -1, 1941375595625340814),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:XLA_CPU:0, XLA_CPU, 17179869184, 9079881000847066378),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:0, TPU, 17179869184, 6922694346479333534),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:1, TPU, 17179869184, 14324637633413341896),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:2, TPU, 17179869184, 3528106575831937158),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:3, TPU, 17179869184, 13852141601322651612),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:4, TPU, 17179869184, 10344791506504172772),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:5, TPU, 17179869184, 16666353711371098164),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:6, TPU, 17179869184, 3428083526573573796),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:7, TPU, 17179869184, 8632908473312514763),
 _DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU_SYSTEM:0, TPU_SYSTEM, 17179869184, 9715206562754100387)]

既然这样有效,我不明白为什么模型转换不起作用。

最佳答案

如果启用了急切执行,您可能希望将其关闭。
对我来说,在代码开头没有 tf.enable_eager_execution() 似乎进展顺利。

关于python - 当我尝试转换 Keras MLP 时,为什么 Google Colab 会给出 "unknown device"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55246749/

相关文章:

python - 通过在 Tensorflow 中将固定数量的行相加来减少矩阵

tensorflow - 将 keras 模型另存为 .h5

python - 如何在 Tensorflow 中为 VGG16 获取梯度

python - 由于 tf.keras.preprocessing.text.Tokenizer.texts_to_sequences 上的 np.hstack 导致尺寸(形状)发生变化

python - 比较二维 numpy 数组的元素

python - 在 CSV 文件中搜索特定值的单个列并返回整行

python - 编译加载模型时 Keras ValueError

python - 如何在 Django 中表示学生与讲师的关系?

python - 如何在json文件中写入变量?

python - 无效参数错误 : input_1_1:0 is both fed and fetched