dorien
dorien

Reputation: 5397

Error in shape of logits in TensorFlow

I am building an LSTM with TensorFlow and I think I am mis-defining my outputs because I am getting the following error:

InvalidArgumentError (see above for traceback): logits and labels must have the same first dimension, got logits shape [160,14313] and labels shape [10]
     [[Node: SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits = SparseSoftmaxCrossEntropyWithLogits[T=DT_FLOAT, Tlabels=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](add, Reshape_1)]]

Key here being: "got logits shape [160,14313] and labels shape [10]". Is the num_steps still being taken into account for the shape of the output?

The input is num_steps (16) wide and the output is just size 1, both with batch_size 10.

I have defined the network like this:

x = tf.placeholder(tf.int32, [None, num_steps], name='input_placeholder')
y = tf.placeholder(tf.int32, [None, 1], name='labels_placeholder')


x_one_hot = tf.one_hot(x, num_classes)
rnn_inputs = [tf.squeeze(i, squeeze_dims=[1]) for i in
              tf.split(x_one_hot, num_steps, 1)]  # still a list of tensors (batch_size, num_classes)

tmp = tf.stack(rnn_inputs)
print(tmp.get_shape())
tmp2 = tf.transpose(tmp, perm=[1, 0, 2])
print(tmp2.get_shape())

rnn_inputs = tmp2


cell = tf.contrib.rnn.LSTMCell(state_size, state_is_tuple=True)
cell = tf.contrib.rnn.MultiRNNCell([cell] * num_layers, state_is_tuple=True)

init_state = cell.zero_state(batch_size, tf.float32)
print(init_state)
rnn_outputs, final_state = tf.nn.dynamic_rnn(cell, rnn_inputs, initial_state=init_state)

with tf.variable_scope('softmax'):
    W = tf.get_variable('W', [state_size, num_classes])
    b = tf.get_variable('b', [num_classes], initializer=tf.constant_initializer(0.0))

#reshape rnn_outputs and y
rnn_outputs = tf.reshape(rnn_outputs, [-1, state_size])
y_reshaped = tf.reshape(y, [-1])

logits = tf.matmul(rnn_outputs, W) + b

Upvotes: 0

Views: 550

Answers (1)

dorien
dorien

Reputation: 5397

Fixed by adding this line:

rnn_outputs = rnn_outputs[:, num_steps-1, :]

Upvotes: 1

Related Questions