#16神经网络基础
import torch
from torch import nn
from torch.nn import functional as F
net = nn.Sequential(nn.Linear(20,256),nn.ReLU(),nn.Linear(256,10))
x = torch.rand(2,20)#2是批量大小 20是输入维度
print(net(x))
class MLP(nn.Module):
def __init__(self):
super().__init__()
self.hidden = nn.Linear(20,256)
self.out = nn.Linear(256,10)
def forward(self,x):
return self.out(F.relu(self.hidden(x)))
net1 = MLP()
print(net(x))
class MySequential(nn.Module):
def __init__(self,*args):#args是收集参数 相当于把若干个参数打包成一个来传入
super().__init__()
for block in args:
self._modules[block] = block
def forward(self,x):
for block in self._modules.values():
x = block(x)
return x
net2 = MySequential(nn.Linear(20,256),nn.ReLU(),nn.Linear(256,10))
print(net(x))
class FixedHiddenMLP(nn.Module):
def __init__(self):
super().__init__()
self.rand_weight = torch.rand((20,20),requires_grad=True)
self.linear = nn.Linear(20,20)
def forward(self,x):
x = self.linear(x)
x = F.relu(torch.mm(x,self.rand_weight)+1)
x = self.linear(x)
while x.abs().sum() > 1:
x /= 2
return x.sum()
net3 = FixedHiddenMLP()
print(net3(x))
#混合搭配各种组合块,的方法
class NestMLP(nn.Module):
def __init__(self):
super().__init__()
self.net = nn.Sequential(nn.Linear(20,64),nn.ReLU(),nn.Linear(64,32),nn.ReLU)
self.linear = nn.Linear(32,16)
def forward(self,x):
return self.linear(self.net(x))
chimera = nn.Sequential(NestMLP(),nn.linear(16,20),FixedHiddenMLP())
print(chimera(x))
16-模型构造 动手深度学习
最新推荐文章于 2024-09-09 23:51:40 发布