Tensorflow:变量batch_size在尝试使用eval进行预测时会出错(输入的维度应该匹配)

时间:2017-03-14 03:10:15

标签: tensorflow neural-network recurrent-neural-network tensorflow-serving

我正在训练一个带有变量batch_size的模型(第一批是200)。所以我使用了batch_size None来使它变量(我不能为init_state做那个,因为它给出了一个错误)。

x = tf.placeholder(tf.int32, [None, num_steps], name='input_placeholder')
y = tf.placeholder(tf.int32, [None, num_steps], name='labels_placeholder')
init_state = tf.zeros([batch_size, state_size])
rnn_inputs = tf.one_hot(x, num_classes)

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))
logits = tf.reshape(
            tf.matmul(tf.reshape(rnn_outputs, [-1, state_size]), W) + b,
            [batch_size, num_steps, num_classes])
predictions = tf.nn.softmax(logits)

训练模型进展顺利。

然后我尝试使用x形状(1,10)而不是(200,10)来预测概率:

我试过了:

test = np.array([[1, 2 , 3 , 4, 5, 6 ,7, 8, 9 ,10]], dtype=np.int32)
  print (predictions.eval(feed_dict = {x:test}))

我也尝试过一种不同的方式:

preds, state = sess.run([g['preds'],g['final_state']], feed_dict)

同样的错误:

InvalidArgumentError (see above for traceback): ConcatOp : Dimensions of inputs should match: shape[0] = [1,1058] vs. shape[1] = [200,4]
     [[Node: rnn/while/basic_rnn_cell/basic_rnn_cell/concat = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](rnn/while/TensorArrayReadV3, rnn/while/Identity_2, rnn/while/basic_rnn_cell/basic_rnn_cell/concat/axis)]]

所以1058是我的num_classes,200是(初始)batch_size,4是张量的宽度。

我认为我没有正确使用变量batch_size。关于改变什么的任何想法?

0 个答案:

没有答案