Tensorflow,select 操作出错

Tensorflow, select operation giving error

我需要我的模型在训练和测试期间表现不同。我正在尝试构建序列到序列模型,我想在训练时将预期输出馈送到解码器,在测试时将实际输出馈送到解码器。我定义了以下应包含缩放器值的占位符。

training = tf.placeholder(tf.bool, None, 'training')

在我的解码器中,我使用以下 select 语句,其中 returns 在第一个时间步中全为零,但在最后一个输出和预期输出之间 selects 取决于它是否是否训练

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 之间的张量大小相同。但是,我想要 select 一个表达式或另一个,而不是一个元素明智的 select。有一个更好的方法吗?我想 select 应该只广播布尔值。我可以平铺它,但这似乎有点低效。

你说得对,tf.select 希望条件与其他两个输入具有相同的形状。

您应该改用 tf.cond。请参阅 关于在 TensorFlow 图中使用 if 条件。