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.
-
Programming Basics: Familiarity with at least one programming language, especially Python, because PyTorch is mainly written in Python.
-
Math fundamentals: Understand basic math knowledge such as linear algebra, probability and statistics, and calculus, which are the foundation for understanding and implementing machine learning algorithms.
-
Machine learning fundamentals: Understand basic machine learning concepts, such as supervised learning, unsupervised learning, reinforcement learning, and model evaluation metrics (accuracy, recall, F1 score, etc.).
-
Deep learning fundamentals: Be familiar with basic neural network concepts, including feedforward neural networks, convolutional neural networks (CNN), recurrent neural networks (RNN), long short-term memory networks (LSTM), etc.
-
Computer vision and natural language processing fundamentals: If you plan to apply PyTorch in these areas, understanding the relevant background knowledge will be very helpful.
-
Linux/Unix fundamentals: Although not required, understanding the basics of the Linux/Unix operating system can help you use command-line tools and scripts more effectively, especially in data preprocessing and model training.
-
English reading ability: Since many documents, tutorials, and community discussions are in English, having a certain level of English reading ability will help you learn better and solve problems.
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
(
"Tensor a:"
)
(
a
)
(
"Tensor b:"
)
(
b
)
Multiply element-wise and output the result
(
"Element-wise product of a and b:"
)
(
a * b
)
Output the sum of all elements of tensor a
(
"Sum of all elements of tensor a:"
)
(
a.
sum
(
)
)
Output the element at row 2, column 3 of tensor a (note that indexing starts at 0)
(
"Element at row 2, column 3 of tensor a:"
)
(
a
[
1
,
2
]
)
Output the maximum value in tensor a
(
"Maximum value in tensor a:"
)
(
a.
max
(
)
)
Create a tensor:
torch.randn(2, 3)Create a tensor with 2 rows and 3 columns, filled with random numbers (following a normal distribution).device=deviceanddtype=dtypeThe computing device (CPU or GPU) and data type (floating point) are specified separately.
Tensor operations:
a * b: Element-wise multiplication.a.sum(): Compute the tensorasum of all elements.a[1, 2]: Access the tensoraelement at row 2, column 3 (note that indexing starts at 0).a.max(): Get the tensoramaximum value in it.
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