0

I am building a 1d convolutional neural net for my own data (spectra) and am having an issue with tf.reshape. First I load in the data with pandas, and convert these to numpy arrays, composed of 708 training example spectra, each of length 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()

where I subtract the mean value in each column. So data is of dimensions 708 x 2151 here. I then create a network that starts with,

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

which are inputs for my 1d convolutional neural net (with kernels with a width of 10, and 32 feature maps),

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)

I then build the rest of the network and then try to run ADAM on it,

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

However I get the following error:

ValueError: Cannot feed value of shape (708, 2151) for Tensor u'Reshape_26:0', 
which has shape '(?, 1, 2151, 1)'

I have looked at these answers: 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 which suggest that I need to be doing some reshaping before I pass my data to the network. However, I am not sure what this should be? Particularly since the following works on the first row of the data,

t = tf.constant(data[0])
tf.reshape(t,[1,1,2151,1])

Does anyone have any ideas here?

Best,

Ben

Community
  • 1
  • 1
ben18785
  • 356
  • 1
  • 5
  • 15

1 Answers1

1

The issue is that feed_dict can replace any Tensor, and since you've changed x_ to reference the reshape op, that's the thing that it's trying to replace. It should work if you just use different Python variables to reference the placeholder and the reshape op:

x_placeholder_ = tf.placeholder(tf.float32, shape=[None, 2151])
x_ = tf.reshape(x_placeholder_, [-1,1,2151,1])

Then when feeding, use x_placeholder_:

sess.run(train_step, feed_dict={x_placeholder_: data, y_: yTrue})
Allen Lavoie
  • 5,778
  • 1
  • 17
  • 26
  • Thank you, but when I change the code to the above I get the following error, InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_47' with dtype float [[Node: Placeholder_47 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] -- Any ideas what this means? I thought that using feed_dict should mean this is fine... – ben18785 Nov 04 '16 at 23:05
  • 1
    Are you doing this inside a Jupyter notebook? It sounds like a whole bunch of placeholders are floating around. Try `tf.reset_default_graph()`? – Allen Lavoie Nov 04 '16 at 23:16
  • I am. I tried the above, but unfortunately I am still having the same issue. Any more ideas? Sorry about this! – ben18785 Nov 04 '16 at 23:27
  • 1
    Is it `Placeholder_48` or something like `Placeholder_1`? – Allen Lavoie Nov 04 '16 at 23:37
  • 1
    Hmm, could you try naming the placeholders by passing a `name=` argument? – Allen Lavoie Nov 04 '16 at 23:42
  • When I do the following: x_placeholder_ = tf.placeholder(tf.float32, shape=[None, 2151],name="test") -- I get a Placeholder_1 error. – ben18785 Nov 04 '16 at 23:43
  • 1
    Is there another placeholder somewhere in your code, defined after the `tf.reset_default_graph`? If you name all of them, presumably the error will at least tell you which one. – Allen Lavoie Nov 04 '16 at 23:45
  • Aha -- yes. Great thinking. It was a Placeholder for a probability of keeping a connection in a dropout layer, that I wasn't passing. Thank you so much for your help here. Really appreciate it. Best, Ben – ben18785 Nov 04 '16 at 23:48