我想改变我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'
在tensorflow网站上所指示的,keras_to_tpu_model方法似乎实验。它是否最近被删除?如果是这样,我怎么能继续利用TPU的估计我Keras模式?如果keras_to_tpu_model方法是仍然可用,为什么我不能调用它?
我假设你定义你TPU_WORKER如下
import os
TPU_WORKER = ‘grpc://’ + os.environ[‘COLAB_TPU_ADDR’]
相反,你的模型转换为TPU,打造一个分发策略。这是通过该批次将被分发到的八个的TPU以及如何从每个损失将被计算的方法。
resolver = tf.contrib.cluster_resolver.TPUClusterResolver(TPU_WORKE)
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')
从tensorflow导入keras。这是因为tf.contrib.tpu.keras_to_tpu_model( )
”需要tensorflow版本产品,而不是keras版本。
例如,改用from tensorflow.keras.layers import Dense, Activation
。等等。