你怎么能在谷歌TPU中使用Tensor2.0?

2024-06-06 22:16:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图利用googlecolab来使用张量处理单元(TPU)来训练神经网络。Tensorflow刚刚发布了一个主要版本,2.0,所以我尝试在Tensorflow 2.0中实现这一点。我试过以下三个指南,但都是为Tensorflow 1.14编写的-但在Tensorflow 2.0中失败了:

1)按照指南TPUs in Colab,我得到错误:

AttributeError: module 'tensorflow' has no attribute 'Session'

(参考文献:,其中tf.会议(tpu地址)as会话:

2)按照指南Simple Classification Model using Keras on Colab TPU,我得到了同样的错误

3)按照指南cloud_tpu_custom_training,我得到错误:

^{pr2}$

(参考文献:解析器=tf.contrib.cluster_解析器.TPUClusterResolver(tpu=tpu工作线程)

有没有人举过用TPU在TensorFlow2.0中训练神经网络的例子?在

编辑:这个问题似乎也出现在github上:InvalidArgumentError: Unable to find a context_id matching the specified one #1


Tags: 版本解析器利用tftensorflow错误指南神经网络
3条回答

在运行代码之前

转到

Edit  > Notebook Settings

在那个选择下

^{pr2}$

Tensorflow 2.0并不真正向后兼容TensorFlow1.X代码。Tensorflow在这些版本之间的工作方式有很多变化,因此我强烈建议您阅读有关如何迁移代码的官方指南:

https://www.tensorflow.org/guide/migrate#estimators

我会说,自动转换脚本虽然在技术上很成功,但只将我的代码更改为Tensorflow 1.X代码的兼容版本—如果您想使用任何实际的Tensorflow 2.0功能,您可能需要手动更改代码。在

首先,教程中给出的代码与2.x不兼容

  1. 您需要在colab中选择runtime作为TPU来执行TPU中的代码
  2. 因为错误

    AttributeError: module 'tensorflow' has no attribute 'Session'

    您需要使用tf.compat.v1.Session(),因为tf.session已弃用。

  3. 请使用tf.contrib.cluster_resolver代替tf.distribute.cluster_resolver

请参考TensorflowAddon-repo将代码从1.x转换为2.x兼容。在

相关问题 更多 >