Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch Tutorial

PyTorch Tutorial

PyTorch is an open-source machine learning library, mainly used for research and development in areas such as computer vision (CV), natural language processing (NLP), and speech recognition.

PyTorch was developed by Facebook's AI research team and is widely used in the machine learning and deep learning communities.

PyTorch is known for its flexibility and ease of use, making it especially suitable for deep learning research and development.

Who should read this tutorial?

As long as you have basic programming knowledge, you can read this tutorial to learn PyTorch. It is suitable for those interested in deep learning and machine learning, including data scientists, engineers, researchers, and students.

Knowledge you need before reading this tutorial:

Before you start reading this tutorial, the basic knowledge you must have includes Python programming, basic mathematics (linear algebra, probability theory, calculus), fundamental concepts of machine learning, knowledge of neural networks, and a certain level of English reading ability to consult documentation and materials.

Example

Below are some basic tensor operations in PyTorch: how to create random tensors, perform element-wise operations, access specific elements, and compute the sum and maximum.

Example

import

torch

Set the data type and device

dtype

=

torch.

float

 

The tensor data type is floating point

device

=

torch.

device

(

"cpu"

)

 

This computation is performed on the CPU

Create and print two random tensors a and b

a

=

torch.

randn

(

2

,

3

,

device

=

device

,

dtype

=

dtype

)

 

Create a 2x3 random tensor

b

=

torch.

randn

(

2

,

3

,

device

=

device

,

dtype

=

dtype

)

 

Create another 2x3 random tensor

print

(

"Tensor a:"

)

print

(

a

)

print

(

"Tensor b:"

)

print

(

b

)

Multiply element-wise and output the result

print

(

"Element-wise product of a and b:"

)

print

(

a * b

)

Output the sum of all elements of tensor a

print

(

"Sum of all elements of tensor a:"

)

print

(

a.

sum

(

)

)

Output the element at row 2, column 3 of tensor a (note that indexing starts at 0)

print

(

"Element at row 2, column 3 of tensor a:"

)

print

(

a

[

1

,

2

]

)

Output the maximum value in tensor a

print

(

"Maximum value in tensor a:"

)

print

(

a.

max

(

)

)

Create a tensor:

Tensor operations:

Output: (values will differ on each run)

张量 a:
tensor([[-0.1460, -0.3490,  0.3705],
        [-1.1141,  0.7661,  1.0823]])

张量 b:
tensor([[ 0.6901, -0.9663,  0.3634],
        [-0.6538, -0.3728, -1.1323]])

a 和 b 的逐元素乘积:
tensor([[-0.1007,  0.3372,  0.1346],
        [ 0.7284, -0.2856, -1.2256]])

张量 a 所有元素的总和:
tensor(0.6097)

张量 a 第 2 行第 3 列的元素:
tensor(1.0823)

张量 a 中的最大值:
tensor(1.0823)

Reference Links

PyTorch official website:https://pytorch.org/

PyTorch official getting-started tutorial:https://pytorch.org/get-started/locally/

PyTorch official documentation:https://pytorch.org/docs/stable/index.html

PyTorch source code:https://github.com/pytorch/pytorch