Pytorch01-tensor

1. torch.Tensor:

1. 数据类型:

包含单一数据类型元素的多维矩阵

2. tensor类型的转化

将python的list或序列数据转化为Tensor,dtypetorch.FloatTensor

torch.tensor()

3. 改变torch.dtype和torch.device:

使用to()方法

torch.ones([2, 3], dtype=torch.float64, device='cuda:0')
# if use mps need to change to mps of device
# tensor([[1., 1., 1.], [1., 1., 1.]], device='cuda:0', dtype=torch.float64)

4. 访问tensor内容:

python索引或切片

索引:

matrix = torch.tensor([[2,3,4],[5,6,7]])
print(matrix[1][2])
# tensor(7)

切片:

matrix[1][2] = 9
print(matrix)
#tensor([[2, 3, 4], [5, 6, 9]])

5. 获取python number从只有一个值的tensor

torch.Tensor.item() 或者int()

x = torch.tensor([[4.5]])
x
# tensor([[4.5000]])
# get x
x.item()
# 4.5
int(x)
# 4

6. 自动求导

设定参数requires_grad=True,此时torch.autograd会记录相关运算实现自动求导。

x = torch.tensor([1., -1.],[1.,1.]], requires_gard=True)
out = x.pow(2).sum()
out.backward()
x.grad
# tensor([[ 2.0000, -2.0000], [ 2.0000,  2.0000]])

7. 保存数据

torch.Storage

8. Tensor属性

8.1 数据类型

dtype 改变

8.2 纬度

dim()

8.3尺寸

shape属性,size()

改变尺寸:

view() :连续性条件(contiguous)

reshape() :对不满足连续条件的,会重新开辟内存空间

Pytorch思维张量形状:(N, C, H, W)  (number,channel,height,width)


2. Tensor & ndarray

1. Tensor和ndarray

从Tensor得到numpy数组:.numpy()

从numpy得到Tensor:torch.from_numpy

arr = np.random.rand(4,5)
print(type(arr)) # <class 'numpy.ndarray'>

tensor1 = torch.from_numpy(arr)
print(type(tensor1)) #<class 'torch.Tensor'>

arr1 = tensor1.numpy()
print(type(arr1)) # <class 'numpy.ndarray'>

⚠️:

两种方式均为共享数据内存。

可用tensor.clone() 拷贝tensor

2.tensor转换Python数值和数值列表

item()

tolist()

scalar = torch.tensor(1) #scalar, only have value without vector
s = scalar.item()
print(s) #1
print(type(s)) <class 'int'>

tensor = torch.rand(3,2) # matrix
print(tensor)

#tensor([[0.6228, 0.5910], [0.9604, 0.1894], [0.4311, 0.7282]])


t = tensor.tolist()
print(t) #[[0.8211846351623535, 0.20020723342895508],[0.011571824550628662, 0.2906131148338318]]
print(type(t)) #<class 'list'>

3. 创建tensor

torch.tensor()&torch.Tensor()

torch.tensor()只能传入数据

torch.Tensor()可传数据和维度,建议使用torch.tensor()

方法名        功能备注
torch.rand(*sizes, out=None)return Tensor, 从[0,1)的均匀分布中抽取一组随机数字,形状由sizes定义
torch.randn(*sizes, out=None)标准正态分布,均值为0,方差为1,高斯白噪声不推荐
torch.normal(means, std, out=None)        包含从指定均值means和标准差std的离散正态分布中抽取随机数。std是tensor, 包含每个输出元素相关的正态分布标准差。
torch.rand_like(a)根据a的shape来生成随机数据
torch.randint(low=0, high, size)生成指定范围(low, hight)和size的随机整数数据常用
torch.arange(start=0, enf, step, * ,out=None)生成指定间隔的数据        常用
torch.ones(*size, *, out=None)生成给定size且值全为1矩阵常用
zeros()/zeros_like()/eye()全0的tensor和对角矩阵        常用

4.tensor操作

4.1 维度变换

4.1.1 squeeze&unsqueeze维度增减

  • squeeze()  :对tensor进行维度的压缩,去掉维数为1的
    • torch.squeeze(1) # delete all the dim=1
  • unsqueeze(): 给指定位置加dim=1的维度
    • torch.unsqueeze(a, N) 
      a.squeeze(N)# add dim=1 to N

4.1.2 transpose & permute​​​​​​​ 维度交换

torch.transpose()只能交换两个维度

permute()自由交换任意位置

import torch

b = torch.rand(1, 3, 28, 32) 
print(b.transpose(1, 3).shape) # transpose 1 and 3 which is 3 and 32
print(b.transpise(1, 3).transpose(1, 2).shape) #[1, 28, 32, 3]
print(b.permute(0, 2, 3, 1).shape)

4.2 索引切片

4.2.1 规则索引切片

t = torch.randint(1, 10, [3,3])
print(t)
# tensor([[7, 9, 1], [5, 7, 5], [8, 3, 1]])
print(t[0]) # tensor([7, 9, 1])

4.3 合并分割

4.3.1 torch.cat & torch.stack

torch.cat是连接,不会增加维度

torch.stack是堆叠,会增加维度

a = torch.arange(0,9).view(3,3)
print(a)
b = torch.arange(10, 19).view(3,3)
print(b)
c = torch.arange(20, 29).view(3,3)
print(c)
cat_abc = torch.cat([a, b, c], dim=0)
print(cat_abc.shape)
stack_abc = torch.stack([a, b, c], axis=0)
print(stack_abc.shape)
print(stack_abc)

'''

tensor([[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]])
tensor([[10, 11, 12],
        [13, 14, 15],
        [16, 17, 18]])
tensor([[20, 21, 22],
        [23, 24, 25],
        [26, 27, 28]])
torch.Size([9, 3])
torch.Size([3, 3, 3])
tensor([[[ 0,  1,  2],
         [ 3,  4,  5],
         [ 6,  7,  8]],

        [[10, 11, 12],
         [13, 14, 15],
         [16, 17, 18]],

        [[20, 21, 22],
         [23, 24, 25],
         [26, 27, 28]]])
'''

4.3.2 torch.split & torch.chunk

torch.split()是将tensor->多个块

a = torch.arange(10).reshape(5,2)
print(a)
print(torch.split(a, 2))
print(torch.split(a, [1,4]))
# If split_size_or_sections is a list, then tensor will be split into len(split_size_or_sections) chunks with sizes in dim according to split_size_or_sections.
"""
torch.split(tensor, split_size_or_sections, dim=0)

'''
tensor([[0, 1],
        [2, 3],
        [4, 5],
        [6, 7],
        [8, 9]])
(tensor([[0, 1],
        [2, 3]]), tensor([[4, 5],
        [6, 7]]), tensor([[8, 9]]))
(tensor([[0, 1]]), tensor([[2, 3],
        [4, 5],
        [6, 7],
        [8, 9]]))
'''

torch.chunk()将tensor按dim分割成chunks个tensor,返回元组

a = torch.arange(10).reshape(5,2)
print(a)
# print(torch.split(a, 2))
# print(torch.split(a, [1,4]))
print(torch.chunk(a, 2, dim=1))
'''
tensor([[0, 1],
        [2, 3],
        [4, 5],
        [6, 7],
        [8, 9]])
(tensor([[0],
        [2],
        [4],
        [6],
        [8]]), tensor([[1],
        [3],
        [5],
        [7],
        [9]]))
'''

4.4卷积相关算子

4.4.1 上采样

上采样大致被总结成了三个类别:

  1. 基于线性插值的上采样:最近邻算法(nearest)、双线性插值算法(bilinear)、双三次插值算法(bicubic)等,这是传统图像处理方法。
  2. 基于深度学习的上采样(转置卷积,也叫反卷积 Conv2dTranspose2d等)
  3. Unpooling 的方法(简单的补零或者扩充操作)
计算效果:最近邻插值算法 < 双线性插值 < 双三次插值。计算速度:最近邻插值算法 > 双线性插值 > 双三次插值。

Reference:

1. https://github.com/HarleysZhang/cv_note/blob/master/5-deep_learning/ml-dl-%E6%A1%86%E6%9E%B6%E7%AC%94%E8%AE%B0/Pytorch%E5%9F%BA%E7%A1%80-tensor%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.md

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值