加载模型,使用 Tensorflow 2.0 保存,使用 Tensorflow 1.x [英] Loading a model, saved using Tensorflow 2.0, using Tensorflow 1.x

查看:72
本文介绍了加载模型,使用 Tensorflow 2.0 保存,使用 Tensorflow 1.x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 Huggingface 使用 transformers 库.

I am using the transformers library by huggingface.

他们在 Tensorflow 2.0 中为许多架构提供了预训练模型.我想使用这些模型对 TPU 进行预测.由于 Tensorflow 2.0 的 TPU API 还在实验阶段,我想在 Tensorflow 1.x 中做

They have provided pre-trained models in Tensorflow 2.0 for a lot of architectures. I want to use these models for predictions on a TPU. Since the TPU API for Tensorflow 2.0 is still experimental, I want to do it in Tensorflow 1.x

有没有办法将预先训练的模型从 Tensorflow 2.0 转换为 Tensorflow 1.x,反之亦然或在框架之间进行转换?

Is there a way for me to convert a pre-trained model from Tensorflow 2.0 to Tensorflow 1.x, like it is possible to do vice versa or between frameworks?

Tensorflow 2.0 的 TPUE API 描述如下:https://www.tensorflow.org/guide/distributed_training#tpustrategy 并且仍处于实验阶段,并且尚未共享适当的教程.甚至本文中共享的代码存根在 Colab 上对我也不起作用,因此我想出了将模型转换为 Tensorflow 1.x 的想法

The TPUE API for Tensorflow 2.0 is described here: https://www.tensorflow.org/guide/distributed_training#tpustrategy and is still experimental and a proper tutorial for it has not been shared yet. Even the stub of code shared that is shared in this article was not working for me on Colab, hence I came up with the idea of converting the model to Tensorflow 1.x

推荐答案

虽然我不能保证这是运行转换的最简单方法,但我遇到了需要将 tensorflow 变量保存和恢复为原始 numpy 数组的情况,这绝对可以帮助您完成转换.

Though I can't guarantee this is the easiest way to run the conversion, I have run into the need to save and restore tensorflow variables as raw numpy arrays, and this could definitely help you with your conversion.

在 Tensorflow 2 一侧,创建一个包含 Tensorflow 变量和这些变量在 Tensorflow 1 图中的名称的字典,然后使用 variable.value() 将变量名称与它们保存的值创建一个字典.然后,您可能需要将名称-值字典序列化为一个文件,除非您可以设法将两个版本的 tensorflow 同时加载到同一个程序中.

On the Tensorflow 2 side, make a dictionary of the Tensorflow variables and the names of those variables in the Tensorflow 1 graph, then make a dictionary with the names of the variables to the value they hold using variable.value(). You'll then probably need to serialize the name-value dictionary to a file, unless you can manage to load both versions of tensorflow simultaneously into the same program.

在 Tensorflow 1 端,使用 tf.trainable_variables() 获取要分配的变量列表.使用 variable.name 获取变量的名称,使用保存的字典获取该变量的值,然后使用 session.run(variable.assign(value)) 将训练好的值填充到参数中.

On the Tensorflow 1 side, use tf.trainable_variables() to fetch the list of variables to assign. Get the name of the variable with variable.name, use the saved dictionary to fetch the value for that variable, and then use session.run(variable.assign(value)) to populate the parameters with the trained values.

诚然,这有点麻烦,而且它的性能可能不是很高,但至少实现起来应该不会超过两打代码!

A little bit cumbersome, admittedly, and it might not be hugely performant, but it shouldn't take more than two dozen lines of code to implement, at least!

这篇关于加载模型,使用 Tensorflow 2.0 保存,使用 Tensorflow 1.x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆