【问题标题】:No gradients provided for any variable: ['generated_image:0']没有为任何变量提供渐变:['generated_image:0']
【发布时间】:2021-08-03 10:58:36
【问题描述】:

我从事神经风格迁移项目。在我训练模型的最后一步时,它显示错误

我正在添加两个图像,一个是内容图像,另一个是样式图像,以生成具有样式图像样式的内容图像的新图像

没有为任何变量提供渐变:['generated_image:0']。

optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
@tf.function()
def train_step(generated_image):
  with tf.GradientTape() as tape:
    a_G = vgg_model_outputs(generated_image)
    J_style = style_cost(a_S, a_G)
    J_content = content_cost(a_C, a_G)
    J = total_cost(J_content, J_style, alpha = 10, beta = 40)

  grad = tape.gradient(J, generated_image)
  optimizer.apply_gradients([(grad, generated_image)])
  generated_image.assign(tf.clip_by_value(generated_image,clip_value_min=0.0, clip_value_max=1.0))

  return J
epochs = 5000
for i in range(epochs):
    train_step(generated_image)
    if i % 250 == 0:
        print(f"Epoch {i} ")
    if i % 250 == 0:
        image = tensor_to_image(generated_image)
        imshow(image)
        image.save(f"output/image_{i}.jpg")
        plt.show() 

完整的堆栈跟踪

ValueError                                Traceback (most recent call last)
<ipython-input-23-3a761c39189b> in <module>()
      1 epochs = 5000
      2 for i in range(epochs):
----> 3     train_step(generated_image)
      4     if i % 250 == 0:
      5         print(f"Epoch {i} ")

8 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
    887 
    888       with OptionalXlaContext(self._jit_compile):
--> 889         result = self._call(*args, **kwds)
    890 
    891       new_tracing_count = self.experimental_get_tracing_count()

/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
    931       # This is the first call of __call__, so we have to initialize.
    932       initializers = []
--> 933       self._initialize(args, kwds, add_initializers_to=initializers)
    934     finally:
    935       # At this point we know that the initialization is complete (or less

/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
    762     self._concrete_stateful_fn = (
    763         self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
--> 764             *args, **kwds))
    765 
    766     def invalid_creator_scope(*unused_args, **unused_kwds):

/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
   3048       args, kwargs = None, None
   3049     with self._lock:
-> 3050       graph_function, _ = self._maybe_define_function(args, kwargs)
   3051     return graph_function
   3052 

/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
   3442 
   3443           self._function_cache.missed.add(call_context_key)
-> 3444           graph_function = self._create_graph_function(args, kwargs)
   3445           self._function_cache.primary[cache_key] = graph_function
   3446 

/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
   3287             arg_names=arg_names,
   3288             override_flat_arg_shapes=override_flat_arg_shapes,
-> 3289             capture_by_value=self._capture_by_value),
   3290         self._function_attributes,
   3291         function_spec=self.function_spec,

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
    997         _, original_func = tf_decorator.unwrap(python_func)
    998 
--> 999       func_outputs = python_func(*func_args, **func_kwargs)
   1000 
   1001       # invariant: `func_outputs` contains only Tensors, CompositeTensors,

/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
    670         # the function a weak reference to itself to avoid a reference cycle.
    671         with OptionalXlaContext(compile_with_xla):
--> 672           out = weak_wrapped_fn().__wrapped__(*args, **kwds)
    673         return out
    674 

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    984           except Exception as e:  # pylint:disable=broad-except
    985             if hasattr(e, "ag_error_metadata"):
--> 986               raise e.ag_error_metadata.to_exception(e)
    987             else:
    988               raise

ValueError: in user code:

    <ipython-input-22-71263e1538e0>:11 train_step  *
        optimizer.apply_gradients([(grad, generated_image)])
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/optimizer_v2/optimizer_v2.py:630 apply_gradients  **
        grads_and_vars = optimizer_utils.filter_empty_gradients(grads_and_vars)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/optimizer_v2/utils.py:76 filter_empty_gradients
        ([v.name for _, v in grads_and_vars],))

    ValueError: No gradients provided for any variable: ['generated_image:0'].

一般图片:

generated_image = tf.Variable(tf.image.convert_image_dtype(content_image, tf.float32))
noise = tf.random.uniform(tf.shape(generated_image), 0, 0.5)
generated_image = tf.add(generated_image, noise)
generated_image = tf.clip_by_value(generated_image, clip_value_min=0.0, clip_value_max=1.0)

不知道怎么回事?请帮忙!

【问题讨论】:

  • 请在代码 sn-p 中分享完整的堆栈跟踪,而不是在引号中
  • 我添加了完整的堆栈跟踪
  • 看起来像generated_image 格式不正确的问题。你能展示生成它的代码吗?
  • 添加通用图片生成代码

标签: python tensorflow deep-learning conv-neural-network


【解决方案1】:

我假设您正在尝试在您自己的计算机上运行此 Coursera course 中的代码,因为在尝试执行此操作后我遇到了完全相同的问题。 问题是当您将 generated_image 传递给 train_step 时,它的类型错误。如果您检查type(generated_image),它很可能是

&lt;class 'tensorflow.python.framework.ops.EagerTensor'&gt;,

应该是这样的:

&lt;class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'&gt;.

您可以通过添加以下行轻松解决此问题 generated_image = tf.Variable(generated_image) 这行代码最初是在一个测试单元中,你可能在清理代码时错误地删除了它(这正是我所做的)。最好的地方是在您发布的最后一行 generate_image 之后添加这一行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 2020-09-01
    相关资源
    最近更新 更多