如何在 Google Colab 中使用 TensorFlow 2.0 将 tf.Keras 模型转换为 TPU?

     2023-03-12     276

关键词:

【中文标题】如何在 Google Colab 中使用 TensorFlow 2.0 将 tf.Keras 模型转换为 TPU?【英文标题】:How to convert tf.Keras model to TPU using TensorFlow 2.0 in Google Colab? 【发布时间】:2019-08-27 17:46:41 【问题描述】:

由于 TF 2.0 没有 tf.contrib 层,我如何转换我的模型以在 TPU 上运行训练,而无需访问 tf.contrib.tpu.keras_to_tpu_model()

我尝试寻找代码,但所有代码都在 TensorFlow 1.x 上运行

我的数据在 .npy,我有一个简单的模型,我只使用 model.compile()model.fit() 来训练它,但看起来模型在 CPU 上运行(需要 30 分钟/epoch与 GPU 上的 2 分钟/纪元相比)。

【问题讨论】:

【参考方案1】:

使用 TensorFlow 2.2 的 Colab(2020 年 3 月更新)

在我修复了这个issue 后它可以工作了,here 上还有一个 Colab 笔记本

使用 TensorFlow 2.0 将 Keras 模型转换为 TPU(2019 年 11 月更新)

使用 TensorFlow 2.0 将 Keras 模型与 Google Cloud TPU 一起使用非常容易,不再需要“转换”。

我们需要做的只是指定一个Distributed Strategy 让 TensorFlow 为我们完成所有繁重的工作。

def create_model():
    model = tf.keras.models.Sequential()

    model.add(tf.keras.layers.Conv2D(128, (3, 3), input_shape=x_train.shape[1:]))
    model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2,2)))
    model.add(tf.keras.layers.Activation('elu'))

    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(10))
    model.add(tf.keras.layers.Activation('softmax'))

    return model

resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
tf.config.experimental_connect_to_host(resolver.master())
tf.tpu.experimental.initialize_tpu_system(resolver)
strategy = tf.distribute.experimental.TPUStrategy(resolver)

with strategy.scope():
    model = create_model()
    model.compile(
        optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
        loss=tf.keras.losses.sparse_categorical_crossentropy,
        metrics=[tf.keras.metrics.sparse_categorical_accuracy])

我们创建一个分布式解析器,然后为解析器创建策略,然后使用strategy.scope(),我们创建我们的 Keras 模型,然后我们就完成了。

通过我的 Colab 笔记本 https://colab.research.google.com/github/huan/tensorflow-handbook-tpu/blob/master/tensorflow-handbook-tpu-example.ipynb 了解有关如何使用 TPU 创建 Keras 模型的更多信息。

TensorFlow 分布式训练官方文档:https://www.tensorflow.org/guide/distributed_training

但是,请注意 Colab 存在一些需要修复的环境问题,因此它可能还无法在 Colab 中运行。

Colab TPU 尚未准备好 2.0(还)

TensorFlow 2.0 已经发布,但支持 TPU 的 Colab 尚不支持。有谷歌人说2.1发布后就可以了。

我们在跟踪此进度时遇到了问题; https://github.com/huan/tensorflow-handbook-tpu/issues/1#issuecomment-552807573

我的旧答案

Googler Wolff 证实,我们还不能在 Colab 中使用 TPU 中的 TF 2.0(报告于 2019 年 4 月 15 日):

您将通过 Colab 分配的 TPU 正在运行 TF 1.x。在 Jupyter VM 上安装 nightly 2.0 pip 时,它不会更改 TPU。您最终会发现 Jupyter 实例上运行的内容与 TPU 所拥有的内容不匹配。

根据https://github.com/tensorflow/tensorflow/issues/24412,TPU 对 TensorFlow 2.0 的支持还不完整。

解决方法是监控上述问题,等待 TF 2.0 发布。

【讨论】:

如何在 google colab 中使用 ngrok?

】如何在googlecolab中使用ngrok?【英文标题】:HowcanIworkwithngrokingooglecolab?【发布时间】:2021-12-1503:08:56【问题描述】:我在GoogleColab中将这个解决方法与ngrok一起使用。我的目标是将一些数据帧隧道传输到公共url,以便我可以与同... 查看详情

如何在 Google Colab 中使用 Pyomo 解决抽象模型

】如何在GoogleColab中使用Pyomo解决抽象模型【英文标题】:HowtosolveanabstractmodelusingPyomoinGoogleColab【发布时间】:2022-01-2102:07:21【问题描述】:我正在学习如何在GoogleColab中使用Pyomo,并创建了一个抽象模型,但我不知道读取数据文... 查看详情

如何在 Google 的 Colab 中安装 Python 包?

】如何在Google的Colab中安装Python包?【英文标题】:HowdoIinstallPythonpackagesinGoogle\'sColab?【发布时间】:2018-12-2220:31:29【问题描述】:在一个项目中,我有例如两个不同的包,在谷歌的Colab中如何使用setup.py安装这两个包,以便导入... 查看详情

如何在 Google Colab 中高效使用 GPU RAM?

】如何在GoogleColab中高效使用GPURAM?【英文标题】:HowtoUseGPURAMinGoogleColabefficiently?【发布时间】:2020-04-1418:57:41【问题描述】:我正在设计一个multi_label_image_Classifier。为此,我需要加载大约7867个的train_images。在我加载图像时,RA... 查看详情

运行 R 内核时如何在 google Colab 中访问 shell

】运行R内核时如何在googleColab中访问shell【英文标题】:HowtoaccesstheshellingoogleColabwhenrunningtheRkernel【发布时间】:2021-12-2915:44:48【问题描述】:当我将Python与colab一起使用时,您可以使用!和%%shell之类的东西访问底层操作系统,例... 查看详情

如何使用 google colab 在 jupyter notebook 中显示 GIF?

】如何使用googlecolab在jupyternotebook中显示GIF?【英文标题】:HowtodisplayaGIFinjupyternotebookusinggooglecolab?【发布时间】:2020-07-2110:12:47【问题描述】:我正在使用googlecolab并想嵌入一个gif。有谁知道如何做到这一点?我正在使用下面的... 查看详情

如何在 Google Colab 中使用 matplotlib 绘制交互式可绘制图像?

】如何在GoogleColab中使用matplotlib绘制交互式可绘制图像?【英文标题】:Howtoplotinteractivedraw-ableimageusingmatplotlibinGoogleColab?【发布时间】:2019-08-1421:17:46【问题描述】:我正在尝试绘制一个交互式图像,它可以让我在GoogleColabNoteboo... 查看详情

如何在 Google Colab 中编辑和保存文本文件 (.py)?

】如何在GoogleColab中编辑和保存文本文件(.py)?【英文标题】:Howtoeditandsavetextfiles(.py)inGoogleColab?【发布时间】:2018-07-1902:49:32【问题描述】:我使用!gitclonehttps://github.com/llSourcell/Pokemon_GAN.git克隆了一个github存储库。我想在Colab中... 查看详情

如何检查我在 Google Colab 中使用的软件包版本?

】如何检查我在GoogleColab中使用的软件包版本?【英文标题】:HowdoIcheckwhichversionsofpackagesI\'musinginGoogleColab?【发布时间】:2021-10-0606:19:07【问题描述】:有些是预装在Colab中的,有些是在Colab单元中使用!pipinstallmodule_name安装的。... 查看详情

如何在 Google Colab 中获得分配的 GPU 规格

】如何在GoogleColab中获得分配的GPU规格【英文标题】:HowtogetallocatedGPUspecinGoogleColab【发布时间】:2020-06-0314:24:00【问题描述】:我正在使用GoogleColab进行深度学习,我知道他们会随机将GPU分配给用户。我希望能够查看在任何给定... 查看详情

如何在 google colab 中动态(循环)显示图像?

】如何在googlecolab中动态(循环)显示图像?【英文标题】:Howcanidynamically(inaloop)showimagesingooglecolab?【发布时间】:2019-03-2204:52:19【问题描述】:我一直在尝试使用pyplot/matplotlib来显示图像,因为它们在循环中发生变化,但我无... 查看详情

如何在 Google colab 中更改 Keras/tensorflow 版本?

】如何在Googlecolab中更改Keras/tensorflow版本?【英文标题】:HowtochangeKeras/tensorflowversioninGooglecolab?【发布时间】:2019-01-2913:19:52【问题描述】:我在googlecolaboratory上使用keras/tensorflow,我需要回到以前的版本。问题是当我运行!pipinst... 查看详情

如何使用python google colab将带有边界框的图像存储在文件夹中?

】如何使用pythongooglecolab将带有边界框的图像存储在文件夹中?【英文标题】:Howtostoreimageswithboundingboxinafolderusingpythongooglecolab?【发布时间】:2020-12-3014:21:21【问题描述】:我正在尝试在检测到图像后保存图像。我尝试使用matplot... 查看详情

如何在 google colab 上升级 pandas

】如何在googlecolab上升级pandas【英文标题】:Howtoupgradepandasongooglecolab【发布时间】:2019-04-0417:54:23【问题描述】:据我了解,Pandas在GoogleColab环境中已预先安装。但是它使用的版本不是最近的。importpandasaspdpd.__version__>>0.22.0... 查看详情

如何在 Google Colab 中显示 Vega 可视化

】如何在GoogleColab中显示Vega可视化【英文标题】:HowtoshowVegavisualizationsinGoogleColab【发布时间】:2020-08-3105:07:06【问题描述】:我可以使用Altair在GoogleColab中显示Vega-Lite可视化。但是有没有办法显示简单的Vega可视化?我在GoogleColab... 查看详情

如何在 Google Colab 中查找 TPU 名称和区域?

】如何在GoogleColab中查找TPU名称和区域?【英文标题】:HowtofindTPUnameandzoneinGoogleColab?【发布时间】:2021-07-0405:03:05【问题描述】:我正在尝试在配置中运行需要tpu名称和区域的代码。使用TPU运行时,如何在GoogleColab中找到TPU名称... 查看详情

如何使用不同的配对语言在 Google Colab 中运行带注意力的神经机器翻译?

】如何使用不同的配对语言在GoogleColab中运行带注意力的神经机器翻译?【英文标题】:HowcanIrunNeuralMachineTranslationwithAttentioninGoogleColabwithadifferentpairedlanguage?【发布时间】:2019-05-2713:13:58【问题描述】:我想在TernsorFlow网站提供的... 查看详情

如何在 Google Colab 交互式中制作 matplotlib 图

】如何在GoogleColab交互式中制作matplotlib图【英文标题】:HowcanImakeamatplotlibplotinGoogleColabinteractive【发布时间】:2021-01-1822:30:09【问题描述】:我正在使用python和matplotlib,我正在尝试在GoogleColab中创建交互式绘图。我可以制作情节... 查看详情