也就是使用一个阈值生成一个bool Tensorsor ,导入公式就行了。
代码:
import numpy as np
import torch
import torchvision
from torch.utils import data
from d2l import torch as d2l
from torchvision import transforms
import torch.nn as nn
batch_size = 256
def load_data_fashion_mnist(batch_size, resize=None):
"""下载Fashion-MNIST数据集,然后将其加载到内存中"""
trans = [transforms.ToTensor()]
if resize:
trans.insert(0, transforms.Resize(resize))
trans = transforms.Compose(trans)
mnist_train = torchvision.datasets.FashionMNIST(
root="./data", train=True, transform=trans, download=True)
mnist_test = torchvision.datasets.FashionMNIST(
root="./data", train=False, transform=trans, download=True)
return (data.DataLoader(mnist_train, batch_size, shuffle=True),
data.DataLoader(mnist_test, batch_size, shuffle=False))
train_iter, test_iter = load_data_fashion_mnist(batch_size)
def dropout_latyer(X,drop_rate):
assert 0. <= drop_rate <=1
if drop_rate == 1:
return torch.zeros_like(X)
if drop_rate == 0:
return X
mask = (torch.rand(X.shape) > drop_rate).float()
return mask*X / ( 1. - drop_rate )
class Net(nn.Module):
def __init__(self,num_inputs,num_output,is_train=True):
super(Net,self).__init__()
self.input_num = num_inputs
self.output_num = num_output
self.is_train = is_train
self.Liner1 = nn.Linear(in_features=self.input_num,out_features=256)
self.relu = nn.ReLU()
self.Liner2 = nn.Linear(in_features=256, out_features=128)
self.Liner3 = nn.Linear(128, out_features=num_output)
def forward(self,X):
X = X.reshape(-1,self.input_num)
X = self.relu( self.Liner1(X) )
X = dropout_latyer(X,.3)
X = self.relu(self.Liner2(X))
X = dropout_latyer(X, .3)
X = self.relu(self.Liner3(X))
return X
loss_function = nn.CrossEntropyLoss()
def accuracy(y_hat,y):
if y_hat.shape[1]>1 and len(y_hat.shape)>1:
pred = y_hat.argmax(axis=1)
cmp = pred.type(y.dtype) == y
return float(cmp.type(y.dtype).sum())
batch_size = batch_size
lr = 0.02
input_num = 784
output_num = 10
epochs = 10
model = Net(input_num,output_num)
optimizer = torch.optim.SGD(model.parameters(), lr=lr)
for epoch in range(epochs):
if model == torch.nn.Module:
model.train()
true_data,data_len = 0,0
true_data_t,data_len_t = 0,0
for X,y in train_iter:
optimizer.zero_grad()
preb = model(X)
l = loss_function(preb,y)
l.backward()
optimizer.step()
true_data += accuracy(preb, y)
data_len += len(X)
# 测试数据
if isinstance(model, torch.nn.Module):
model.eval()
with torch.no_grad():
for X, y in test_iter:
pre = model(X)
l = loss_function(pre, y)
true_data_t += accuracy(pre, y)
data_len_t += len(X)
print("epoch =", epoch + 1, ' train acc=', true_data / data_len, ' test acc=', true_data_t / data_len_t)
true_data, data_len = 0, 0
true_data_t, data_len_t = 0, 0# 测试数据
运行结果:
epoch = 1 train acc= 0.33418333333333333 test acc= 0.4582
epoch = 2 train acc= 0.5416666666666666 test acc= 0.5803
epoch = 3 train acc= 0.6120666666666666 test acc= 0.6236
epoch = 4 train acc= 0.64865 test acc= 0.6507
epoch = 5 train acc= 0.6743 test acc= 0.6707
epoch = 6 train acc= 0.6935833333333333 test acc= 0.6893
epoch = 7 train acc= 0.705 test acc= 0.6971
epoch = 8 train acc= 0.7126333333333333 test acc= 0.7051
epoch = 9 train acc= 0.7216333333333333 test acc= 0.7123
epoch = 10 train acc= 0.7278666666666667 test acc= 0.7188
```