我正在为自己的数据(光谱)构建一个1d卷积神经网络,并且我遇到了tf.reshape的问题。首先我用pandas加载数据,然后将它们转换为numpy数组,由708个训练样例光谱组成,每个光谱长度为2151,
addItem: function(section, ev, eventArgs) {
...
}
其中我减去每列中的平均值。因此,数据的尺寸为708 x 2151。然后我创建一个以
开头的网络import pandas as pd
import numpy as np
data = pd.read_csv('test.csv',header=None)
yTrue = data.ix[:,0].as_matrix()
data = data - data.mean()
data = data.ix[:,1:].as_matrix()
是我的1d卷积神经网络的输入(内核宽度为10,有32个特征映射),
sess = tf.InteractiveSession()
## define inputs
x_ = tf.placeholder(tf.float32, shape=[None, 2151])
x_ = tf.reshape(x_, [-1,1,2151,1])
y_ = tf.placeholder(tf.float32, shape=[None])
然后构建网络的其余部分,然后尝试在其上运行ADAM,
W_conv1 = weight_variable([1, 10, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
但是我收到以下错误:
cost_function = tf.reduce_mean(tf.pow(y_out - y_, 2))/(2 * samples_number) #L2 loss
train_step = tf.train.AdamOptimizer(1e-4).minimize(cost_function)
correct_prediction = tf.equal(tf.argmax(y_out,1), tf.argmax(y_,1))
sess.run(tf.initialize_all_variables())
for i in range(20000):
print(i)
sess.run(train_step, feed_dict={x_: data, y_: yTrue})
我看过这些答案:TensorFlow/TFLearn: ValueError: Cannot feed value of shape (64,) for Tensor u'target/Y:0', which has shape '(?, 10)'; Tensorflow error using my own data这表明我需要在将数据传递到网络之前进行一些重塑。但是,我不确定这应该是什么?特别是因为以下工作在数据的第一行,
ValueError: Cannot feed value of shape (708, 2151) for Tensor u'Reshape_26:0',
which has shape '(?, 1, 2151, 1)'
有人有任何想法吗?
最佳,
本
答案 0 :(得分:1)
问题是feed_dict可以替换任何Tensor,并且因为你已经改变了x_来引用reshape
op,所以它正在尝试替换它。如果你只是使用不同的Python变量来引用占位符和重塑op:
x_placeholder_ = tf.placeholder(tf.float32, shape=[None, 2151])
x_ = tf.reshape(x_placeholder_, [-1,1,2151,1])
然后在喂食时,使用x_placeholder_
:
sess.run(train_step, feed_dict={x_placeholder_: data, y_: yTrue})