2017-03-17 162 views
1
自定義分類

到目前爲止,這裏是我做了什麼:無法建立tensorflow

import tensorflow as tf 

dists_next_error = tf.placeholder(tf.float32) 
dists_center_error = tf.placeholder(tf.float32) 
pts_count = tf.placeholder(tf.float32) 
ideal_polygon = tf.Variable(0.) 

cost = tf.square(dists_next_error)  \ 
     + tf.square(dists_center_error) \ 
     + tf.square(pts_count - ideal_polygon) 

optimizer = tf.train.GradientDescentOptimizer(.05).minimize(cost) 

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

hund_zeros = tf.zeros([100]) 
hund_ones = tf.ones([100]) 

for i in range(1000): 
    sess.run(optimizer, feed_dict={ 
     dists_next_error: hund_zeros, 
     dists_center_error: hund_zeros, 
     pts_count: hund_ones}) 

print(cost.eval(feed_dict={ 
     dists_next_error: 0., 
     dists_center_error: 0., 
     pts_count: 6.}))   #it should output 0 or close to it. 

的問題是在

sess.run(optimizer, feed_dict={ 
     dists_next_error: hund_zeros, 
     dists_center_error: hund_zeros, 
     pts_count: hund_ones}) 

pts_count線更確切地說,它說: :

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.

但我看不到te nsors在pts_count,所以我不知道發生了什麼事。

回答

0

張量不能用作飼料值(這你知道); tf.ones(),tf.zeros創建Tensors。所以,

hund_zeros = tf.zeros([100]) 
hund_ones = tf.ones([100]) 

只是張量。

你想要做的是發送一些實際的數字(這是你的意圖,它似乎)。你可以使用:

  • numpy的:np.ones([100])np.zeros([100])
  • 或使用hund_zeros, hund_ones = sess.run([hund_zeros, hund_ones])
  • 使用Tensor.eval()方法

參考答案在這裏更多地瞭解eval()

下面是您提供的代碼使用with上下文管理器更改:

import tensorflow as tf 

dists_next_error = tf.placeholder(tf.float32) 
dists_center_error = tf.placeholder(tf.float32) 
pts_count = tf.placeholder(tf.float32) 
ideal_polygon = tf.Variable(0.) 

cost = tf.square(dists_next_error)  \ 
     + tf.square(dists_center_error) \ 
     + tf.square(pts_count - ideal_polygon) 

optimizer = tf.train.GradientDescentOptimizer(.05).minimize(cost) 


hund_zeros = tf.zeros([100]) 
hund_ones = tf.ones([100]) 

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

with tf.Session() as sess: 
    sess.run(init) 

    # run graphs to get some constants 
    hund_zeros, hund_ones = sess.run([hund_zeros, hund_ones]) 

    for i in range(1000): 
     sess.run(optimizer, feed_dict={ 
      dists_next_error: hund_zeros, 
      dists_center_error: hund_zeros, 
      pts_count: hund_ones}) 

    print(cost.eval(feed_dict={ 
      dists_next_error: 0., 
      dists_center_error: 0., 
      pts_count: 6.})) 

以此開始。