### Article catalog

Dataset:

minist handwritten data set, gray-scale image with numbers of 0-9 and size of 28 * 28. Implementation of loading dataset Code:

train_ts = tv.datasets.MNIST(root='./data', train=True, download=True, transform=transform) test_ts = tv.datasets.MNIST(root='./data', train=False, download=True, transform=transform) train_dl = DataLoader(train_ts, batch_size=32, shuffle=True, drop_last=False) test_dl = DataLoader(test_ts, batch_size=64, shuffle=True, drop_last=False)

Data preprocessing method:

transform = tv.transforms.Compose( [tv.transforms.ToTensor(), tv.transforms.Normalize((0.5,), (0.5,)), ])

among tv.transform.ToTensor () means to convert the pixel value of gray image from 0-255 to 0-1.

tv.transform.Normalize() is the subtraction of 0.5 from the input, divided by 0.5

# Infrastructure of the model

Input layer: 784 neurons

Hidden layer: 100 neurons

Output layer: 10 neurons

model = t.nn.Sequential( t.nn.Linear(784, 100), t.nn.ReLU(), t.nn.Linear(100, 10), t.nn.LogSoftmax(dim=1) )

# Loss function and optimization function

loss_fn = t.nn.NLLLoss(reduction="mean") optimizer = t.optim.Adam(model.parameters(), lr=1e-3)

# Training model

for s in range(5): print("run in step : %d"%s) for i, (x_train, y_train) in enumerate(train_dl): x_train = x_train.view(x_train.shape[0], -1) y_pred = model(x_train) train_loss = loss_fn(y_pred, y_train) if (i + 1) % 100 == 0: print(i + 1, train_loss.item()) model.zero_grad() train_loss.backward() optimizer.step()

# Test accuracy

total = 0; correct_count = 0 for test_images, test_labels in test_dl: for i in range(len(test_labels)): image = test_images[i].view(1, 784) with t.no_grad(): pred_labels = model(image) plabels = t.exp(pred_labels) probs = list(plabels.numpy()[0]) pred_label = probs.index(max(probs)) true_label = test_labels.numpy()[i] if pred_label == true_label: correct_count += 1 total += 1

# Run print accuracy and save model

print("total acc : %.2f\n"%(correct_count / total)) t.save(model, './nn_mnist_model.pt')

Basic knowledge points to understand: Using torch.nn The functions in the package build the network, and the model is saved as the. pt file and the loading and calling of the model. Torchvision.transforms To do data preprocessing. The Dataloader simple call processes the dataset.

This is the final result of the model operation:

run in step : 4 100 0.14458955824375153 200 0.3559816777706146 300 0.042216718196868896 400 0.01483279000967741 500 0.08254643529653549 600 0.03366608917713165 700 0.1758185774087906 800 0.24678932130336761 900 0.06122265011072159 1000 0.03310537710785866 1100 0.24489007890224457 1200 0.04772411286830902 1300 0.03609851747751236 1400 0.019260048866271973 1500 0.06729663163423538 1600 0.02728693000972271 1700 0.10883776843547821 1800 0.01690010167658329 total acc : 0.97

Set download=True, the data set is automatically downloaded, the model structure is very simple, so it is very suitable for people who do not know how to read the model to be the first contact model. In fact, they do not understand anything about the complex model. They debug and run the complex model by themselves, look at so many documents, and do not know what is written in the file. If an error is reported, they go to Baidu. After Baidu is finished, they still don't know anything Dao, basically speaking from my current foundation, I can't understand that I have not made progress in learning knowledge except baidu. I think I should have a foundation first. It's not impossible to rush for success, but I'm too stupid to fit me.