为了账号安全,请及时绑定邮箱和手机立即绑定

使用 TensorFlow 在 ResNetv2 101 上没有梯度

使用 TensorFlow 在 ResNetv2 101 上没有梯度

千巷猫影 2023-06-20 17:20:58
使用 TensorFlow 2.3,在 Ubuntu 18.04 上,在 Python 中,我想训练一个残差网络来生成热图。我是TF的新手。到目前为止,我可以解决所有问题,直到出现No gradients provided for any variable异常。我能够使用以下基本代码重现异常。import tensorflow as tfdef 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 tfdef 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)我仍然不知道为什么原始代码会生成无梯度异常(因此我不认为此编辑是答案)。
查看完整描述

1 回答

?
千万里不及你

TA贡献1784条经验 获得超9个赞

我试图通过仅更改 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)


查看完整回答
反对 回复 2023-06-20
  • 1 回答
  • 0 关注
  • 101 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信