python - 在 colab 中使用 keras_to_tpu_model 时,TPU 运行速度与 CPU 一样慢

标签 python tensorflow keras google-colaboratory google-cloud-tpu

我使用 tf.contrib.tpu.keras_to_tpu_model 使我的代码能够在 TPU 上运行,但是完成一个 epoch 需要 170 小时,而 CPU 花费了相同的时间而 GPU 只花费了 40每个 epoch 小时数。我尝试调整批处理大小但没有任何改变。而且我已经测试过输入函数在 GPU 上运行时可能会占用 20% 的运行时间,所以我认为这可能不是主要原因。

这是我的代码:https://github.com/WangHexie/DHNE/blob/master/src/hypergraph_embedding.py

在 colab 上运行:

  1. 热塑性聚氨酯:https://colab.research.google.com/gist/WangHexie/30c385509f9cd93be747f04c39f039a4/tpu-error.ipynb
  2. GPU: https://colab.research.google.com/gist/WangHexie/5bfac53bf92ef0ad527f15ddbf8705e1/-gpu-ipynb.ipynb

模型:

def build_model(self):
    self.inputs = [Input(shape=(self.options.dim_feature[i], ), name='input_{}'.format(i), dtype='float') for i in range(3)]

    self.encodeds = [Dense(self.options.embedding_size[i], activation='tanh', name='encode_{}'.format(i))(self.inputs[i]) for i in range(3)]
    self.decodeds = [Dense(self.options.dim_feature[i], activation='sigmoid', name='decode_{}'.format(i),
                    activity_regularizer = regularizers.l2(0.0))(self.encodeds[i]) for i in range(3)]

    self.merged = concatenate(self.encodeds, axis=1)
    self.hidden_layer = Dense(self.options.hidden_size, activation='tanh', name='full_connected_layer')(self.merged)
    self.ouput_layer = Dense(1, activation='sigmoid', name='classify_layer')(self.hidden_layer)

    self.model = Model(inputs=self.inputs, outputs=self.decodeds+[self.ouput_layer])

    self.model.compile(optimizer=tf.train.AdamOptimizer(learning_rate=self.options.learning_rate),
            loss=[self.sparse_autoencoder_error]*3+['binary_crossentropy'],
                          loss_weights=[self.options.alpha]*3+[1.0],
                          metrics=dict([('decode_{}'.format(i), 'mse') for i in range(3)]+[('classify_layer', 'accuracy')]))
    self.model = tf.contrib.tpu.keras_to_tpu_model(
        self.model,
        strategy=tf.contrib.tpu.TPUDistributionStrategy(
            tf.contrib.cluster_resolver.TPUClusterResolver(
                tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
        )
    )
    self.model.summary()

最佳答案

自 2019-02-20 起,函数 tf.contrib.tpu.keras_to_tpu_model 已被弃用。因此,您应该使用新的 Distribution Strategy 重新尝试转换您的模型。功能。可以找到关于分布式训练的深入指南 here .

我还注意到您使用数据类型 float 作为输入值。在 CPython 中,默认位值为 64 位。目前,TPU 的功能最适合 16 位 float ,因此您应该将输入减少到 8 位或 16 位。位值越低,模型的处理速度就越快。

因此也建议利用量化,将浮点权重转换为 8 位整数。有两种类型的量化训练:post-training quantizationquantization-aware training .

有关 Google Cloud Platform 上 TPU 的更多信息,您可以引用 Cloud TPU documentation ,更多关于TPU系统架构的信息可以引用this Google 的文档,因为它正确解释了 TPU 的设计方式。

关于python - 在 colab 中使用 keras_to_tpu_model 时,TPU 运行速度与 CPU 一样慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53311962/

相关文章:

tensorflow - 如何使用 Keras 打开大型 Parquet 文件?

python - Keras LSTM 的多维输入 -(用于分类)

javascript - 将使用 Tensorflow 库的 Python 代码实现到 HTML 中?

python - 是否可以通过训练小数据子集来验证深度学习模型?

python - 创建具有未知数量属性的类实例

python - 如何在 Python 中使用带有数组的 if 语句?

python-3.x - Dataset api中的多线程

Python 字符串追加

python - 如何将函数或运算符作为参数传递给 Python 中的函数?

python - Stylegan2 Tensorflow 训练在 Google Colab 中在一分钟后中断