2.0.PyTorch神经网络基础-自定义层

​ 构造一个没有任何参数的自定义层,

import torch
import torch.nn.functional as F
from torch import nn

class CenteredLayer(nn.Module):
	def __init__(self):
		super.__init__()
		
	def forward(self,X):
		return X- X.mean()
		
layer = CenteredLayer()
layer = (torch.FloatTensor([1,2,3,4,5]))

#最后结果是tensor([-2.,-1.,0.,1.,2.])

#将层作为组件合并到更复杂的模型中
net = nn.Sequential(nn.Linear(8,128),CenteredLayer())

Y = net(torch.rand(4,8))
Y.mean()
#结果为 tensor(-2.7940e-09,grad_fn=<MeanBackward0>)

带参数的图层

class MyLinear(nn.Module):
	def __init__(self,in_units,units):#有两个参数,输入维度,输出维度
		super().__init__()
		self.weight = nn.Parameter(torch.randn(in_units,units))#正态分布初始化
		self.bias = nn.Parameter(torch.randn(units,))
	def forward(self.X):
		linear = torch.matmul(X,self.weight.data)+self.bias.data
    return F.rulu(linear)
  
dense = MyLinear(5,3)
print(dense.weight)
上一篇:MySQL(2)库的操作- 1.创建数据库:


下一篇:锐捷开机自联网服务