TensorFlow中的logits形状出错

时间:2017-03-23 10:44:13

标签: tensorflow neural-network deep-learning recurrent-neural-network

我正在使用TensorFlow构建LSTM,我认为我错误地定义了输出,因为我收到以下错误:

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)]]

这里的关键是:" 得到logits形状[160,14313]和标签形状[10] "。 num_steps是否仍然考虑到输出的形状?

输入为num_steps(16)宽 而输出只是大小 1,都是batch_size 10。

我已经像这样定义了网络:

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

1 个答案:

答案 0 :(得分:1)

通过添加以下行修复:

#set($inputRoot = $input.path('$'))
{ 
  "userId": "$input.params('userId')",
  "userEmail": "$input.params('userEmail')",
  "userName": "$input.params('userName')",
  "userPassword": "$input.params('userPassword')"
}