gpt4 book ai didi

python - 值错误: No variables to optimize

转载 作者:行者123 更新时间:2023-11-30 09:08:29 25 4
gpt4 key购买 nike

我正在尝试计算两个图像之间的l2_loss并获取它们的梯度。这里给出了我的代码片段:

with tf.name_scope("train"):

X = tf.placeholder(tf.float32, [1, None, None, None], name='X')
y = tf.placeholder(tf.float32, [1, None, None, None], name='y')
Z = tf.nn.l2_loss(X - y, name="loss")
step_loss = tf.reduce_mean(Z)
optimizer = tf.train.AdamOptimizer()
training_op = optimizer.minimize(step_loss)

init = tf.global_variables_initializer()

with tf.Session() as sess:
init.run()
content = tf.gfile.FastGFile('cat.0.jpg', 'rb').read()
noise = tf.gfile.FastGFile('color_img.jpg', 'rb').read()
loss_append = []
for epoch in range(10):
for layer in layers:
c = sess.run(layer, feed_dict={input_img: content})
n = sess.run(layer, feed_dict={input_img: noise})
sess.run(training_op, feed_dict={X: c, y: n})

但它给出了以下错误:

    Traceback (most recent call last):
File "/home/noise_image.py", line 68, in <module>
training_op = optimizer.minimize(lossss)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training /optimizer.py", line 315, in minimize
grad_loss=grad_loss)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training /optimizer.py", line 380, in compute_gradients
raise ValueError("No variables to optimize.")
ValueError: No variables to optimize.

如何摆脱它?

最佳答案

Xy 具有从 feed_dict 馈送的值,而 Z 是这些值的函数,因此TensorFlow 无法训练它们。

不要将X设置为占位符,而是将其分配给其张量值(layer)。对 y 执行相同的操作。

您的最终代码应类似于:

for epoch in range(10):
sess.run(training_op, feed_dict={input_image_content: content, input_image_noise: noise})

关于python - 值错误: No variables to optimize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46246556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com