比较与torch.nn.init.constant_的功能差异

查看源文件

torch.nn.init.constant_

torch.nn.init.constant_(
    tensor,
    val
)

更多内容详见torch.nn.init.constant_

mindspore.common.initializer.Constant

class mindspore.common.initializer.Constant(value)(arr)

更多内容详见mindspore.common.initializer.Constant

使用方式

PyTorch:以常量val填充输入的tensor。

MindSpore:以value(整型或numpy数组)填充输入的numpy数组,且是原地更新输入值。

代码示例

import mindspore
import torch
import numpy as np

# In MindSpore, fill a constant array with value(int or numpy array).
input_constant = np.array([1, 2, 3])
constant_init = mindspore.common.initializer.Constant(value=1)
out_constant = constant_init(input_constant)
print(input_constant)
# Out:
# [1 1 1]

# In torch, fill in the input tensor with constant val.
input_constant = np.array([1, 2, 3])
out_constant = torch.nn.init.constant_(
    tensor=torch.tensor(input_constant),
    val=1
)
print(out_constant)
# Out:
# tensor([1, 1, 1])