如何将keras模型转换为tpu模型 [英] How to transform keras model to tpu model

查看:666
本文介绍了如何将keras模型转换为tpu模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to transform my Keras model in the Google cloud console into a TPU model. Unfortunatelly I am getting an error as shown below. My minimal example is the following:

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方法仍然可用,为什么我不能调用它?

The keras_to_tpu_model method seems experimental as indicated on the tensorflow website. Has it recently been removed? If so, how can I proceed to make use of TPUs to estimate my Keras model? If the keras_to_tpu_model method would be still available, why can I not invoke it?

推荐答案

我假设您按照以下方式定义了TPU_WORKER

I am assuming you defined you TPU_WORKER as below

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

建立分销策略,而不是将模型转换为TPU.这是将批次分配到八个TPU以及如何计算每个TPU的损失的方法.

Instead of converting your model to TPU, build a distribution strategy. This is the method by which the batch will be distributed to the eight TPUs and how the loss from each will be calculated.

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

通过策略构建和编译模型.对于回归分析,这应该工作得很好.

With the strategy build and compile your model. This should work quite nicely for regression.

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')

这篇关于如何将keras模型转换为tpu模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆