我需要我的模型在训练和测试期间表现不同。我正在尝试构建序列到序列模型,我想在训练时将预期输出馈送到解码器,在测试时将实际输出馈送到解码器。我定义了以下应包含缩放器值的占位符。
training = tf.placeholder(tf.bool, None, 'training')
在我的解码器中,我使用以下选择语句,它在第一个时间步返回所有零,但根据是否正在训练在最后一个输出和预期输出之间进行选择。
last_step = tf.select(training, getTimeStep(expected_output, t - 1), decoder_outputs[t - 1])if t else tf.zeros((BATCH_SIZE, 128))
当我在训练模式下运行模型时,我使用以下方法将训练设置为 True。
sess.run([accuracy, cross_entropy, train_step], feed_dict = {input_tensor: x_train, expected_output: y_train, training: True})
运行时出现以下错误。
tensorflow.python.framework.errors.InvalidArgumentError: Inputs to operation decoder/Select of type Select must have the same size and shape. Input 0: [] != input 1: [32,128]
条件似乎需要与我在其中选择的张量大小相同。但是,我想选择一个或另一个表达式,而不是元素明智的选择。有一个更好的方法吗?我想 select 应该只广播 bool 值。我可以平铺它,但这似乎有点低效。
请您参考如下方法:
你是对的,tf.select 希望条件与其他两个输入具有相同的形状。
您应该改用 tf.cond。参见 this detailed answer关于在 TensorFlow 图中使用 if 条件。
