组合函数(即用张量替换占位符)

时间:2017-12-06 15:54:52

标签: python tensorflow

我需要连接两个函数:首先我定义f(x),然后定义g(t),最后我需要设置x = g(t)

MWE

import tensorflow as tf
import numpy as np

session = tf.Session()

# f(x)
x = tf.placeholder(shape=[None,4], dtype=tf.float64)
f = tf.square(x)

# g(t)
t = tf.placeholder(shape=[None,2], dtype=tf.float64)
g = tf.matmul(t,np.ones((2,4)))

session.run(tf.global_variables_initializer())
t_eval = np.asmatrix(np.random.rand(10,2))

# f(g(t))
session.run(f, {x: session.run(g, {t: t_eval})})

# but I want to do (does not work because I need to assign a value to placeholder x)
x = g
session.run(f, {t: t_eval})

基本上我想用张量x替换占位符g。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您的代码根本不需要x

f = tf.square(g)  # replace your definition of f with this
session.run(f, {t: t_eval})