python - 在 Google Colab 中使用 TPU
问题描述
我目前正在 TPU 的帮助下训练神经网络。我更改了运行时类型并初始化了 TPU。我感觉它仍然没有更快。我使用了 https://www.tensorflow.org/guide/tpu。我有什么问题吗?
# TPU initialization
resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
tf.config.experimental_connect_to_cluster(resolver)
# This is the TPU initialization code that has to be at the beginning.
tf.tpu.experimental.initialize_tpu_system(resolver)
print("All devices: ", tf.config.list_logical_devices('TPU'))
.
.
.
# experimental_steps_per_execution = 50
model.compile(optimizer=Adam(lr=learning_rate), loss='binary_crossentropy', metrics=['accuracy'], experimental_steps_per_execution = 50)
我的模型总结
我还有什么需要考虑或调整的吗?
解决方案
您需要创建 TPU 策略:
strategy = tf.distribute.TPUStrategy(resolver).
而不是正确使用此策略:
with strategy.scope():
model = create_model()
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['sparse_categorical_accuracy'])
推荐阅读
- python - 在条件列表推导中使用 or 语句来过滤数据框中的列
- ionic-framework - Ionic Creator 导出,如何制作数据库和服务器?
- java - How to get lines starting with some word from file and if no match get lines starting with another word
- ruby-on-rails - 如何配置项目以使用多个条带 webhook
- google-docs-api - 如何使用 ruby api 从谷歌文档中获取所有文本?
- r - ggplot-如何绘制独立图例?如何仅传说某些事物?
- excel - 在 excel 中创建输入掩码以在 VBA 中为代码插入新代码/新参数
- json - 如何使用 Python 从一个 JSON 文件中查找数据并将其替换为另一个文件?
- javascript - 如何使 v-tooltip 具有交互性?
- ruby - 如何递归地将嵌套哈希展平为具有特定格式的数组数组?