我有三个张量,张量流A, B and C
,A
和B
都是(m, n, r)
形状,C
是形状(m, n, 1)
的二元张量1}}。
我想根据C
的值从A或B中选择元素。显而易见的工具是tf.select
,但是没有广播语义,所以我需要首先将C
明确地广播到与A和B相同的形状。
这是我第一次尝试如何做到这一点,但它并不像我将张量(tf.shape(A)[2]
)混合到形状列表中。
import tensorflow as tf
A = tf.random_normal([20, 100, 10])
B = tf.random_normal([20, 100, 10])
C = tf.random_normal([20, 100, 1])
C = tf.greater_equal(C, tf.zeros_like(C))
C = tf.tile(C, [1,1,tf.shape(A)[2]])
D = tf.select(C, A, B)
这里有什么正确的方法?
答案 0 :(得分:13)
编辑:在0.12rc0之后的所有TensorFlow版本中,问题中的代码都可以直接使用。 TensorFlow会自动将张量和Python数字叠加到张量参数中。使用tf.pack()
的以下解决方案仅在0.12rc0之前的版本中需要。请注意,tf.pack()
已在TensorFlow 1.0中重命名为tf.stack()
。
您的解决方案非常接近工作。您应该替换以下行:
C = tf.tile(C, [1,1,tf.shape(C)[2]])
......以下内容:
C = tf.tile(C, tf.pack([1, 1, tf.shape(A)[2]]))
(问题的原因是TensorFlow无法隐式地将张量和Python文字列表转换为张量。tf.pack()
获取张量列表,因此它将转换每个元素它的输入(1
,1
和tf.shape(C)[2]
)到张量。由于每个元素都是一个标量,因此结果将是一个向量。)
答案 1 :(得分:3)
这是一个肮脏的黑客:
import tensorflow as tf
def broadcast(tensor, shape):
return tensor + tf.zeros(shape, dtype=tensor.dtype)
A = tf.random_normal([20, 100, 10])
B = tf.random_normal([20, 100, 10])
C = tf.random_normal([20, 100, 1])
C = broadcast(C, A.shape)
D = tf.select(C, A, B)
答案 2 :(得分:0)
import tensorflow as tf
def broadcast(tensor, shape):
"""Broadcasts ``x`` to have shape ``shape``.
|
Uses ``tf.Assert`` statements to ensure that the broadcast is
valid.
First calculates the number of missing dimensions in
``tf.shape(x)`` and left-pads the shape of ``x`` with that many
ones. Then identifies the dimensions of ``x`` that require
tiling and tiles those dimensions appropriately.
Args:
x (tf.Tensor): The tensor to broadcast.
shape (Union[tf.TensorShape, tf.Tensor, Sequence[int]]):
The shape to broadcast to.
Returns:
tf.Tensor: ``x``, reshaped and tiled to have shape ``shape``.
"""
with tf.name_scope('broadcast') as scope:
shape_x = tf.shape(x)
rank_x = tf.shape(shape0)[0]
shape_t = tf.convert_to_tensor(shape, preferred_dtype=tf.int32)
rank_t = tf.shape(shape1)[0]
with tf.control_dependencies([tf.Assert(
rank_t >= rank_x,
['len(shape) must be >= tf.rank(x)', shape_x, shape_t],
summarize=255
)]):
missing_dims = tf.ones(tf.stack([rank_t - rank_x], 0), tf.int32)
shape_x_ = tf.concat([missing_dims, shape_x], 0)
should_tile = tf.equal(shape_x_, 1)
with tf.control_dependencies([tf.Assert(
tf.reduce_all(tf.logical_or(tf.equal(shape_x_, shape_t), should_tile),
['cannot broadcast shapes', shape_x, shape_t],
summarize=255
)]):
multiples = tf.where(should_tile, shape_t, tf.ones_like(shape_t))
out = tf.tile(tf.reshape(x, shape_x_), multiples, name=scope)
try:
out.set_shape(shape)
except:
pass
return out
A = tf.random_normal([20, 100, 10])
B = tf.random_normal([20, 100, 10])
C = tf.random_normal([20, 100, 1])
C = broadcast(C, A.shape)
D = tf.select(C, A, B)
答案 3 :(得分:0)
在最新的Tensorflow版本(2.0)中,您可以按以下方式使用tf.broadcast_to
:
import tensorflow as tf
A = tf.random_normal([20, 100, 10])
B = tf.random_normal([20, 100, 10])
C = tf.random_normal([20, 100, 1])
C = tf.greater_equal(C, tf.zeros_like(C))
C = tf.broadcast_to(C, A.shape)
D = tf.where(C,A,B)