如何使TensorFlow SimpleRNN静态以在Colab TPU上进行训练?

问题描述

从TPU常见问题中阅读以下内容https://cloud.google.com/tpu/docs/faq

我可以在Compute Engine上训练递归神经网络(RNN)吗?

在某些配置中,tf.static_rnn()和tf.dynamic_rnn()与当前的TPU执行引擎兼容。 更一般而言,TPU支持tf.while_loop()和TensorArray,用于实现tf.dynamic_rnn()。 TPU不支持诸如CuDNN之类的专用工具箱,因为它们包含GPU特定的代码。 在TPU上使用tf.while_loop()确实需要指定循环迭代次数的上限,以便TPU执行引擎可以静态确定内存使用情况。

如何使我的SimpleRNN静态或有效以在Colab TPU上运行?

Colab TPU代码

import tensorflow as tf
import os
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense,SimpleRNN

resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
tf.config.experimental_connect_to_cluster(resolver)
tf.tpu.experimental.initialize_tpu_system(resolver)
print("All devices: ",tf.config.list_logical_devices('TPU'))

strategy = tf.distribute.TPUStrategy(resolver)

with strategy.scope():  
  model = Sequential()
  model.add(SimpleRNN(units=32,input_shape=(1,step),activation="relu"))
  model.add(Dense(16,activation="relu"))
  model.add(Dense(1))
  model.compile(loss='mean_squared_error',optimizer='rmsprop')

model.fit(X,y,epochs=50,batch_size=16,verbose=0)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)