tensorflow placeholder in function
<p>Now, I have a bit of code like below:</p>

<pre><code>import tensorflow as tf

x = tf.placeholder(tf.float32, [1, 3])
y = x * 2

with tf.Session() as sess:
    result = sess.run(y, feed_dict={x: [2, 4, 6]})
    print(result)
</code></pre>

<p>I want to feed values to a function.</p>

<p>Below is an obvious wrong.
How do I suppose to do that?</p>

<pre><code>import tensorflow as tf

def place_func():
    x = tf.placeholder(tf.float32, [1, 3])
    y = x * 2

with tf.Session() as sess:
    result = sess.run(y, feed_dict={x: [2, 4, 6]})
    print(result)
</code></pre>

<p>One option is to run a session inside of function just as follows: </p>



<pre><code>import tensorflow as tf

def my_func(data):
    x = tf.placeholder(tf.float32, [1, 3])
    y = x * 2
    return sess.run(y, feed_dict = {x: data})

with tf.Session() as sess:
    result = my_func([[2, 4, 6]])
    print(result)
</code></pre>

<p>You could also create a class and let x to be a field of that class. </p>

python tensorflow placeholder