Created by: songyouwei
NumPy Bridge is used for zero-copy creation of a Tensor with a NumPy array.
zero_copy
is enabled by default when place is a CPUPlace
and is in dygraph mode.
The created Tensor and NumPy array will share their underlying memory locations (only when working with CPUPlace
), and change the numpy array will also change the corresponding Tensor.
Example:
import numpy as np
import paddle.fluid as fluid
data_np = np.array([[2, 3, 1]]).astype('float32')
with fluid.dygraph.guard(fluid.CPUPlace()):
var = fluid.dygraph.to_variable(data_np, zero_copy=True)
data_np[0][0] = 4
var[0][0].numpy()[0] == 4 # True