在Tensorflow中创建神经网络层具有N tf.exp函数的和

时间:2018-07-11 09:32:24

标签: tensorflow neural-network sum

我想创建一个函数f,它将接受输入x并将输出y赋予为:y = f(x)

f(x) = c1 exp( (x-m1) /l1 ) + c2 exp( (x-m2) /l2 ) + ...... 

此处,x是输入张量。 cnmnlnn=1N)是Tensorflow标量变量(N*3变量)。

1 个答案:

答案 0 :(得分:0)

解决方案1)明确广播然后减少:

tiling = [1] * len(x.get_shape().as_list()) + [n]
res = tf.reduce_sum(
    c * tf.exp((tf.tile(tf.expand_dims(x, axis=-1), tiling) - m) / l), 
    axis=-1)

解决方案2)遍历n

condition = lambda i, y: i < n
operation = lambda i, y: [i+1, y + c[i] * tf.exp((x - m[i]) / l[i])]
_, res = tf.while_loop(condition, operation,
                       loop_vars=[0, tf.zeros_like(x)],
                       shape_invariants=[tf.TensorShape([]), x.get_shape()])

示例:

import tensorflow as tf
import numpy as np

# Inputs for example:
x_val = np.random.rand(3, 2)
n = 3
c_val, m_val, l_val = np.random.rand(3, n)

x = tf.constant(x_val)
c, m, l = tf.constant(c_val), tf.constant(m_val), tf.constant(l_val)

# Getting numpy result for comparison:
res = np.sum([c_val[i] * np.exp((x_val - m_val[i]) / l_val[i]) for i in range(n)], axis=0)
print(res)
# [[ 2.55195594  0.42834575]
#  [ 0.29125215  0.29025419]
#  [ 0.74048059  1.63411303]]

# Solution 1:
tiling = [1] * len(x.get_shape().as_list()) + [n]
res_broad = tf.reduce_sum(c * tf.exp((tf.tile(tf.expand_dims(x, axis=-1), tiling) - m) / l), 
                          axis=-1)

# Solution 2:
condition = lambda i, y: i < n
operation = lambda i, y: [i+1, y + c[i] * tf.exp((x - m[i]) / l[i])]
_, res_loop = tf.while_loop(condition, operation,
                            loop_vars=[0, tf.zeros_like(x)],
                            shape_invariants=[tf.TensorShape([]), x.get_shape()])

with tf.Session() as sess:
    print(sess.run(res_broad))
# [[2.55195594  0.42834575]
#  [0.29125215  0.29025419]
#  [0.74048059 1.63411303]]
    print(sess.run(res_loop))
# [[2.55195594  0.42834575]
#  [0.29125215  0.29025419]
#  [0.74048059 1.63411303]]