【问题标题】:Google Colab TPU takes more time than GPUGoogle Colab TPU 比 GPU 需要更多时间
【发布时间】:2019-04-29 14:43:11
【问题描述】:

下面是我正在使用的代码。我注释掉了将我的模型转换为 TPU 模型的行。使用 GPU 处理相同数量的数据,一个 epoch 需要 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)

这是notebook的链接

【问题讨论】:

  • 你能分享一个重现减速的笔记本吗?
  • @BobSmith 我更新了问题并提供了 colab notebook 的链接
  • 您是否根据cloud.google.com/tpu/docs/tpus#when_to_use_tpus检查了您的脚本是否适合TPU用例?
  • @Jaroslav 我最终使用了 GPU,因为我的数据集并没有那么大。
  • @mjosh 你指的是 TPU 还是 GPU?

标签: tensorflow keras google-colaboratory google-cloud-tpu mlp


【解决方案1】:

您是否尝试过下面示例中的tpu_model.fit_generator 方法? 另一部分看起来不错。 此外,一个问题可能是使用 Adam Optimizer。有什么。关于它,但我忘记了链接在哪里。尝试另一个优化器和下面的代码,如果另一个优化器工作,你知道它一定是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)

【讨论】:

  • 我尝试了 RMSPropOptimizer,但结果仍然相同。
猜你喜欢
  • 2020-02-23
  • 2017-08-30
  • 1970-01-01
  • 2020-10-25
  • 2019-03-05
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 2019-12-06
相关资源
最近更新 更多