2

我正在尝试将 Google 云控制台中的 Keras 模型转换为 TPU 模型。不幸的是,我收到如下所示的错误。我的最小示例如下:

import keras
from keras.models import Sequential
from keras.layers import Dense, Activation
import tensorflow as tf
import os
model = Sequential()
model.add(Dense(32, input_dim=784))
model.add(Dense(32))
model.add(Activation('relu'))
model.compile(optimizer='rmsprop', loss='mse')
tpu_model = tf.contrib.tpu.keras_to_tpu_model(
    model,
    strategy=tf.contrib.tpu.TPUDistributionStrategy(
         tf.contrib.cluster_resolver.TPUClusterResolver(TPU_WORKER)))

我的输出是:

Using TensorFlow backend.
Traceback (most recent call last):
     File "cloud_python4.py", line 11, in <module>
     tpu_model = tf.contrib.tpu.keras_to_tpu_model(AttributeError: module 'tensorflow.contrib.tpu' has no attribute 'keras_to_tpu_model'

keras_to_tpu_model 方法似乎是实验性的,如 tensorflow 网站所示。它最近被删除了吗?如果是这样,我该如何继续使用 TPU 来估计我的 Keras 模型?如果 keras_to_tpu_model 方法仍然可用,为什么我不能调用它?

4

2 回答 2

5

我假设您将 TPU_WORKER 定义如下

import os
TPU_WORKER = ‘grpc://’ + os.environ[‘COLAB_TPU_ADDR’]

与其将模型转换为 TPU,不如构建分发策略。这是将批次分配到八个 TPU 以及如何计算每个 TPU 的损失的方法。

resolver = tf.contrib.cluster_resolver.TPUClusterResolver(TPU_WORKER)
tf.contrib.distribute.initialize_tpu_system(resolver)
strategy = tf.contrib.distribute.TPUStrategy(resolver)

使用该策略构建并编译您的模型。这对于回归应该非常有效。

with strategy.scope():
  model = Sequential() 
  model.add(Dense(32, input_dim=784))
  model.add(Dense(32))
  model.add(Activation('relu'))
  model.compile(optimizer='rmsprop', loss='mse')

于 2019-08-10T10:20:05.943 回答
2

从 tensorflow 导入 keras。这是因为tf.contrib.tpu.keras_to_tpu_model( )' 需要tensorflow版本的 Model,而不是 keras 的版本。

例如,from tensorflow.keras.layers import Dense, Activation改为使用。等等。

于 2019-02-05T16:04:55.257 回答