-
Notifications
You must be signed in to change notification settings - Fork 435
/
06-dropout-simple.py
28 lines (22 loc) · 959 Bytes
/
06-dropout-simple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import os
import sys
import torch
from d2l import torch as d2l
from torch import nn
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
def init_weights(m):
if type(m) == nn.Linear:
nn.init.normal_(m.weight, std=0.01)
num_inputs, num_outputs, num_hidden1, num_hidden2 = 784, 10, 256, 256
dropout1, dropout2 = 0.2, 0.5
net = nn.Sequential(nn.Flatten(), nn.Linear(784, 256), nn.ReLU(),
# 在第一个全连接层之后添加一个dropout层
nn.Dropout(dropout1), nn.Linear(256, 256), nn.ReLU(),
# 在第二个全连接层之后添加一个dropout层
nn.Dropout(dropout2), nn.Linear(256, 10))
net.apply(init_weights)
num_epochs, lr, batch_size = 10, 0.5, 256
loss = nn.CrossEntropyLoss()
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
trainer = torch.optim.SGD(net.parameters(), lr=lr)
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)