现在,我有一些代码如下:
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)
我想将值提供给函数。
下面是一个明显的错误。 我怎么想这样做?
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)
答案 0 :(得分:2)
一个选项是在函数内部运行会话,如下所示:
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)
您还可以创建一个类,让x成为该类的一个字段。