imperative programming (dynamic graph)

paddle.fluid.dygraph.to_variable(value, name=None, zero_copy=None)[source]

The API will create a Variable or ComplexVariable object from numpy.ndarray, Variable or ComplexVariable object.

  • value (ndarray|Variable|ComplexVariable) – The numpy.ndarray, Variable or ComplexVariable object that needs to be converted, it can be multi-dimension, and the data type is one of numpy.{float16, float32, float64, int16, int32, int64, uint8, uint16, complex64, complex128}.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name .

  • zero_copy (bool, optional) – Whether to share memory with the input numpy array. This parameter only works with CPUPlace and will be set to True when it is None. Default: None.


If value is a numpy.ndarray object, return Tensor created from the specified numpy.ndarray object, which has same data type and shape with value. If value is a Variable or ComplexVariable object, just return value.

Return type

Variable or ComplexVariable


import numpy as np
import paddle.fluid as fluid

with fluid.dygraph.guard(fluid.CPUPlace()):
    x = np.ones([2, 2], np.float32)
    y = fluid.dygraph.to_variable(x, zero_copy=False)
    x[0][0] = -1
    y[0][0].numpy()  # array([1.], dtype=float32)
    y = fluid.dygraph.to_variable(x)
    x[0][0] = 0
    y[0][0].numpy()  # array([0.], dtype=float32)
    c = np.array([2+1j, 2])
    z = fluid.dygraph.to_variable(c)
    z.numpy() # array([2.+1.j, 2.+0.j])
    z.dtype # 'complex128'