Tensorflow Google Colab TPU比GPU花费更多的时间

Tensorflow Google Colab TPU比GPU花费更多的时间,tensorflow,keras,google-colaboratory,google-cloud-tpu,mlp,Tensorflow,Keras,Google Colaboratory,Google Cloud Tpu,Mlp,下面是我正在使用的代码。我注释掉了将我的模型转换为TPU模型的行。使用GPU处理相同数量的数据,一个历元需要7秒,而使用TPU则需要90秒 Inp = tf.keras.Input(name='input', shape=(input_dim,), dtype=tf.float32) x = tf.keras.layers.Dense(900, kernel_initializer='uniform', activation='relu', input_dim=input_di

下面是我正在使用的代码。我注释掉了将我的模型转换为TPU模型的行。使用GPU处理相同数量的数据,一个历元需要7秒,而使用TPU则需要90秒

    Inp = tf.keras.Input(name='input', shape=(input_dim,), dtype=tf.float32)
    x = tf.keras.layers.Dense(900, kernel_initializer='uniform',  activation='relu', input_dim=input_dim, name = 'Dense_01')(Inp)
    x = tf.keras.layers.Dropout(0.3, name = 'Dropout_02')(x)
    output = tf.keras.layers.Dense(stop_criteria, activation='softmax',name = 'Dense_02')(x)

    model = tf.keras.Model(inputs=[Inp], outputs=[output])
    opt = tf.train.AdamOptimizer(.001)
    model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['acc'])

    '''tpu_model = tf.contrib.tpu.keras_to_tpu_model(model,
                                                  strategy=tf.contrib.tpu.TPUDistributionStrategy(
                                                      tf.contrib.cluster_resolver.TPUClusterResolver(TPU_ADDRESS)))'''
    model.fit(X_tra, y_tra, epochs=5, batch_size=batch_size, shuffle=False,
              validation_split=0.1, verbose=2)

这是指向的链接。您是否尝试过下面示例中的
tpu\u model.fit\u generator
方法? 另一部分看起来不错。 另外,一个问题可能是使用Adam优化器。有smth。关于它,但是我忘了链接在哪里了。尝试另一个优化器和下面的代码,如果另一个优化器工作,您知道它一定是smth。使用Adam优化器

tf.keras.backend.clear_session()

training_model = lstm_model(seq_len=100, batch_size=128, stateful=False)

tpu_model = tf.contrib.tpu.keras_to_tpu_model(
    training_model,
    strategy=tf.contrib.tpu.TPUDistributionStrategy(
        tf.contrib.cluster_resolver.TPUClusterResolver(TPU_WORKER)))

tpu_model.fit_generator(
    training_generator(seq_len=100, batch_size=1024),
    steps_per_epoch=100,
    epochs=10,
)
tpu_model.save_weights('/tmp/bard.h5', overwrite=True)

你能分享一个重现减速的笔记本吗?@BobSmith我更新了这个问题,并提供了到colab notebook的链接。你检查过你的脚本是否适合TPU用例吗?@Jaroslav我最终使用了GPU,因为我的数据集没有那么大。@mjosh你是指TPU还是GPU?我尝试了RMSPropOptimizer,但结果仍然一样。