ValueError:操作的梯度为“无”。没有自定义图层或丢失功能

时间:2019-12-01 03:53:47

标签: python-3.x tensorflow keras

我遇到的问题与this post中的错误相同,但就我而言,我没有在Keras中创建新层或更改内部任何内容,这是在训练AAE体系结构时发生的。

基本上,我有一个编码器,它输出标签和潜在向量,还有一个生成器,它同时接收两个参数并重新创建图像。

由于我需要在训练过程的不同步骤中分别训练每个模型,因此我有第三个模型声明了AAE:

def define_autoencoder(encoder, generator):


    reconstructed = generator(encoder.get_output_at(0))

    aae = Model(encoder.get_input_at(0), reconstructed)
    opt = Adam(lr=0.0001, beta_1=0.5)
    aae.compile(loss=['mean_squared_error'], optimizer=opt)
    return aae

我可以像这样同时运行编码器和生成器:

encoder.train_on_batch(inputs, outputs)

但是一旦我致电aae.train_on_batch(X_real,X_real),就会抛出以下错误:

    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 1216, in train_on_batch
    self._make_train_function()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 509, in _make_train_function
    loss=self.total_loss)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/optimizers.py", line 475, in get_updates
    grads = self.get_gradients(loss, params)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/optimizers.py", line 91, in get_gradients
    raise ValueError('An operation has `None` for gradient. '
ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.

如有必要,我可以在此处发布两个模型的实现

0 个答案:

没有答案