<jupyter_start><jupyter_text>模型构造<jupyter_code>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)
print(X)
net(X )
# 2是batch size   20是输入维度
# 自定义

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)))
net = MLP()
net(X)
nn.Linear(20, 256)(X).shape
class MySequential(nn.Module):
    def __init__(self, *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

net = MySequential(nn.Linear(20, 256), nn.ReLU(), nn.Linear(256, 10))
net(X)
# 正向传播中执行

class FixedHiddenMLP(nn.Module):
    def __init__(self):
        super().__init__()
        self.rand_weight = torch.rand((20,20),requires_grad=False) # 生成随机权重
        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()

net = FixedHiddenMLP()
net(X)
# 通过继承nn.Module  可以更灵活的前向计算 
# nn.Module 可以任意嵌套
# __init__内  外部类 都可以<jupyter_output><empty_output><jupyter_text>参数管理<jupyter_code># 单隐藏层的多层感知机
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))
net(X)
net[2].state_dict()
net[2].bias
net[2].bias.data
net[2].weight.grad == None
#  一次性访问所有参数
print(* [(name, param.shape) for name, param in net[0].named_parameters()])  #    * 将列表内每个元素打印
print(* [(name, param.shape) for name, param in net.named_parameters()])
#  net[1]是relu 是没有参数的
print(net.state_dict()["2.bias"].data)<jupyter_output>tensor([0.0651])<jupyter_text>嵌套块收集参数<jupyter_code>def block1():
    return nn.Sequential(nn.Linear(4, 8), nn.ReLU(), nn.Linear(8,4), nn.ReLU())

def block2():
    net = nn.Sequential()
    for i in range(4):
        net.add_module(f'block{i}', block1())
    return net
rgnet = nn.Sequential(block2(), nn.Linear(4, 1))
rgnet(X)
rgnet<jupyter_output><empty_output><jupyter_text>linear的初始化<jupyter_code>def init_normal(m):
    if type(m) == nn.Linear:
        nn.init.normal_(m.weight, mean=0, std=0.01) # 均值为0,方差为0.01的正态分布初始化
        nn.init.zeros_(m.bias)  # 偏移为0
net.apply(init_normal) # 和pandas 类似  每一层调用这个函数

net[0].weight.data[0], net[0].bias.data[0]
nn.ReLU == F.relu
nn.ReLU
# 参数绑定
shared = nn.Linear(8, 8)  # 出入一致即可

net = nn.Sequential(nn.Linear(4,8), nn.ReLU(), shared, nn.ReLU(), shared, nn.ReLU(), nn.Linear(8,2))

net(X)
net[2] == net[4]
#改变net[2] 或者 net[4]内参数 另一个也会随之改变 指向的同一个<jupyter_output><empty_output><jupyter_text>自定义层<jupyter_code># 任意嵌套<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.166497461928934, 'fraction_numerical': 0.03519458544839255, 'mean_word_length': 3.5829457364341084, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '593873', 'license': 'MIT', 'n_tokens_mistral': 1360, 'n_tokens_neox': 1346, 'n_words': 227, 'path': '16.ipynb', 'repo_name': 'chrzc/-', 'size': '2955'}