/imgs/avatar.png

读写文件

加载和保存张量 import torch from torch import nn from torch.nn import functional as F x = torch.arange(4) torch.save(x, 'x-file') x2 = torch.load('x-file') 使用torch.save 和torch.load 来保存和读取张量,也可保存列表和字典 y =

自定义层

不带参数的层 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() 带参数的层 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.relu(linear) 注意使用nn.Param

参数管理

参数访问 import torch from torch import nn net = nn.Sequential(nn.Linear(4, 8), nn.ReLU(), nn.Linear(8, 1)) X = torch.rand(size=(2, 4)) print(net[2].state_dict()) OrderedDict([('weight', tensor([[-0.0427, -0.2939, -0.1894, 0.0220, -0.1709, -0.1522, -0.0334, -0.2263]])), ('bias', tensor([0.0887]))]) 使用state_dict()方法返回一个模型的参数设置,在这个例子中,我们

模型构建

层和块 层和块本质没有区别,都可以看作是一个黑盒子,有输入和输出,都可以当作单独的模块进行组合,组合之后的产物又是一个新的块。就像一本书一样,

第三周

学习时间 5月27日 到 6月2日 学习目标 学习第四章多层感知机内容 MLP 模型选择、过拟合 权重衰减 dropout 初始化 实战kaggle房价预测 论文精读-AlexNe