assign

paddle.fluid.layers.assign(input, output=None)[源代码]

该OP将输入Tensor或numpy数组拷贝至输出Tensor。

参数:
  • input (Variable|np.ndarray) - 输入Tensor或numpy数组,支持数据类型为float32, float64, int32, int64和bool。
  • output (Variable,可选) - 输出Tensor。如果为None,则创建一个新的Tensor作为输出Tensor,默认值为None。

返回:输出Tensor,形状、数据类型、数据值和 input 一致。

返回类型:Variable

代码示例

import paddle.fluid as fluid
import numpy as np
data = fluid.layers.fill_constant(shape=[3, 2], value=2.5, dtype='float64') # [[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]]
result1 = fluid.layers.create_tensor(dtype='float64')
fluid.layers.assign(data, result1) # result1 = [[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]]
result2 = fluid.layers.assign(data)  # result2 = [[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]]
result3 = fluid.layers.assign(np.array([[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]], dtype='float32')) # result3 = [[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]]