2017-07-11 44 views
0

這是我的第一個運行良好的代碼。簡單地將輸入層的尺寸從2改爲10

輸入是2點的尺寸和輸出是2名維

第一代碼:

w = tf.Variable(tf.zeros([2,1])) 
b = tf.Variable(tf.zeros([1])) 


x = tf.placeholder(tf.float32,shape=[None,2]) 
t = tf.placeholder(tf.float32,shape=[None,1]) 
y = tf.nn.sigmoid(tf.matmul(x,w) + b) 

cross_entropy = - tf.reduce_sum(t * tf.log(y) + (1-t) * 
           tf.log(1 -y)) 
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy) 
correct_prediction = tf.equal(tf.to_float(tf.greater(y,0.5)),t) 


X = np.array([[0,0],[0,1],[1,0],[1,1]]) 
Y = np.array([[0],[1],[1],[1]]) 

init = tf.global_variables_initializer() 
sess = tf.Session() 
sess.run(init) 

for epoch in range(200): 
    sess.run(train_step,feed_dict={ 
     x:X, 
     t:Y 
    }) 

現在我想這延伸到10個維輸入和2個尺寸輸出。

然後我改變了這個樣子,但是它顯示錯誤 。 我知道這個錯誤與佔位符的大小有關,但是 我應該在哪裏改變,爲什麼?

Traceback (most recent call last): 
    File "wisdom2.py", line 57, in <module> 
    t: Y 
    File "/Users/whitebear/tensorflow/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 789, in run 
    run_metadata_ptr) 
    File "/Users/whitebear/tensorflow/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 975, in _run 
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape()))) 
ValueError: Cannot feed value of shape (5, 1) for Tensor 'Placeholder:0', which has shape '(?, 10)' 

第二碼:

w = tf.Variable(tf.zeros([10,1])) ## change dimensions to 2 -> 10 
b = tf.Variable(tf.zeros([1])) 

x = tf.placeholder(tf.float32,shape=[None,10]) # change dimensions to 2 -> 10 
t = tf.placeholder(tf.float32,shape=[None,1]) 
y = tf.nn.sigmoid(tf.matmul(x,w) + b) 

cross_entropy = - tf.reduce_sum(t * tf.log(y) + (1 -t) * tf.log(1 -y)) 
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy) 
correct_prediction = tf.equal(tf.to_float(tf.greater(y,0.5)),t) 

##I changed here.... 
X = np.array([[0],[1],[0],[1],[1]]) #answer 
Y = np.array([ 
[2,-2,3,-4,2,2,3,5,3,6], 
[1,3,-3,2,2,5,1,3,2,3], 
[-2,3,2,-2,2,-2,1,3,4,5], 
[-2,2,-1,-2,2,-2,7,3,9,2], 
[-2,-3,2,-2,2,-4,1,-4,4,5] 
]) 


init = tf.global_variables_initializer() 
sess = tf.Session() 
sess.run(init) 

for epoch in range(200): 
    sess.run(train_step,feed_dict={ 
     x: X, 
     t: Y 
    }) 

回答

1

你是餵養錯誤的形狀,投入到placeholders。您已在佔位符中更改了x的尺寸,但爲其輸入了錯誤的輸入X(您沒有更改),而不是y(您已更改)。所以要麼交換X,Y或更改相應的placeholders

+0

我只是錯誤地給了XY數據反向。非常感謝 – whitebear