Tensorflow没有为任何可变LSTM编码器解码器提供梯度
我是Tensorflow的新手,我正在尝试使用Tensorflow从头开始实现LSTM编码器-解码器,根据以下博文: 这是编码器的代码(使用tf.while\u循环) 这是解码器的代码:Tensorflow没有为任何可变LSTM编码器解码器提供梯度,tensorflow,lstm,recurrent-neural-network,Tensorflow,Lstm,Recurrent Neural Network,我是Tensorflow的新手,我正在尝试使用Tensorflow从头开始实现LSTM编码器-解码器,根据以下博文: 这是编码器的代码(使用tf.while\u循环) 这是解码器的代码: def decode_timestep(self, context, max_dec, result, C_t_d, H_t_d, current_index): # Same as above # Calculate new H_t H_t_d_new = tf.multiply
def decode_timestep(self, context, max_dec, result, C_t_d, H_t_d, current_index):
# Same as above
# Calculate new H_t
H_t_d_new = tf.multiply(o_t_d, tf.nn.tanh(C_t_d))
# Write the result of this timestep to the tensor array at pos current_index
result.write(tf.subtract(tf.subtract(max_dec, 1), current_index), H_t_d_new)
# Decrement the current_index by 1
index_next = tf.subtract(current_index, 1)
return context, max_dec, result, C_t_d_i, H_t_d_new, index_next
这是会议的代码:
with tf.variable_scope('encode_scope', reuse=True):
H_t_e = tf.get_variable('H_t_e')
C_t_e = tf.get_variable('C_t_e')
with tf.variable_scope('global_scope', reuse=True):
current_index = tf.get_variable('current_index', dtype=tf.int32)
context, _, C_t_e, H_t_e, current_index = tf.while_loop(self.encode_timestep_cond, self.encode_timestep,
[X_Sent, max_enc, C_t_e, H_t_e, current_index])
result = tf.TensorArray(tf.float32, size=max_dec)
_,_,result, C_t_e, H_t_e, current_index = tf.while_loop(self.decode_timestep_cond, self.decode_timestep,
[context, max_dec, result, C_t_e, H_t_e, current_index])
loss = tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(result.concat(), Y_Sent)), reduction_indices=1)))
train_step = tf.train.AdamOptimizer().minimize(loss)
错误是:
ValueError:没有为任何变量提供渐变,请检查图形
对于不支持渐变的操作,在变量之间
[“”
请帮忙!这是我认为应该在我从那篇文章中读到并理解之后立即实现的。谢谢你,我的英语很抱歉
with tf.variable_scope('encode_scope', reuse=True):
H_t_e = tf.get_variable('H_t_e')
C_t_e = tf.get_variable('C_t_e')
with tf.variable_scope('global_scope', reuse=True):
current_index = tf.get_variable('current_index', dtype=tf.int32)
context, _, C_t_e, H_t_e, current_index = tf.while_loop(self.encode_timestep_cond, self.encode_timestep,
[X_Sent, max_enc, C_t_e, H_t_e, current_index])
result = tf.TensorArray(tf.float32, size=max_dec)
_,_,result, C_t_e, H_t_e, current_index = tf.while_loop(self.decode_timestep_cond, self.decode_timestep,
[context, max_dec, result, C_t_e, H_t_e, current_index])
loss = tf.reduce_sum(tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(result.concat(), Y_Sent)), reduction_indices=1)))
train_step = tf.train.AdamOptimizer().minimize(loss)