使用 TensorFlow 在 ResNetv2 101 上没有梯度

No gradient on ResNetv2 101 with TensorFlow

使用 TensorFlow 2.3,在 Ubuntu 18.04,在 Python,我想训练一个残差网络来生成热图。我是TF的新手。到目前为止,我可以解决所有问题,直到出现异常 No gradients provided for any variable。 我能够使用以下基本代码重现异常。

import tensorflow as tf

def dummy_image_float(w,h):
    return tf.constant([0.,]*(h*w*3), shape=[1,w,h,3], dtype=tf.float32)
def dummy_result(w,h,nfeature):
    return tf.constant([0,]*(h*w*nfeature), shape=[1,w,h,nfeature], dtype=tf.float32)

model = tf.keras.applications.ResNet101V2(
        include_top=False,
        #input_tensor=x1,
        weights='imagenet',
        input_shape=(224, 224, 3),
        pooling=None
        )

model.compile(optimizer='adam', loss="mean_squared_error", run_eagerly=True)

train_ds = [ (dummy_image_float(224,224), dummy_result(7,7,2048)) ]
model.fit(train_ds, epochs=1)

此代码以“ValueError:没有为任何变量提供梯度”结束。

我可以在网上找到相关的错误。我想我可以使用 tf.GradientTape 编写自己的学习循环,但我希望上面的代码能够在不需要自定义学习循环的情况下工作。有谁知道为什么上面的代码失败了?


经过一番挖掘,这就是我想出的。

import tensorflow as tf

def dummy_image_float(w,h):
    return tf.constant([0.,]*(h*w*3), shape=[1,w,h,3], dtype=tf.float32)
def dummy_result(w,h,nfeature):
    return tf.constant([0,]*(h*w*nfeature), shape=[1,w,h,nfeature], dtype=tf.float32)

model = tf.keras.applications.ResNet101V2(
        include_top=False,
        weights='imagenet',
        input_shape=(224, 224, 3),
        pooling=None
        )

train_ds = [ (dummy_image_float(224,224), dummy_result(7,7,2048)) ]
opt = tf.keras.optimizers.Adam()
loss_fn = lambda: tf.keras.losses.mse(model(input), output)
for input, output in train_ds:
  opt.minimize(loss_fn, model.trainable_weights)

我仍然不知道为什么原始代码会生成 无梯度 异常(因此我不认为此编辑是答案)。

我找到了basic training loops not helpful for my specific problem. I followed this example on the Optimizer doc page

我试图通过仅更改 model.fit() 操作中的最后一部分代码来复制和解析您在 TF 2.6 版本中的代码。 这是编辑后的代码:

import tensorflow as tf

def dummy_image_float(w,h):
    return tf.constant([0.,]*(h*w*3), shape=[1,w,h,3], dtype=tf.float32)
def dummy_result(w,h,nfeature):
    return tf.constant([0,]*(h*w*nfeature), shape=[1,w,h,nfeature], dtype=tf.float32)

model = tf.keras.applications.ResNet101V2(
        include_top=False,
        #input_tensor=x1,
        weights='imagenet',
        input_shape=(224, 224, 3),
        pooling=None
        )

model.compile(optimizer='adam', loss="mean_squared_error", run_eagerly=True)

#train_ds = [ (dummy_image_float(224,224), dummy_result(7,7,2048)) ]
model.fit(dummy_image_float(224,224), dummy_result(7,7,2048), epochs=2)

参考: