2016-09-24 76 views
0

這裏train_X(shape = m,64)和train_Y(m,1)是numpy數組,其中m是我的訓練樣本數。簡單張量流線性模型

print('train and test array computed') 
print(train_X[:2]) 
print(train_Y[:2]) 

這些線路的輸出是

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 ] 

這裏是我的程序的相關部分。

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

我努力使它工作很長時間,但它從來沒有奏效。我給了我錯誤。

Traceback (most recent call last): 
    File "q.py", line 158, in <module> 
    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)' 

的問題是一致sess.run(優化,feed_dict = {X:X,Y:Y})

回答

1

我認爲錯誤是,你拉拉鍊(X,Y )in zip(train_X,train_Y):所以這會給出一個x和一個y的例子。

你,而不是要直接供給在trainX和trainY像這樣:通過運行

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

feed_dict={X: train_X, Y:train_Y} 

您可以檢查是這樣的話,你希望它有形狀X =( #,64)