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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| import torch from torch import nn from d2l import torch as d2l import matplotlib.pyplot as plt
def droppot_layer(X,dropput): assert 0 <= dropput <= 1 if dropput == 0: return X if dropput == 1: return torch.zeros_like(X) mask = (torch.randn(X.shape) > dropput).float()
return mask * X / (1.0 - dropput)
class Net(nn.Module): def __init__(self,num_inputs, num_outputs, num_hiddens1, num_hidden2, dropout1, dropout2): super(Net, self).__init__() self.flatten = nn.Flatten() self.linear1 = nn.Linear(num_inputs, num_hiddens1) self.linear2 = nn.Linear(num_hiddens1, num_hidden2) self.linear3 = nn.Linear(num_hidden2, num_outputs) self.relu = nn.ReLU() self.dropout1 = dropout1 self.dropout2 = dropout2
def forward(self, X): H1 = torch.relu(self.linear1(self.flatten(X.reshape((-1, num_inputs))))) if self.training: H1 = droppot_layer(H1, dropout1) H2 = self.relu(self.linear2(H1)) if self.training: H2 = droppot_layer(H2, dropout2) out = self.linear3(H2) return out
if __name__ == "__main__": X = torch.arange(784, dtype=torch.float32).reshape((28, 28)) num_inputs, num_outputs, num_hiddens1, num_hidden2 = 784,10,256,256 dropout1, dropout2 = 0.2, 0.5
net = Net(num_inputs, num_outputs, num_hiddens1, num_hidden2, dropout1, dropout2) num_epochs, lr, batch_size = 10, 0.1, 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 ) plt.show() print(net(X))
|