6、PyTorch中搭建分类网络实例

1. 重要类

  • nn.Module
  • nn.flatten
  • nn.linear
  • nn.relu
  • to.device
  • torch.cuda.is_available
  • nn.softmax
  • nn.argmax
  • nn.sequential
  • nn.conv2d
  • add_module
  • buffer
  • load_state_dict
  • named_parameters
  • requires_grad
  • save_check_points

2. 代码测试

python 复制代码
import torch
from torch import nn
from torch.nn import Module

torch.set_printoptions(precision=3)


class MyModelTest(Module):
    def __init__(self):
        super(MyModelTest, self).__init__()
        self.linear_1 = nn.Linear(3, 4)
        self.relu = nn.ReLU()
        self.linear_2 = nn.Linear(4, 5)

    def forward(self, x):
        x = self.linear_1(x)
        x = self.relu(x)
        y = self.linear_2(x)
        return y


if __name__ == "__main__":
    matrix = torch.arange(3,dtype=torch.float)
    my_softmax = nn.Softmax(dim=0)
    output = my_softmax(matrix)
    print(f"matrix=\n{matrix}")
    print(f"output=\n{output}")
    my_model = MyModelTest()
    for name, param in my_model.named_parameters():
        print(f"layer:{name}\n|size:{param.size()}\n|values:{param[:2]}\n")
  • 结果:
python 复制代码
matrix=
tensor([0., 1., 2.])
output=
tensor([0.090, 0.245, 0.665])
layer:linear_1.weight
|size:torch.Size([4, 3])
|values:tensor([[-0.544, -0.492,  0.190],
        [-0.424, -0.068,  0.134]], grad_fn=<SliceBackward0>)

layer:linear_1.bias
|size:torch.Size([4])
|values:tensor([0.295, 0.306], grad_fn=<SliceBackward0>)

layer:linear_2.weight
|size:torch.Size([5, 4])
|values:tensor([[ 0.489,  0.018,  0.314,  0.497],
        [ 0.364, -0.455,  0.047, -0.215]], grad_fn=<SliceBackward0>)

layer:linear_2.bias
|size:torch.Size([5])
|values:tensor([-0.027,  0.190], grad_fn=<SliceBackward0>)
相关推荐
冬奇Lab1 小时前
Agent 系列(23):Web Agent——让 Agent 真正浏览网页
人工智能·llm·agent
冬奇Lab1 小时前
每日一个开源项目(第135篇):codebase-memory-mcp - 给 AI Agent 一张代码库的知识图谱
人工智能·开源·llm
IT_陈寒4 小时前
JavaScript的闭包把我坑惨了,说好的内存会自动回收呢?
前端·人工智能·后端
jooloo8 小时前
Codex 间歇性 400 之谜:一条对话里,它为什么有时候用 chat/completions,有时候切到 responses?
人工智能
用户5191495848458 小时前
OpenSSL PKCS#12 PBMAC1 堆栈缓冲区溢出漏洞 (CVE-2025-11187) 分析与验证
人工智能·aigc
用户5191495848459 小时前
HP Sound Research SECOMNService 权限提升漏洞利用工具
人工智能·aigc
用户018349301699 小时前
给 AI 智能体能力包一层 BFF,前端只调一个接口
人工智能
这token有力气13 小时前
Function Calling 格式漂移
人工智能