pytorch 基础知识2.0

pytorch 基础知识2.0

1统计相关的函数

import torch
import pandas as pd
import numpy as np
# 1. torch.mean() 求均值
a = torch.tensor([4,5,6,8,1,5,6],dtype = torch.float)
print(torch.mean(a))
tensor(5.)
# 2. torch.sum()  求和
print(torch.sum(a))
tensor(35.)
# 3. torch.prod() 求所有元素的积
print(torch.prod(a))
tensor(28800.)
# 4. torch.max() 求最大值
print(torch.max(a))
tensor(8.)
# 5. torch.min() 求最小值
print(torch.min(a))
tensor(1.)
# 6. 获取最大值的下标 torch.argmax
print(a)
print(torch.argmax(a))
tensor([4., 5., 6., 8., 1., 5., 6.])
tensor(3)
# 7. 获取最小值的下标 torch.argmin()
print(torch.argmin(a))
tensor(4)
# 8. 求解一个矩阵/张量的标准差 torch.std()
print(a)
print(torch.std(a))
tensor([4., 5., 6., 8., 1., 5., 6.])
tensor(2.1602)
# 9. torch.var()求一个矩阵/张量的方差
print(torch.var(a))
tensor(4.6667)
# 10. torch.median() 求中位数
print(torch.median(a))
tensor(5.)
# 11. torch.mode()  求众数
print(torch.mode(a))
torch.return_types.mode(
values=tensor(5.),
indices=tensor(5))
# 12. torch.histc() 直方图
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.])
# 13. 定义随机种子,保证每次运行的结果都一样。torch.manual_seed()
print(torch.manual_seed(15))


# 14. torch.cat()组合/拼接,按照已经存在的维度进行拼接
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.])
# 15. torch.stack()组合/拼接,按照新的维度进行拼接
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.]])
# 16.torch.where(condition,x,y) :按照条件从x和y中选出满足条件的元素组成新的tensor
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]))
# 17. torch.gather(input, dim, index, otu = None)
# 指定维度上按照索引值输出tensor
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]])
# 18. torch.index_select(input, dim, index, out=None)
# 按照指定索引输出tensor
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.]])
# 19. Tensor和tensor的区别
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])
# 20. torch.masked_select(input, mask, out=None)
# 按照mask输出tensor,输出为向量
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])
# 21. torch.take(input, indices)  
# 将输入看成ID-tensor,按照索引得到输出tensor
src = torch.tensor([[4,5,6],
                   [7,8,9]])
print(torch.take(src, torch.tensor([0,2,4])))
tensor([4, 6, 8])
# 22. tensor 切片: tensor.chunk(tensor, chunks, dim = 0)
# 按照某个维度平均分块(最后一个块可能小于平均值)
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)  # 按照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]]))
#  23. torch.split(tensor, split_size_or_sections, dim = 0)
# 按照某个维度依照第二个参数给出的list或者int进行分割tensor

# 第二个参数有两种形式
# 第一种是分割分数,和torch。chunk()一样
# 第二种是分割方案,这是一个list,带分割的张量将会分割为len(list)分,没一份的大小取决于list中的元素

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]]))
# 24. torch.t(input) 只针对2d tensor转置
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]])
# 25. torch.transpose(input, dim0,dim1)  交换两个维度
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]])
# 26. torch.squeeze()  这个函数主要对数据进行压缩,去掉维度为1的维度
a = torch.tensor([[1],
                 [4],
                 [7]])
print(torch.squeeze(a))
tensor([1, 4, 7])
# 27. torch.unsqueeze() 这个函数主要对数据维度进行扩充,给指定的位置加上一个为1的维度
a = torch.tensor([1,2,3,4])
print(torch.unsqueeze(a,1))
tensor([[1],
        [2],
        [3],
        [4]])
# 28. torch.unbind(tensor, dim = 0 )  去除一个维度
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]))
# 29. torch.flip(input, dims) 按照给定的维度进行翻转
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]])
# 30. torch.rot90(input, k, dims)  按照指定的维度和旋转次数进行张量旋转
# k: 旋转次数
# dims; 那些恶维度需要选装
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]])

你可能感兴趣的:(pytorch,python,深度学习)