如何使用pytorch的nn.Module创建一个基础全连接层实例?

2026-06-09 20:373阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计441个文字,预计阅读时间需要2分钟。

如何使用pytorch的nn.Module创建一个基础全连接层实例?

pythonPython 3.7, 使用虚拟环境安装的PyTorch,随意搭建,不怕影响其他Python框架。

定义一个类Linear,继承nn.Module:

pythonimport torchimport nn

class Linear(nn.Module): def __init__(self): super(Linear, self).__init__() # 这里可以添加权重和偏置的初始化

def forward(self, x): # 这里实现前向传播 return x

python版本3.7,用的是虚拟环境安装的pytorch,这样随便折腾,不怕影响其他的python框架

1、先定义一个类Linear,继承nn.Module

import torch as t from torch import nn from torch.autograd import Variable as V class Linear(nn.Module): '''因为Variable自动求导,所以不需要实现backward()''' def __init__(self, in_features, out_features): super().__init__() self.w = nn.Parameter( t.randn( in_features, out_features ) ) #权重w 注意Parameter是一个特殊的Variable self.b = nn.Parameter( t.randn( out_features ) ) #偏值b def forward( self, x ): #参数 x 是一个Variable对象 x = x.mm( self.w ) return x + self.b.expand_as( x ) #让b的形状符合 输出的x的形状

2、验证一下

layer = Linear( 4,3 ) input = V ( t.randn( 2 ,4 ) )#包装一个Variable作为输入 out = layer( input ) out

#成功运行,结果如下:

tensor([[-2.1934, 2.5590, 4.0233], [ 1.1098, -3.8182, 0.1848]], grad_fn=<AddBackward0>)

下面利用Linear构造一个多层网络

class Perceptron( nn.Module ): def __init__( self,in_features, hidden_features, out_features ): super().__init__() self.layer1 = Linear( in_features , hidden_features ) self.layer2 = Linear( hidden_features, out_features ) def forward ( self ,x ): x = self.layer1( x ) x = t.sigmoid( x ) #用sigmoid()激活函数 return self.layer2( x )

测试一下

perceptron = Perceptron ( 5,3 ,1 ) for name,param in perceptron.named_parameters(): print( name, param.size() )

输出如预期:

如何使用pytorch的nn.Module创建一个基础全连接层实例?

layer1.w torch.Size([5, 3]) layer1.b torch.Size([3]) layer2.w torch.Size([3, 1]) layer2.b torch.Size([1])

以上这篇用pytorch的nn.Module构造简单全链接层实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

本文共计441个文字,预计阅读时间需要2分钟。

如何使用pytorch的nn.Module创建一个基础全连接层实例?

pythonPython 3.7, 使用虚拟环境安装的PyTorch,随意搭建,不怕影响其他Python框架。

定义一个类Linear,继承nn.Module:

pythonimport torchimport nn

class Linear(nn.Module): def __init__(self): super(Linear, self).__init__() # 这里可以添加权重和偏置的初始化

def forward(self, x): # 这里实现前向传播 return x

python版本3.7,用的是虚拟环境安装的pytorch,这样随便折腾,不怕影响其他的python框架

1、先定义一个类Linear,继承nn.Module

import torch as t from torch import nn from torch.autograd import Variable as V class Linear(nn.Module): '''因为Variable自动求导,所以不需要实现backward()''' def __init__(self, in_features, out_features): super().__init__() self.w = nn.Parameter( t.randn( in_features, out_features ) ) #权重w 注意Parameter是一个特殊的Variable self.b = nn.Parameter( t.randn( out_features ) ) #偏值b def forward( self, x ): #参数 x 是一个Variable对象 x = x.mm( self.w ) return x + self.b.expand_as( x ) #让b的形状符合 输出的x的形状

2、验证一下

layer = Linear( 4,3 ) input = V ( t.randn( 2 ,4 ) )#包装一个Variable作为输入 out = layer( input ) out

#成功运行,结果如下:

tensor([[-2.1934, 2.5590, 4.0233], [ 1.1098, -3.8182, 0.1848]], grad_fn=<AddBackward0>)

下面利用Linear构造一个多层网络

class Perceptron( nn.Module ): def __init__( self,in_features, hidden_features, out_features ): super().__init__() self.layer1 = Linear( in_features , hidden_features ) self.layer2 = Linear( hidden_features, out_features ) def forward ( self ,x ): x = self.layer1( x ) x = t.sigmoid( x ) #用sigmoid()激活函数 return self.layer2( x )

测试一下

perceptron = Perceptron ( 5,3 ,1 ) for name,param in perceptron.named_parameters(): print( name, param.size() )

输出如预期:

如何使用pytorch的nn.Module创建一个基础全连接层实例?

layer1.w torch.Size([5, 3]) layer1.b torch.Size([3]) layer2.w torch.Size([3, 1]) layer2.b torch.Size([1])

以上这篇用pytorch的nn.Module构造简单全链接层实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。