Simple tensorflow linear model
<p>Here train_X(shape=m,64) and train_Y(m,1) are numpy arrays where m is my number of training samples.</p>

<pre><code>print('train and test array computed')
print(train_X[:2])
print(train_Y[:2])
</code></pre>

<p>Output of these lines is</p>

<pre><code>train and test array computed
[[  8.10590000e+01   8.91460000e+01   1.00000000e+02   1.00000000e+02
    0.00000000e+00   5.26000000e-01   5.80000000e-01   5.80000000e-01
    5.80000000e-01   5.80000000e-01   8.00000000e-01   6.66670000e+01
    6.36160000e+01   8.36000000e-01   1.17300000e+00   5.80000000e-01
    6.48860000e+01   5.80000000e-01   1.73640000e+01  -2.07250000e+01
    5.81000000e-01   0.00000000e+00   5.80000000e-01   5.80000000e-01
    7.00000000e-03   5.80000000e-01  -5.44000000e-01  -2.36000000e-01
    5.80000000e-01   5.81000000e-01   5.81000000e-01   5.80000000e-01
    5.80000000e-01   5.80000000e-01   5.80000000e-01   5.80000000e-01
    0.00000000e+00   4.00000000e-01   2.10000000e-02   1.00000000e+00
    1.00021000e+02   8.18080000e+01   5.71000000e-01   5.48000000e-01
    6.14000000e-01   5.80000000e-01   7.62000000e-01   0.00000000e+00
    1.00000000e+02   1.00000000e+02   0.00000000e+00   1.00000000e+02
    1.00000000e+02   1.16100000e+00   5.80000000e-01   6.56000000e-01
    5.80000000e-01   5.80000000e-01   5.81000000e-01   5.80000000e-01
    1.00000000e+02   5.80000000e-01   0.00000000e+00   5.80000000e-01]
 [  5.12680000e+01   4.87480000e+01   1.00000000e+02   1.00000000e+02
    0.00000000e+00   4.18000000e-01   4.44000000e-01   4.44000000e-01
    4.44000000e-01   4.44000000e-01   5.00000000e-01   6.66670000e+01
    3.83570000e+01   9.03000000e-01   1.10000000e+00   4.44000000e-01
    5.63070000e+01   4.44000000e-01   1.85220000e+01   1.94233000e+02
    4.44000000e-01   0.00000000e+00   4.44000000e-01   4.44000000e-01
    1.00000000e-03   4.44000000e-01  -8.12000000e-01  -3.53000000e-01
    4.44000000e-01   4.44000000e-01   4.44000000e-01   4.44000000e-01
    4.44000000e-01   4.44000000e-01   4.44000000e-01   4.44000000e-01
    0.00000000e+00   5.00000000e-01   2.00000000e-03   1.00000000e+00
    1.00002000e+02   6.91780000e+01   4.42000000e-01   4.29000000e-01
    4.59000000e-01   4.44000000e-01   6.66000000e-01   0.00000000e+00
    5.00000000e+01   5.00000000e+01   0.00000000e+00   5.00000000e+01
    5.00000000e+01   8.88000000e-01   4.44000000e-01   4.75000000e-01
    4.44000000e-01   4.44000000e-01   4.44000000e-01   4.44000000e-01
    5.00000000e+01   4.44000000e-01  -5.00000000e+01   4.44000000e-01]]
[ 0.44378  0.6821 ]
</code></pre>

<p>Here is the relevant part of my program.</p>

<pre><code>rng = np.random

# Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 50

# Training Data
n_samples = train_X.shape[0]
n_features = train_X.shape[1]

# tf Graph Input
X = tf.placeholder(tf.float32,[None,n_features])
Y = tf.placeholder(tf.float32,[None,1])

# Set model weights
W = tf.Variable(tf.random_normal([n_features,1],mean=0,stddev=(np.sqrt(6/n_features+
                                                         1+1))), name="weight")
b = tf.Variable(tf.random_normal([1,1],
                                    mean=0,
                                    stddev=(np.sqrt(6/n_features+1+1)),
                                    name="bias"))

# Construct a linear model
pred = tf.add(tf.mul(X, W), b)

# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Initializing the variables
init = tf.initialize_all_variables()

print('starting the session')

# Launch the graph
with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X: x, Y: y})

        # Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
                "W=", sess.run(W), "b=", sess.run(b))

    print("Optimization Finished!")
    training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
    print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')

    # Graphic display
    # plt.plot(train_X, train_Y, 'ro', label='Original data')
    # plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
    # plt.legend()
    # plt.show()

    # Testing example, as requested (Issue #2)

    print("Testing... (Mean square loss Comparison)")
    testing_cost = sess.run(
        tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * test_X.shape[0]),
        feed_dict={X: test_X, Y: test_Y})  # same function as cost above
    print("Testing cost=", testing_cost)
    print("Absolute mean square loss difference:", abs(
        training_cost - testing_cost))

    # plt.plot(test_X, test_Y, 'bo', label='Testing data')
    # plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
    # plt.legend()
    # plt.show()
</code></pre>

<p>I am struggling to make it work for a long time but it never worked. I gives me the error.</p>

<pre><code>Traceback (most recent call last):
  File "q.py", line 158, in &lt;module&gt;
    sess.run(optimizer, feed_dict={X: x, Y: y})
  File "/home/tensorflow/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 382, in run
    run_metadata_ptr)
  File "/home/tensorflow/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 640, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (64,) for Tensor 'Placeholder:0', which has shape '(?, 64)'
</code></pre>

<p>The problem is in line <em>sess.run(optimizer, feed_dict={X: x, Y: y})</em></p>

<p>I think the error is that you're zipping (x, y) in zip(train_X, train_Y): so this will give one x and one y example. </p>

<p>you instead want to directly feed in trainX and trainY like so:</p>

<pre><code>feed_dict={X: train_X, Y:train_Y}
</code></pre>

<p>You can check that this is the case by running </p>

<pre><code>for (x, y) in zip(train_X, train_Y):
    print(x.shape,y.shape) # I guess this will be 64 and 1 for x and y resp.
    sess.run(optimizer, feed_dict={X: x, Y: y})
</code></pre>

<p>you want it to have shape X = (#,64)</p>

python python-3.x numpy tensorflow linear-regression