Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize pytorch Multilayer Perceptron

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to implement pytorch multilayer perceptron". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to implement pytorch multilayer perceptron"!

directory

initialize the model parameters

activation function

model

loss function

training

Now that we have mathematically described the multilayer perceptron, let us try to implement one ourselves. To compare with the results we obtained earlier using softmax regression, we will continue to use the Fashion-MNIST image classification dataset.

import torch from torch import nnfrom d2l import torch as d2lbatch_size = 256train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) Initialize model parameters

Each image in Fashion-MNIST consists of 28 × 28 = 784 grayscale image values. All images are divided into 10 categories. Ignoring the spatial structure between pixels, we can think of each image as a simple classification dataset of 784 input features and 10 classes.

First, we will implement a multilayer perceptron with a single hidden layer, which contains 256 hidden units. Note that we can think of both quantities as hyperparameters. Usually, we choose several powers of 2 as the width of the layer.

We express our parameters in terms of several tensors. Note that for each layer we need to record a weight matrix and an offset vector. As before, we allocate memory for the loss gradients of these parameters.

num_inputs, num_outputs, num_hiddens = 784, 10, 256W1 = nn.Parameter (torch.randn(num_inputs, num_hiddens, requires_grad=True) * 0.01)b1 = nn.Parameter (torch.zeros(num_hiddens, requires_grad=True))W2 = nn.Parameter (torch.randn(num_hiddens, num_outputs, requires_grad=True) * 0.01)b2 = nn.Parameter (torch.zeros(num_outputs, requires_grad=True))params = [W1, b1, W2, b2] activation function

To make sure we know how everything works, we will implement the ReLU activation function ourselves using the max function instead of calling the built-in relu function directly.

def relu(X): a = torch.zeros_like(X) return torch.max(X, a) model

Because we ignore spatial structure, we use reshape to convert each 2D image into a vector of length num_inputs as shown. We only need a few lines of code to implement our model.

def net(X): X = X.reshape((-1, num_inputs)) H = relu(X@W1 + b1) #where "@" stands for matrix multiplication return (H@W2 + b2) loss function

To ensure numerical stability, and since we have implemented the softmax function from zero, here we use the built-in functions in the advanced API directly to calculate softmax and cross-entropy loss.

loss = nn.CrossEntropyLoss() Training

Fortunately, the training process for multilayer perceptrons is exactly the same as for softmax regression. You can call the train_ch4 function of the d2l package directly, setting the iteration period to 10 and the learning rate to 0.1.

num_epochs, lr = 10, 0.1updater = torch.optim.SGD(params, lr=lr)d2l.train_ch4(net, train_iter, test_iter, loss, num_epochs, updater)

To evaluate the learned model, we will apply the model to some test data.

d2l.predict_ch4(net, test_iter)

At this point, I believe everyone has a deeper understanding of "how to realize pytorch multilayer perceptron," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report