值错误:在keras模型子类和渐变带中渐变为无

值错误:在keras模型子类和渐变带中渐变为无,keras,tensorflow2.0,Keras,Tensorflow2.0,代码: 编码器: 输入形状:[(批量大小,1,1),(批量大小,128)] 输出形状:[(批量大小,128),(批量大小,128)] 解码器: 输入形状:[(批量大小,1,1),(批量大小,128)] 输出形状:[(批量大小,1,1),(批量大小,128)] 错误:ValueError:没有为任何变量提供渐变 在自定义训练循环中,渐变为“无” 链接到colab笔记本: 我还尝试了一个带有渐变带的自定义训练循环,但得到了相同的错误 请帮忙!!! 先谢谢你 def encoder_actor(inp

代码:

编码器:

输入形状:[(批量大小,1,1),(批量大小,128)]

输出形状:[(批量大小,128),(批量大小,128)]

解码器:

输入形状:[(批量大小,1,1),(批量大小,128)]

输出形状:[(批量大小,1,1),(批量大小,128)]

错误:ValueError:没有为任何变量提供渐变

在自定义训练循环中,渐变为“无”

链接到colab笔记本:

我还尝试了一个带有渐变带的自定义训练循环,但得到了相同的错误

请帮忙!!! 先谢谢你

def encoder_actor(input):

  start = layers.Input(shape=input)
  state = layers.Input(shape=(128))
  last_h, last_c = layers.GRU(128, return_state=True)(start, initial_state=state)

  return keras.Model(inputs=[start, state], outputs=[last_h, last_c])

def decoder_actor(input):

  start = layers.Input(shape=input)
  state = layers.Input(shape=(128))
  last_h, last_c = layers.GRU(128, return_state=True, kernel_initializer='he_uniform')(start, initial_state=state)
  x = layers.Dense(16, activation="relu")(last_h)
  x = layers.Dense(1)(x)
  last_h = layers.Reshape((1, input[1]))(x)

  return keras.Model(inputs=[start, state], outputs=[last_h, last_c])


encoder = encoder_actor((1, 1))
decoder = decoder_actor((1, 1))
class MyModel(keras.Model):

  def __init__(self, state_size=(20, 1), action_size=(20,), batch_size=100):
    
    super(MyModel, self).__init__()
    self.action_size = action_size
    self.state_size = state_size
    self.batch_size = batch_size

    self.encoder = encoder_actor((1, 1))
    self.decoder = decoder_actor((1, 1))


  def call(self, inputs, training=False):

    encoder_state = tf.zeros((self.batch_size, 128))
    out = tf.Variable(tf.zeros((self.batch_size, self.action_size[0], self.state_size[1]), dtype=tf.float64))

    for i in range(self.action_size[0]):
      encoder_output, encoder_state = self.encoder([inputs[:, i:i+1, :], encoder_state])

    decoder_input = inputs[:, -1, :]
    decoder_state = encoder_state

    for i in range(self.action_size[0]):
      decoder_output, decoder_state = self.decoder([decoder_input, decoder_state])
      decoder_input = decoder_output
      out = out[:, i:i+1, :].assign(decoder_output)

    return tf.reshape(out, [out.shape[0], out.shape[1]])