知识卡片
模块初始化与前向各有职责
继承nn.Module,在init中登记Linear等子模块及参数,在forward中定义从输入到输出的计算。自动求导随后可沿本次计算图求梯度;前向定义不是优化步骤,也不是仅写了模块就已经学到参数。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
- · 神经网络基础
原书出处 · 1 处
- · 查看原书依据
>>> class LogisticRegressionNN(torch.nn.Module): ... def __init__(self, num_features, num_outputs=1): ... super().__init__() ... self.linear = torch.nn.Linear(num_features, num_outputs) ... def forward(self, X): ... return torch.sigmoid(self.linear(X)) >>> model = LogisticRegressionNN(num_features=vecs.shape[1], num_outputs=1) >>> model LogisticRegressionNN( (linear): Linear(in_features=3663, out_features=1, bias=True) ) Let’s see what happened here. Our model is a class that extends the PyTorch class used to define neural networks, torch.nn.Module. As with every Python class, it has a constructor
参考来源
- 位置:《Natural Language Processing in Action, Second Edition》第05章《神经网络基础》(来源项目:a19c7933-4eda-4176-aebd-30fe7e1ad292;源文件:extracted/Natural Language Processing in Action, Second Edition/_epub-src/OEBPS/Text/chapter-5.html)
- 结论依据:摘录中关于模块初始化与前向各有职责的定义、比较或条件支撑卡片;原书表述不严谨的范围已在覆盖记录逐项限定,不把案例结果外推为普遍结论。
- 原始内容:>>> class LogisticRegressionNN(torch.nn.Module): ... def __init__(self, num_features, num_outputs=1): ... super().__init__() ... self.linear = torch.nn.Linear(num_features, num_outputs) ... def forward(self, X): ... return torch.sigmoid(self.linear(X)) >>> model = LogisticRegressionNN(num_features=vecs.shape[1], num_outputs=1) >>> model LogisticRegressionNN( (linear): Linear(in_features=3663, out_features=1, bias=True) ) Let’s see what happened here. Our model is a class that extends the PyTorch class used to define neural networks, torch.nn.Module. As with every Python class, it has a constructor