335 Star 1.5K Fork 859

MindSpore / docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Dense.md 2.46 KB
一键复制 编辑 原始数据 按行查看 历史
luojianing 提交于 2023-07-21 15:16 . replace target=blank

比较与torch.nn.Linear的功能差异

查看源文件

torch.nn.Linear

class torch.nn.Linear(
    in_features,
    out_features,
    bias=True
)(input) -> Tensor

更多内容详见torch.nn.Linear

mindspore.nn.Dense

class mindspore.nn.Dense(
    in_channels,
    out_channels,
    weight_init='normal',
    bias_init='zeros',
    has_bias=True,
    activation=None
)(x) -> Tensor

更多内容详见mindspore.nn.Dense

差异对比

PyTorch:全连接层,实现矩阵相乘的运算。

MindSpore:MindSpore此API实现功能与PyTorch基本一致,而且可以在全连接层后添加激活函数。

分类 子类 PyTorch MindSpore 差异
参数 参数1 in_features in_channels 功能一致,参数名不同
参数2 out_features out_channels 功能一致,参数名不同
参数3 bias has_bias 功能一致,参数名不同
参数4 - weight_init 权重参数的初始化方法,PyTorch无此参数
参数5 - bias_init 偏置参数的初始化方法,PyTorch无此参数
参数6 - activation 应用于全连接层输出的激活函数,PyTorch无此参数
输入 单输入 input x 功能一致,参数名不同

代码示例

两API实现功能一致,用法相同。

# PyTorch
import torch
from torch import nn
import numpy as np

net = nn.Linear(3, 4)
x = torch.tensor(np.array([[180, 234, 154], [244, 48, 247]]), dtype=torch.float)
output = net(x)
print(output.detach().numpy().shape)
# (2, 4)

# MindSpore
import mindspore
from mindspore import Tensor, nn
import numpy as np

x = Tensor(np.array([[180, 234, 154], [244, 48, 247]]), mindspore.float32)
net = nn.Dense(3, 4)
output = net(x)
print(output.shape)
# (2, 4)
1
https://gitee.com/mindspore/docs.git
git@gitee.com:mindspore/docs.git
mindspore
docs
docs
r2.0

搜索帮助