Tensorflow 中的广播是视图还是副本?
Is broadcasting in Tensorflow a view or a copy?
请说明在 Tensorflow 中广播是否会在广播时分配新的内存缓冲区。
在Tensorflow文档Introduction to Tensors - Broadcasting中,有一句话说(强调):
Most of the time, broadcasting is both time and space efficient, as the broadcast operation never materializes the expanded tensors in memory
然而在另一句话中它说:
Unlike a mathematical op, for example, broadcast_to does nothing special to save memory. Here, you are materializing the tensor.
print(tf.broadcast_to(tf.constant([1, 2, 3]), [3, 3]))
tf.broadcast_to表示是广播操作
Broadcast an array for a compatible shape.
那么根据上面的“广播操作永远不会实现内存中扩展的张量”的说法,它应该不会实现。
请帮助澄清文档的实际含义。
它说正常的广播操作从不实现内存中的扩展张量,因为时间和space效率。
x = tf.constant([1, 2, 3])
y = tf.constant(2)
print(x * y)
tf.Tensor([2 4 6], shape=(3,), dtype=int32)
但是如果我们想看看它在广播后的样子,那么我们使用 tf.broadcast_to
这当然需要具体化张量。
x = tf.constant([1, 2, 3, 4])
y = tf.broadcast_to(x, [3, 4])
print(y)
tf.Tensor(
[[1 2 3 4]
[1 2 3 4]
[1 2 3 4]], shape=(3, 4), dtype=int32)
When doing broadcasted operations such as multiplying a tensor by a scalar, broadcasting (usually) confers some time or space benefit, as the broadcasted tensor is never materialized.
However, broadcast_to does not carry with it any such benefits. The newly-created tensor takes the full memory of the broadcasted shape. (In a graph context, broadcast_to might be fused to subsequent operation and then be optimized away, however.)
请说明在 Tensorflow 中广播是否会在广播时分配新的内存缓冲区。
在Tensorflow文档Introduction to Tensors - Broadcasting中,有一句话说(强调):
Most of the time, broadcasting is both time and space efficient, as the broadcast operation never materializes the expanded tensors in memory
然而在另一句话中它说:
Unlike a mathematical op, for example, broadcast_to does nothing special to save memory. Here, you are materializing the tensor.
print(tf.broadcast_to(tf.constant([1, 2, 3]), [3, 3]))
tf.broadcast_to表示是广播操作
Broadcast an array for a compatible shape.
那么根据上面的“广播操作永远不会实现内存中扩展的张量”的说法,它应该不会实现。
请帮助澄清文档的实际含义。
它说正常的广播操作从不实现内存中的扩展张量,因为时间和space效率。
x = tf.constant([1, 2, 3])
y = tf.constant(2)
print(x * y)
tf.Tensor([2 4 6], shape=(3,), dtype=int32)
但是如果我们想看看它在广播后的样子,那么我们使用 tf.broadcast_to
这当然需要具体化张量。
x = tf.constant([1, 2, 3, 4])
y = tf.broadcast_to(x, [3, 4])
print(y)
tf.Tensor(
[[1 2 3 4]
[1 2 3 4]
[1 2 3 4]], shape=(3, 4), dtype=int32)
When doing broadcasted operations such as multiplying a tensor by a scalar, broadcasting (usually) confers some time or space benefit, as the broadcasted tensor is never materialized.
However, broadcast_to does not carry with it any such benefits. The newly-created tensor takes the full memory of the broadcasted shape. (In a graph context, broadcast_to might be fused to subsequent operation and then be optimized away, however.)