Location>code7788 >text

Learning Artificial Intelligence from Zero - Python-Pytorch Learning (VII)

Popularity:871 ℃/2024-08-22 09:41:11

preamble

This article focuses on the second half of neural networks.
It's really just a neural network training session that combines all of the previous learning.

neural network

Here is the neural network training and use for handwritten digit recognition using MNIST dataset.
MNIST dataset, a commonly used dataset for handwritten digit recognition.The MNIST dataset contains 60,000 28x28 pixel grayscale training images and 10,000 test images, each representing a handwritten digit (0-9).

import torch
import as nn
import torchvision
import as transforms
import as plt
# device config
device = ('cuda' if .is_available() else 'cpu')
# hyper parameters
input_size = 784 # 28x28
hidden_size = 100
num_classes = 10
batch_size = 100
learning_rate = 0.001


# MNIST
# : This is a class for loading MNIST dataset. MNIST dataset which contains grayscale images of handwritten numbers. The size of each image is 28x28 pixels and the grayscale image has only one channel (channels=1)
# root='. /data': The root parameter specifies where the dataset is stored '. /data' denotes a relative path that indicates the dataset will be stored in the data folder in the current working directory. If this folder does not exist, PyTorch will create it automatically.
# train=True: Indicates that the training set is loaded.
# transform=(): Transform the image into a PyTorch tensor normalized to the range [0, 1].
# download=True: Automatically downloads the MNIST dataset from the Internet if no dataset is found under the specified root path.
train_dataset = (
    root='. /data', train=True, transform=(), download=True)
test_dataset = (
    root='. /data', train=False, transform=())
# It's a built-in dataset, so you don't have to make a csv file like in the previous section.
# Here we import MNIST directly into the DataLoader.
# batch_size specifies the amount of data to be imported into the model at one time. Specify batch_size as 100, that's 100 reads in one batch, using the index of the dataset can be read, because there is another parameter below shuffle=True, so the data is disrupted when reading.
train_loader = . (
    dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = (
    dataset=test_dataset, batch_size=batch_size, shuffle=False)
print('100 per batch, divided into more than one batch',len(train_loader))

examples = iter(train_loader) # Convert to an iterator so that you can call next, line by line, to get the data, except that his line, is a set of data
samples, labels = examples.__next__() # here take out x and y
print(, ) # samples i.e. x, is a batch, i.e. 100 images
# Here the output is ([100, 1, 28, 28]) ([100])
# Where x is the dimension of the data is something like the following.
# The first dimension (64): indicates the number of samples included in the batch, i.e. batch_size. in this example, there are 100 images entered into the model at a time.
# The second dimension (1): indicates the number of channels in the image. For grayscale images, the number of channels is 1, while color images usually have 3 channels (corresponding to RGB).
# The third dimension (28): represents the height of the image; MNIST images have a height of 28 pixels.
# The fourth dimension (28): represents the width of the image; MNIST images are also 28 pixels wide.
# y has only one dimension, which is 100 images

# The data in x are all handwritten numbers, so you can show them in an image to see how they look
for i in range(6).
    (2, 3, i+1) # Create a 2-row, 3-column subgraph layout in the image window, and select the i+1st subgraph position.
    (samples[i][0], cmap='gray')
    # ()


class NeuralNet().
    def __init__(self, input_size, hidden_size, num_classes).
        super(NeuralNet, self). __init__()
        self.linear1 = (input_size, hidden_size)
         self.linear1 = (input_size, hidden_size) = ()
        self.linear2 = (hidden_size, num_classes)

    def forward(self, x).
        out = self.linear1(x)
        out = (out)
        out = self.linear2(out)
        # no softmax at the end
        return out


model = NeuralNet(input_size=input_size, hidden_size=hidden_size, num_classes=num_classes)
                  hidden_size=hidden_size, num_classes=num_classes)
criterion = () # (applies Softmax) The activation function is called here, so it's not called above

optimizer = ((), lr=learning_rate)
# training loop
n_total_steps = len(train_loader)
num_epochs = 2
# The following loop goes twice, meaning that after training all the data in the set, it starts over again
for epoch in range(num_epochs): #for--range pattern=for in other languages
    #The following loop trains on all the data in the collection.
    for i, (images, labels) in enumerate(train_loader): #for--enumerate pattern=foreach in other languages
        # Here images is 100 images, i.e. one batch.

        # Convert the 4-dimensional array 100, 1, 28, 28 to a 2-dimensional array, the result should be 100, 784.
        # to(device) means to transfer the data to this device for computation, if you have a GPU, this computation will be accelerated
        images = (-1, 28*28).to(device)
        labels = (device)
        # forward
        outputs = model(images)
        loss = criterion(outputs, labels)
        # backwards
        optimizer.zero_grad()
        ()
        ()
        if (i+1) % 100 == 0.
            print(
                f'epoch {epoch+1} / {num_epochs}, step {i+1}/{n_total_steps}, loss = {}')
# test
with torch.no_grad():
    n_correct = 0
    n_samples = 0
    for images, labels in test_loader.
        images = (-1, 28*28).to(device) # to 2D array
        labels = (device)
        outputs = model(images) # With our trained model, we get y_predicted

        # value,index
        _, predictions = (outputs, 1) #(outputs, 1) will find the maximum value and its index in each row of outputs (corresponding to each sample). Since the model outputs a probability distribution for each category, the index of the maximum value represents the model's predicted category for that image.
        n_samples += [0] #[0] will return the number of rows of y, which is 100, since a batch of 100 images
        print("number of rows of y",[0])
        #predictions == labels generates a boolean tensor (True means correct prediction, False means wrong prediction)
        #sum() counts the number of correct predictions and adds it to n_correct
        n_correct += (predictions == labels).sum().item()

acc = 100.0*n_correct/n_samples #calculate the correctness rate
print(f'accuracy ={acc}')

depiction

Now that we've learned to use neural network development, let's take a look at some graphs that make sense.
For example, this M-P neuron model.

In for example this neural network structure diagram.
The pink color below is the input layer, the green color is the hidden layer, and the blue color is the output layer. Although the hidden layer nodes drawn below are more than the input layer, this is not necessarily the case in reality, this is just a schematic, for example, above us, the input x is 784 columns, the hidden layer calculates that there are 100 columns left.
image

concluding remarks

Essentially I'm not a python programmer, in fact you should be able to get a feel for it by looking at my comments, for example I comment all my python for loops.
The reason I'm writing this series is that I'm not a python developer, and this series is for jogging dead memories when I go back to using python after a super long interval.
However, I feel like I wrote it in a pretty good order, and if people read it carefully over and over again, they should be able to master neural network development as well.


Portal:
Learning Artificial Intelligence from Zero - Python-Pytorch Learning (I)
Learning Artificial Intelligence from Zero - Python-Pytorch Learning (II)
Learning Artificial Intelligence from Zero - Python-Pytorch Learning (III)
Learning Artificial Intelligence from Zero - Python-Pytorch Learning (IV)
Learning Artificial Intelligence from Zero - Python-Pytorch Learning (V)
Learning Artificial Intelligence from Zero - Python-Pytorch Learning (VI)
Learning Artificial Intelligence from Zero - Python-Pytorch Learning (VII)


Note: This post is original, please contact the author for authorization and attribution for any form of reproduction!



If you think this article is still good, please click [Recommend] below, thank you very much!

/kiba/p/18372411