2015-11-19 17 views
16

Sto cercando di implementare una rete di feed-forward semplice. Tuttavia, non riesco a capire come alimentare un Placeholder. Questo esempio:Come nutrire un segnaposto?

import tensorflow as tf 

num_input = 2 
num_hidden = 3 
num_output = 2 

x = tf.placeholder("float", [num_input, 1]) 
W_hidden = tf.Variable(tf.zeros([num_hidden, num_input])) 
W_out = tf.Variable(tf.zeros([num_output, num_hidden])) 
b_hidden = tf.Variable(tf.zeros([num_hidden])) 
b_out = tf.Variable(tf.zeros([num_output])) 

h = tf.nn.softmax(tf.matmul(W_hidden,x) + b_hidden) 

sess = tf.Session() 

with sess.as_default(): 
    print h.eval() 

mi dà il seguente errore:

... 
    results = self._do_run(target_list, unique_fetch_targets, feed_dict_string) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 419, in _do_run 
    e.code) 
tensorflow.python.framework.errors.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape dim { size: 2 } dim { size: 1 } 
    [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[2,1], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] 
Caused by op u'Placeholder', defined at: 
    File "/home/sfalk/workspace/SemEval2016/java/semeval2016-python/slot1_tf.py", line 8, in <module> 
    x = tf.placeholder("float", [num_input, 1]) 
    ... 

ho cercato

tf.assign([tf.Variable(1.0), tf.Variable(1.0)], x) 
tf.assign([1.0, 1.0], x) 

ma che a quanto pare non funziona.

+0

bella domanda, ho cercato di capire anche tu questo –

risposta

28

Per inserire un segnaposto, utilizzare l'argomento feed_dict su Session.run() (o Tensor.eval()). Diciamo che avete il grafico seguente, con un segnaposto:

x = tf.placeholder(tf.float32, shape=[2, 2]) 
y = tf.constant([[1.0, 1.0], [0.0, 1.0]]) 
z = tf.matmul(x, y) 

Se si desidera valutare z, è necessario alimentare un valore per x. È possibile farlo nel modo seguente:

sess = tf.Session() 
print sess.run(z, feed_dict={x: [[3.0, 4.0], [5.0, 6.0]]}) 

Per ulteriori informazioni, vedere la documentation on feeding.

+0

Hmm .. non c'è altro modo? Ciò sembra inopportuno se io ad es. voglio guardare i risultati intermedi. – displayname

+0

È anche possibile passare l'argomento 'feed_dict' a' Tensor.eval() ', che potrebbe essere più comodo quando si costruisce il grafico. Se si desidera un segnaposto "appiccicoso", suggerirei di creare una propria funzione che avvolga 'sess.run()', acquisisca un insieme di valori di feed e passi ogni volta alla chiamata 'run() '. – mrry

+0

@mrry, potresti fare un esempio del tuo commento? Grazie – Amir