02pytorch基础语法

This commit is contained in:
luzhisheng 2023-04-21 15:15:07 +08:00
parent 07e12b7625
commit 6667a6960b
16 changed files with 151 additions and 0 deletions

View File

@ -0,0 +1,5 @@
import torch
# 在100~1000中随机整数
t4 = torch.FloatTensor(2, 3, 4)
print(t4)

View File

@ -0,0 +1,4 @@
import torch
t4 = torch.empty((2, 2, 3))
print(t4)

View File

@ -0,0 +1,4 @@
import torch
t4 = torch.ones((2, 2, 3))
print(t4)

View File

@ -0,0 +1,5 @@
import torch
# 在100~1000中随机整数
t4 = torch.randint(low=100, high=1000, size=(2, 3, 4))
print(t4)

View File

@ -0,0 +1,4 @@
import torch
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)

View File

@ -0,0 +1,13 @@
import torch
import numpy as np
list_1 = [
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
]
print(list_1)
print(np.array(list_1))
# 张量
print(torch.tensor(list_1))

View File

@ -0,0 +1,8 @@
import torch
t5 = torch.randint(low=100, high=1000, size=(2, 3, 4))
print(t5.numpy())
t6 = torch.tensor([[[[[[[[[[[100]]]]]]]]]]])
print(t6.item())

View File

@ -0,0 +1,17 @@
import torch
list_8 = [[[792, 436, 928, 303],
[809, 170, 778, 652],
[967, 520, 419, 184]],
[[402, 754, 327, 979],
[713, 926, 879, 934],
[540, 953, 209, 369]]]
t8 = torch.tensor(list_8)
# 合并成单列单行
print(t8.view(-1))
# 拉升成4行6列
print(t8.view(4, 6))

View File

@ -0,0 +1,17 @@
import torch
list_7 = [
[792, 436, 928, 303],
[809, 170, 778, 652],
[967, 520, 419, 184]
]
t7 = torch.tensor(list_7)
# 求最大值
print(t7.max())
# 列求最大值
print(t7.max(dim=0))
# 行求最大值
print(t7.max(dim=1))

View File

@ -0,0 +1,14 @@
import torch
list_10 = [[[792, 436, 928, 303],
[809, 170, 778, 652],
[967, 520, 419, 184]],
[[402, 754, 327, 979],
[713, 926, 879, 934],
[540, 953, 209, 369]]]
t10 = torch.tensor(list_10)
# 切第一个维度为1其他维度不切
print(t10[1:, :, :])
print(t10[1:, 2:, 3].item())

View File

@ -0,0 +1,11 @@
import torch
list_10 = [[[792, 436, 928, 303],
[809, 170, 778, 652],
[967, 520, 419, 184]],
[[402, 754, 327, 979],
[713, 926, 879, 934],
[540, 953, 209, 369]]]
t10 = torch.tensor(list_10)
print(t10*100)

View File

@ -0,0 +1,17 @@
import torch
list_9 = [[402, 754, 327, 979],
[713, 926, 879, 934],
[540, 953, 209, 369]]
t9 = torch.tensor(list_9)
# 轴交换,列变行,行变成列
print(t9.size())
# 1和0代表维度顺序
t9 = t9.permute(1, 0)
print(t9)
print(t9.size())
# 原维度是(2, 3, 4),改变下标顺序(1, 0, 2),变成了([3, 2, 4])
t10 = torch.randint(low=100, high=1000, size=(2, 3, 4))
print(t10.permute(1, 0, 2).size())

View File

@ -0,0 +1,9 @@
import torch
list_2 = [
[127, 128, 129, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
]
t2 = torch.tensor(list_2, dtype=torch.int8) # [-128 - 127]
print(t2)

View File

@ -0,0 +1,12 @@
import torch
list_2 = [
[127, 128, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
]
t2 = torch.tensor(list_2) # [-128 - 127]
print(t2.int())
print(t2.double())
print(t2.double().dtype)

View File

@ -0,0 +1,11 @@
import torch
list_3 = [
[127, 128, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
]
t3 = torch.tensor(list_3) # [-128 - 127]
print(t3.shape)
print(t3.size())