2016-09-24 11 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あるIエラーがあなたが(X、Yをzip圧縮していることであると考え

答えて

1

(オプティマイザ、feed_dict = {X:Y:X、Y}) )in zip(train_X、train_Y):これは1つのxとyの例を与えます。

あなたの代わりに直接そうようtrainXとtrainYに送りたい

:あなたはそれを形にしたい

feed_dict={X: train_X, Y:train_Y} 

あなたはこのケースを確認することができます

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

を実行することにより、X =( #、64)

関連する問題