Reputation: 12175
I need my model to behave differently during training vs testing. I am trying to build I sequence to sequence model and I want to feed the expected output into the decoder when training and the actual output when I am testing. I defined the following placeholder which should contain a scaler value.
training = tf.placeholder(tf.bool, None, 'training')
In my decoder I use the following select statement which returns all zeros in the first time step but selects between the last output and expected output depending on if it is training or not.
last_step = tf.select(training, getTimeStep(expected_output, t - 1), decoder_outputs[t - 1])if t else tf.zeros((BATCH_SIZE, 128))
When I run the model in training mode I use the following where I set training to True.
sess.run([accuracy, cross_entropy, train_step], feed_dict = {input_tensor: x_train, expected_output: y_train, training: True})
When it runs I get the following error.
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]
It seems the condition needs to be the same size as the tensors I am selecting between. However, I want to select one expression or another, not an element wise select. Is there a better way to do this? I figured select should just broadcast the boolean. I can tile it but that seems somewhat inefficient.
Upvotes: 3
Views: 3267
Reputation: 28198
You are right, tf.select
wants the condition to be of the same shape as the two other inputs.
You should instead use tf.cond
. See this detailed answer about using if condition in the TensorFlow graph.
Upvotes: 2