将 Python 函数列表应用于张量
Applying a list of Python functions to a Tensor
我有一个预先计算和存储的 Python lambda 函数列表,例如:
fn_list = [lambda x: x + 2 for x in some_data]
现在,我想要张量:
X = tf.placeholder(..., shape=[None, 1])
将 fn_list
中的每个函数应用于 X
的 None
维度中的每个值。
理想情况下,输出将具有与 X
相同的形状,方法是使用 tf.reduce_sum
为 X
中的每个值压缩来自 fn_list
的所有结果。
下面的代码示例显示了我想从上述问题中得到什么:
import numpy as np
import tensorflow as tf
def _get_gaussian(loc=0.0, scale=1.0):
return lambda x: (1/scale * np.sqrt(2 * np.pi)) * np.exp(-0.5 * np.power((x-loc)/scale, 2))
data = np.random.normal(3, 1.25, 100)
fn_list = [_get_gaussian(loc=x, scale=0.2) for x in data]
X = tf.placeholder(tf.float32, shape=[None, 1])
pred = tf.map_fn(lambda x: tf.map_fn(lambda fn_: tf.reduce_sum(fn_(x)), fn_list), X)
上面的代码基本上会通过首先计算 data
中每个点周围的正态分布来执行某种内核密度估计,然后将内核存储在 fn_list
中。
然后,对于某个 Tensor X
,它可以计算 X
.
中每个值的可能性
pred
现在是形状为 X.shape
.
的张量
这样的事情有可能吗?
我的脑袋有点疼,试图按照这个:)但基本上,如果你像这样打破最后一行:
pred = tf.map_fn( lambda x: tf.map_fn(lambda fn_: tf.reduce_sum(fn_(x)),
fn_list),
X)
那么你的问题现在更清楚了。也就是说,内部 tf.map_fn
试图将函数 tf.reduce_sum( fn_( x ) )
映射到 fn_list
,但后者是 函数的列表 而不是张量。此外,外部 tf.map_fn
试图将内部映射 的 结果映射到 X,但该结果 不是函数 而是张量。所以对这段代码进行一些重新思考和重组是为了,没有映射,高斯表示为Tensorflow操作,可能是这样的:
import numpy as np
import tensorflow as tf
data = np.random.normal(3, 1.25, 5)
#X = tf.placeholder(tf.float32, shape=[None, 1])
X = tf.constant( [ 1, 2, 3 ], dtype = tf.float32 )
scale = 0.2
coeff = np.sqrt(2 * np.pi) / scale
X_fn = []
for idx in xrange( len ( data ) ):
x = data[ idx ]
X_fn.append(
coeff * tf.exp( - 0.5 * tf.pow( ( X - x ) / scale, 2 ) )
)
X1 = tf.stack( X_fn, axis = 0 )
X2 = tf.reduce_sum( X1, axis = 1 )
with tf.Session() as sess:
print( sess.run( X2 ) )
输出:
[3.7835757e-09 9.0623178e+00 9.2027439e-03 2.7527404e-01 1.1571876e+00]
上面的代码有效,但我不完全确定它是否完全符合您的要求。还是希望对你有帮助。
我有一个预先计算和存储的 Python lambda 函数列表,例如:
fn_list = [lambda x: x + 2 for x in some_data]
现在,我想要张量:
X = tf.placeholder(..., shape=[None, 1])
将 fn_list
中的每个函数应用于 X
的 None
维度中的每个值。
理想情况下,输出将具有与 X
相同的形状,方法是使用 tf.reduce_sum
为 X
中的每个值压缩来自 fn_list
的所有结果。
下面的代码示例显示了我想从上述问题中得到什么:
import numpy as np
import tensorflow as tf
def _get_gaussian(loc=0.0, scale=1.0):
return lambda x: (1/scale * np.sqrt(2 * np.pi)) * np.exp(-0.5 * np.power((x-loc)/scale, 2))
data = np.random.normal(3, 1.25, 100)
fn_list = [_get_gaussian(loc=x, scale=0.2) for x in data]
X = tf.placeholder(tf.float32, shape=[None, 1])
pred = tf.map_fn(lambda x: tf.map_fn(lambda fn_: tf.reduce_sum(fn_(x)), fn_list), X)
上面的代码基本上会通过首先计算 data
中每个点周围的正态分布来执行某种内核密度估计,然后将内核存储在 fn_list
中。
然后,对于某个 Tensor X
,它可以计算 X
.
pred
现在是形状为 X.shape
.
这样的事情有可能吗?
我的脑袋有点疼,试图按照这个:)但基本上,如果你像这样打破最后一行:
pred = tf.map_fn( lambda x: tf.map_fn(lambda fn_: tf.reduce_sum(fn_(x)),
fn_list),
X)
那么你的问题现在更清楚了。也就是说,内部 tf.map_fn
试图将函数 tf.reduce_sum( fn_( x ) )
映射到 fn_list
,但后者是 函数的列表 而不是张量。此外,外部 tf.map_fn
试图将内部映射 的 结果映射到 X,但该结果 不是函数 而是张量。所以对这段代码进行一些重新思考和重组是为了,没有映射,高斯表示为Tensorflow操作,可能是这样的:
import numpy as np
import tensorflow as tf
data = np.random.normal(3, 1.25, 5)
#X = tf.placeholder(tf.float32, shape=[None, 1])
X = tf.constant( [ 1, 2, 3 ], dtype = tf.float32 )
scale = 0.2
coeff = np.sqrt(2 * np.pi) / scale
X_fn = []
for idx in xrange( len ( data ) ):
x = data[ idx ]
X_fn.append(
coeff * tf.exp( - 0.5 * tf.pow( ( X - x ) / scale, 2 ) )
)
X1 = tf.stack( X_fn, axis = 0 )
X2 = tf.reduce_sum( X1, axis = 1 )
with tf.Session() as sess:
print( sess.run( X2 ) )
输出:
[3.7835757e-09 9.0623178e+00 9.2027439e-03 2.7527404e-01 1.1571876e+00]
上面的代码有效,但我不完全确定它是否完全符合您的要求。还是希望对你有帮助。