在PyTorch中,nn.functional()和nn.sequential()在计算效率上有什么区别吗?

您所在的位置:网站首页 pytorch的nnlinear 在PyTorch中,nn.functional()和nn.sequential()在计算效率上有什么区别吗?

在PyTorch中,nn.functional()和nn.sequential()在计算效率上有什么区别吗?

2023-04-18 03:33| 来源: 网络整理| 查看: 265

下面是一个使用PyTorch中的nn.functional()模块的前馈网络

import torch.nn as nn import torch.nn.functional as F class newNetwork(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(784, 128) self.fc2 = nn.Linear(128, 64) self.fc3 = nn.Linear(64,10) def forward(self,x): x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = F.softmax(self.fc3(x)) return x model = newNetwork() model

下面是相同的前馈-使用nn.sequential()模块构建相同的东西。两者之间的区别是什么?什么时候我应该使用一个而不是另一个?

input_size = 784 hidden_sizes = [128, 64] output_size = 10

构建前馈网络

model = nn.Sequential(nn.Linear(input_size, hidden_sizes[0]), nn.ReLU(), nn.Linear(hidden_sizes[0], hidden_sizes[1]), nn.ReLU(), nn.Linear(hidden_sizes[1], output_size), nn.Softmax(dim=1)) print(model)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3