pytorch 基础知识2.0
1统计相关的函数
import torch
import pandas as pd
import numpy as np
a = torch.tensor([4,5,6,8,1,5,6],dtype = torch.float)
print(torch.mean(a))
tensor(5.)
print(torch.sum(a))
tensor(35.)
print(torch.prod(a))
tensor(28800.)
print(torch.max(a))
tensor(8.)
print(torch.min(a))
tensor(1.)
print(a)
print(torch.argmax(a))
tensor([4., 5., 6., 8., 1., 5., 6.])
tensor(3)
print(torch.argmin(a))
tensor(4)
print(a)
print(torch.std(a))
tensor([4., 5., 6., 8., 1., 5., 6.])
tensor(2.1602)
print(torch.var(a))
tensor(4.6667)
print(torch.median(a))
tensor(5.)
print(torch.mode(a))
torch.return_types.mode(
values=tensor(5.),
indices=tensor(5))
print(torch.histc(a))
tensor([1., 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., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 2., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 2.,
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., 0., 0., 1.])
print(torch.manual_seed(15))
a1 = torch.tensor([1,2,3],dtype = torch.float)
a2 = torch.tensor([4,5,6],dtype = torch.float)
print(torch.cat([a1,a2],dim = 0))
tensor([1., 2., 3., 4., 5., 6.])
a1 = torch.tensor([1,2,3],dtype = torch.float)
a2 = torch.tensor([4,5,6],dtype = torch.float)
print(torch.stack([a1,a2],dim = 1))
tensor([[1., 4.],
[2., 5.],
[3., 6.]])
print(torch.stack([a1,a2],dim = 0))
tensor([[1., 2., 3.],
[4., 5., 6.]])
x = torch.tensor([[1,2,3,],
[4,5,6],
[7,8,9]])
print(torch.where(x>6))
(tensor([2, 2, 2]), tensor([0, 1, 2]))
b = torch.tensor([[1,2,3],
[4,5,6]])
index_1 = torch.LongTensor([[0,1],
[1,1]])
index_2 = torch.LongTensor([[0,1,1],
[0,0,0]])
print(torch.gather(b, dim = 1, index = index_1))
print(torch.gather(b, dim = 0, index = index_2))
tensor([[1, 2],
[5, 5]])
tensor([[1, 5, 6],
[1, 2, 3]])
a = torch.linspace(1, 12, steps = 12).view(3 , 4)
print(a)
tensor([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.]])
b = torch.index_select(a, 0, torch.tensor([0, 2]) )
print(b)
tensor([[ 1., 2., 3., 4.],
[ 9., 10., 11., 12.]])
b = torch.index_select(a, 1, torch.tensor([0, 2]) )
print(b)
tensor([[ 1., 3.],
[ 5., 7.],
[ 9., 11.]])
a = torch.Tensor([3,4])
b = torch.tensor([3,4])
print(a)
print(b)
print(a.type())
print(b.type())
tensor([3., 4.])
tensor([3, 4])
torch.FloatTensor
torch.LongTensor
c = torch.tensor(5.6)
print(c)
print(c.type())
tensor(5.6000)
torch.FloatTensor
c1 = torh.Tensor(5.6)
print(c1)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
C:\Users\Public\Documents\Wondershare\CreatorTemp/ipykernel_8120/1449584292.py in
----> 1 c1 = torh.Tensor(5.6)
2 print(c1)
NameError: name 'torh' is not defined
c2 = torch.Tensor([5.6])
print(c2)
tensor([5.6000])
x = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
mask = x.ge(5)
print(mask)
tensor([[False, False, False],
[False, True, True],
[ True, True, True]])
torch.masked_select(x, mask)
tensor([5, 6, 7, 8, 9])
src = torch.tensor([[4,5,6],
[7,8,9]])
print(torch.take(src, torch.tensor([0,2,4])))
tensor([4, 6, 8])
a = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
b = torch.tensor([[10,11,12]])
c = torch.cat([a, b], dim = 0)
print(c)
d = torch.chunk(c, 4,dim = 0)
print(d)
tensor([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
(tensor([[1, 2, 3]]), tensor([[4, 5, 6]]), tensor([[7, 8, 9]]), tensor([[10, 11, 12]]))
e = torch.split(a,2,dim = 0)
print(e)
(tensor([[1, 2, 3],
[4, 5, 6]]), tensor([[7, 8, 9]]))
f = torch.split(c, [1, 2, 1],dim = 0)
print(f)
(tensor([[1, 2, 3]]), tensor([[4, 5, 6],
[7, 8, 9]]), tensor([[10, 11, 12]]))
a = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
print(torch.t(a))
tensor([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
a = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
print(torch.transpose(a,0,1))
tensor([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
a = torch.tensor([[1],
[4],
[7]])
print(torch.squeeze(a))
tensor([1, 4, 7])
a = torch.tensor([1,2,3,4])
print(torch.unsqueeze(a,1))
tensor([[1],
[2],
[3],
[4]])
a = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
print(torch.unbind(a,dim = 0))
print(torch.unbind(a,dim = 1))
(tensor([1, 2, 3]), tensor([4, 5, 6]), tensor([7, 8, 9]))
(tensor([1, 4, 7]), tensor([2, 5, 8]), tensor([3, 6, 9]))
a = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
print(torch.flip(a, [0,1]))
tensor([[9, 8, 7],
[6, 5, 4],
[3, 2, 1]])
a = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
print(torch.rot90(a, 1, [0,1]))
tensor([[3, 6, 9],
[2, 5, 8],
[1, 4, 7]])
a = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
print(torch.rot90(a, 1, [0,1]))
tensor([[9, 8, 7],
[6, 5, 4],
[3, 2, 1]])
a = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
print(torch.rot90(a, 3, [0,1]))
tensor([[7, 4, 1],
[8, 5, 2],
[9, 6, 3]])
a = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
print(torch.rot90(a, 4, [0,1]))
tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])