张量流占位符的功能

时间:2016-09-17 17:01:00

标签: python tensorflow placeholder

现在,我有一些代码如下:

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)

1 个答案:

答案 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成为该类的一个字段。

相关问题