python - 使用 tf.eager 训练复杂的 nn 模型(使用 TF2 符号支持效果更好)

标签 python tensorflow tensorflow2.0 eager

是否有(或多或少)简单的方法来编写复杂的神经网络模型,以便可以在 eager 模式下进行训练?有这样的代码示例吗?

例如,我想使用InceptionResnetV2。我使用 tf.contrib.slim 创建了代码。根据此链接,https://github.com/tensorflow/tensorflow/issues/16182 , slim 已被弃用,我需要使用 Keras。而且我真的无法使用精简代码进行 eager 训练,因为我无法获取变量列表并应用梯度(好吧,我可以尝试将模型包装到 GradientTape 中,但不确定什么与正则化损失有关)。

好吧,让我们尝试一下 Keras

In [30]: tf.__version__                                                                                                                                                                          
Out[30]: '1.13.1'

In [31]: tf.enable_eager_execution()

In [32]: from keras.applications.inception_resnet_v2 import InceptionResNetV2

In [33]: model = InceptionResNetV2(weights=None)
...
/usr/local/lib/python3.6/dist-packages/keras_applications/inception_resnet_v2.py in InceptionResNetV2(include_top, weights, input_tensor, input_shape, pooling, classes, **kwargs)
    246 
    247     if input_tensor is None:
--> 248         img_input = layers.Input(shape=input_shape)
    249     else:
    250         if not backend.is_keras_tensor(input_tensor):
...
RuntimeError: tf.placeholder() is not compatible with eager execution.

默认情况下不起作用。

在本教程中,他们说我需要创建自己的模型类并自己维护变量 https://www.tensorflow.org/tutorials/eager/custom_training#define_the_model 。我不确定我是否想为《盗梦空间》做这件事。需要创建和维护的变量太多。这就像回到旧版本的 TF,在那些日子里,甚至 slim 都不存在。

在本教程中,网络是使用 Keras 创建的 https://www.tensorflow.org/tutorials/eager/custom_training_walkthrough#create_a_model_using_keras但我怀疑我是否可以通过仅定义模型而不将其与 Input 一起使用来轻松地以这种方式维护复杂的结构。例如,在本文中,如果我理解正确的话,作者会初始化 keras Input 并通过模型传播它(当与 Eager 一起使用时,这会导致 RuntimeError,如您之前所见) 。我可以通过对模型类进行子类化来制作自己的模型,如下所示:https://www.tensorflow.org/api_docs/python/tf/keras/Model 。哎呀,这样我需要维护图层,而不是变量。对我来说这几乎是同样的问题。

此处有趣地提到了 AutoGrad https://www.tensorflow.org/beta/guide/autograph#keras_and_autograph 。它们只覆盖 __call__,所以在这种情况下我似乎不需要维护变量,但我还没有测试它。


那么,有什么简单的解决办法吗?

GradientTape包裹纤细的模型?然后我如何将reg损失应用于权重?

我自己跟踪每个变量?听起来有点痛。

使用Keras?当模型中有分支且结构复杂时,如何使用 eager 来使用它?

最佳答案

您的第一种方法可能是最常见的。这个错误:

RuntimeError: tf.placeholder() is not compatible with eager execution.

是因为不能在急切模式下使用tf.placeholder。急切执行的时候是没有这样的概念的。

您可以使用tf.data API为您的训练数据构建数据集并将其提供给模型。像这样的数据集替换为您的真实数据:

import tensorflow as tf
tf.enable_eager_execution()

model = tf.keras.applications.inception_resnet_v2.InceptionResNetV2(weights=None)

model.compile(tf.keras.optimizers.Adam(), loss=tf.keras.losses.categorical_crossentropy)

### Replace with tf.data.Datasets for your actual training data!
train_x = tf.data.Dataset.from_tensor_slices(tf.random.normal((10,299,299,3)))
train_y = tf.data.Dataset.from_tensor_slices(tf.random.uniform((10,), maxval=10, dtype=tf.int32))
training_data = tf.data.Dataset.zip((train_x, train_y)).batch(BATCH_SIZE)

model.fit(training_data)

正如您的标题中提到的,这种方法在 TensorFlow 2.0 中也同样有效。

关于python - 使用 tf.eager 训练复杂的 nn 模型(使用 TF2 符号支持效果更好),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57565019/

相关文章:

tensorflow - Tensorflow 2.0 中的二阶导数

python - Django 无法删除/清除表单上的数据

Python 在 "IN"循环中使用 "IF ELSE"

android - 在使用 tensorflow 模型时,我们可以使用 .pbtxt 而不是 .pb 文件吗

python - tensorflow 中二维数组最小值到最大值的排序

python - TensorFlow:模块 '__main__' 没有属性 'main'

Tensorflow2.0 MultiWorkerMirroredStrategy 示例挂起

python - 为什么 tf.executing_eagerly() 在 TensorFlow 2 中返回 False?

python - python程序显示编译错误

python - opencv cv2.imshow自动向小图像添加填充