欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > 6、PyTorch中搭建分类网络实例

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

2025/2/24 7:41:06 来源:https://blog.csdn.net/scar2016/article/details/143945187  浏览:    关键词: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. 代码测试

import torch
from torch import nn
from torch.nn import Moduletorch.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 yif __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")
  • 结果:
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>)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词