<jupyter_start><jupyter_text>从零开始<jupyter_code>def dropout_layer(X, dropout):
    assert 0 <= dropout <= 1

    if dropout == 1:
        return torch.zeros_like(X)
    if dropout == 0:
        return X
    mask = (torch.rand(X.shape) > dropout).float()
    return mask * X /(1-dropout)
X = torch.arange(16, dtype=torch.float32).reshape(2,8)
print(X)
print(dropout_layer(X, 0.5))
print(dropout_layer(X, 0.0))
input_dims, hidden1_dims, hidden2_dims, output_dims = 28*28, 256, 256, 10
dropout1, dropout2 = 0.2, 0.5

class Net(nn.Module):
    def __init__(self, input_dims, hidden1_dims, hidden2_dims, output_dims, is_training = True):
        super(Net, self).__init__()
        self.input_dims = input_dims
        self.training = is_training
        self.linear_1 = nn.Linear(input_dims, hidden1_dims)
        self.linear_2 = nn.Linear(hidden1_dims, hidden2_dims)
        self.linear_3 = nn.Linear(hidden2_dims, output_dims)
        self.relu = nn.ReLU()
    def forward(self, x):
        h1 = self.relu(self.linear_1(x.reshape(-1, self.input_dims)))
        if self.training == True:
            h1 = dropout_layer(h1, dropout1)
        h2 = self.relu(self.linear_2(h1))
        if self.training == True:
            h2 = dropout_layer(h2, dropout2)
        return self.linear_3(h2)
net = Net(input_dims, hidden1_dims, hidden2_dims, output_dims)
net
epochs, lr, batch_size = 10, 0.1, 256
loss = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr)
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
d2l.train_ch3(net, train_iter, test_iter, loss, epochs, optimizer)<jupyter_output><empty_output><jupyter_text>简洁实现<jupyter_code>input_dims, hidden1_dims, hidden2_dims, output_dims = 28*28, 256, 256, 10
dropout1, dropout2 = 0.2, 0.5
net = nn.Sequential(
    nn.Flatten(),
    nn.Linear(input_dims, hidden1_dims),
    nn.ReLU(),
    nn.Dropout(dropout1),
    nn.Linear(hidden1_dims, hidden2_dims),
    nn.ReLU(),
    nn.Dropout(dropout2),
    nn.Linear(hidden2_dims, output_dims)
)
net
epochs, lr, batch_size = 10, 0.1, 256
optimizer = optim.SGD(net.parameters(), lr)
loss = nn.CrossEntropyLoss()
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
d2l.train_ch3(net, train_iter, test_iter, loss, epochs, optimizer)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13026315789473683, 'fraction_numerical': 0.04649122807017544, 'mean_word_length': 4.00219298245614, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 10, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '147717', 'license': 'Apache-2.0', 'n_tokens_mistral': 947, 'n_tokens_neox': 925, 'n_words': 164, 'path': 'chapter 4 MLP/dropout.ipynb', 'repo_name': 'TLDX-XIONG/d2l-notebook', 'size': '2280'}