Tensorflow:如何以“笛卡尔积”方式减去2个张量?

时间:2018-03-26 12:27:37

标签: python python-3.x numpy tensorflow

我想做这样的事情:

v = tf.get_variable('v', [N])
p = tf.placeholder(shape=[None])
tmp = []
for element_p in p:
   tmp.append(element_p - v)
ret = tf.stack(tmp, axis=0)
# ret.get_shape().as_list() = [None, N]

这在Tensorflow中是否可行?

1 个答案:

答案 0 :(得分:0)

利用广播:

import tensorflow as tf

N = ...
v = tf.get_variable('v', [N])
p = tf.placeholder(shape=[None])
ret = p[:, tf.newaxis] - v[tf.newaxis, :]