我试图在tensorflow中将数据输入到我的模型中。我有一个大小为N的输入向量,但为了使tf.matmul(X,weights ['h1'])工作,我需要将数据的形状设为(None,N)。
假设我有两个大小为N的数组(在我的情况下为N = 1000),分别对应于输入和输出(分别为X和Y)。输入/输出数据已经分别定义为x和y。我已为我的代码部分如下:num_input = 1000
num_output = 1000
#place holders for tensorflow
X = tf.placeholder("float", [None, num_input])
Y = tf.placeholder("float", [None, num_output])
#Define weights/biases
weights = {
"h1" : tf.Variable(tf.random_normal([num_input, n_hidden_1])),
"out" : tf.Variable(tf.random_normal([n_hidden_1, num_output]))
}
biases = {
"b1" : tf.Variable(tf.random_normal([n_hidden_1])),
"out" : tf.Variable(tf.random_normal([num_output]))
}
#define neural network
def neural_net(x):
logits_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.softmax(logits_1)
out_logits = tf.matmul(layer_1, weights['out']) + biases['out']
out_layer = tf.nn.softmax(out_logits)
return out_layer
运行代码时,出现以下错误,我确定这是由于数据尺寸与上面定义的占位符不匹配所致。这是错误:
Traceback (most recent call last):
File "main.py", line 69, in <module>
sess.run(optimizer, feed_dict={X: x,Y: y})
File "/user/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 887, in run
run_metadata_ptr)
File "/user/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1086, in _run
str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (1000,) for Tensor 'Placeholder:0', which has shape '(?, 1000)'
有一种方法来重塑我的形状(N)的x和y阵列的形状(无,N)?
提前谢谢!
答案 0 :(得分:0)
如果您的输入数据总是呈[N]
形状,那么我认为这样定义您的输入更有意义,所以:
x = tf.placehoder(tf.float32, [N])
y = tf.placehoder(tf.float32, [N])
,然后当您需要将它们相乘时,可以添加一个额外的维度:
x = tf.expand_dims(x, 0)
y = tf.expand_dims(y, 0)
或者,您可以输入额外的维度。
x = np.expand_dims(x, 0)
y = np.expand_dims(y, 0)
sess.run(optimizer, feed_dict={X: x,Y: y})