SoFunction
Updated on 2024-11-21

PyTorch Installation and Basic Usage Details

What to learn PyTorch?

Some people always choose, choose the most people's frame, to use as their beginner's frame, such asTensorflow, but most of the papers' implementations are based on thePyTorchIf we want to go into the details of the thesis, we must choose to learn the introductoryPyTorch

Installing PyTorch

One line of command.official website

pip install torch===1.6.0 torchvision===0.7.0 - /whl/torch_stable.html

It's taking a while. Be patient.

Test yourself to see if the installation was successful

Run the command test

import torch
x = (5,3)
print(x)

exports

tensor([[0.5096, 0.1209, 0.7721],
        [0.9486, 0.8676, 0.2157],
        [0.0586, 0.3467, 0.5015],
        [0.9470, 0.5654, 0.9317],
        [0.2127, 0.2386, 0.0629]])

Start learning PyTorch

Creation without initializationtensor (math.)

import torch
x = ([5,5])
print(x)

exports

tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])

Randomly create a 0-1tensor (math.)

import torch
x = (5,5)
print(x)

exports

tensor([[0.3369, 0.5339, 0.8419, 0.6857, 0.6241],
        [0.4991, 0.1691, 0.8356, 0.4574, 0.0395],
        [0.9714, 0.2975, 0.9322, 0.5213, 0.8509],
        [0.3037, 0.8690, 0.3481, 0.2538, 0.9513],
        [0.0156, 0.9516, 0.3674, 0.1831, 0.6466]])

Creates an all-zerotensor (math.)

import torch
x = (5,5, dtype=torch.float32)
print(x)

The creation can be done with thedtypeSpecify the data type

exports

tensor([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])

Use the data to directly createtensor (math.)

import torch
x = ([5,5], dtype=torch.float32)
print(x)

exports

tensor([5., 5.])

Using the originaltensorCreate a newtensor

import torch
x = ([5,5], dtype=torch.float32)
x = x.new_zeros(5, 3)
y = torch.rand_like(x)
print(x)
print(y)

exports

tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
tensor([[0.5552, 0.3333, 0.0426],
        [0.3861, 0.3945, 0.6658],
        [0.6978, 0.3508, 0.4813],
        [0.8193, 0.2274, 0.8384],
        [0.9360, 0.9226, 0.1453]])

heedtensorThe dimensional information of the

x = (3,3)
()

exports

([3, 3])

Some simple arithmetic

x = ([1])
y = ([3])
'''
Mode 1
'''
z = x + y
'''
Mode 2
''' 
z = (x, y)
'''
Mode 3
'''
result = (1)
# No initialization of data
(x, y, out=result)
# Return the results to result
'''
Mode 4
'''
x.add_(y)

exports

tensor([4])

indexing operation

x = (5,5)
x[:,:]
x[1,:]
x[:,1]
x[1,1]

Separate outputs

tensor([[0.4012, 0.2604, 0.1720, 0.0996, 0.7806],
        [0.8734, 0.9087, 0.4828, 0.3543, 0.2375],
        [0.0924, 0.9040, 0.4408, 0.9758, 0.2250],
        [0.7179, 0.7244, 0.6165, 0.1142, 0.7363],
        [0.8504, 0.0391, 0.0753, 0.4530, 0.7372]])
tensor([0.8734, 0.9087, 0.4828, 0.3543, 0.2375])
tensor([0.2604, 0.9087, 0.9040, 0.7244, 0.0391])
tensor(0.9087)

(math.) a dimensional transformation

x = (4,4)
(16)
(8,2)
(-1,8)

Separate outputs

tensor([0.9277, 0.9547, 0.9487, 0.9841, 0.4114, 0.1693, 0.8691, 0.3954, 0.4679,
        0.7914, 0.7456, 0.0522, 0.0043, 0.2097, 0.5932, 0.9797])
tensor([[0.9277, 0.9547],
        [0.9487, 0.9841],
        [0.4114, 0.1693],
        [0.8691, 0.3954],
        [0.4679, 0.7914],
        [0.7456, 0.0522],
        [0.0043, 0.2097],
        [0.5932, 0.9797]])
tensor([[0.9277, 0.9547, 0.9487, 0.9841, 0.4114, 0.1693, 0.8691, 0.3954],
        [0.4679, 0.7914, 0.7456, 0.0522, 0.0043, 0.2097, 0.5932, 0.9797]])

Note: The number of dimensionally transformed data must be consistent.

This article on PyTorch installation and basic use of the article is introduced to this, more related to the installation and use of PyTorch content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!