PyTorch 基础操作整理(持续更新..)


数据类型

数据类型dtypeCPU 张量GPU 张量
32 位浮点torch.float32torch.FloatTensortorch.cuda.FloatTensor
64 位浮点torch.float64torch.DoubleTensortorch.cuda.DoubleTensor
16 位浮点torch.float16torch.HalfTensortorch.cuda.HalfTensor
8 位整数(无符号)torch.uint8torch.ByteTensortorch.cuda.ByteTensor
8 位整数(有符号)torch.int8torch.CharTensortorch.cuda.CharTensor
16 位整数(有符号)torch.int16torch.ShortTensortorch.cuda.ShortTensor
32 位整数(有符号)torch.int32torch.IntTensortorch.cuda.IntTensor
64 位整数(有符号)torch.int64torch.LongTensortorch.cuda.LongTensor
布尔型torch.booltorch.BoolTensortorch.cuda.BoolTensor

张量定义

随机值 rand

类型 dtype 只能是浮点值,data 在 [0,1] 之间

imgs = torch.rand(3,3,2,5)
 
tensor([[[[0.5211, 0.9219, 0.6250, 0.0364, 0.7262],
          [0.8277, 0.5067, 0.8237, 0.6780, 0.4223]],
          
         [[0.4073, 0.8355, 0.6411, 0.9650, 0.5677],
          [0.1545, 0.9761, 0.4261, 0.6104, 0.3905]],
          
         [[0.5831, 0.5657, 0.4119, 0.2128, 0.5937],
          [0.2113, 0.6590, 0.9260, 0.7682, 0.8999]]],

-----------------------------------------------------------------

        [[[0.2044, 0.0613, 0.2935, 0.7480, 0.4436],
          [0.9724, 0.5701, 0.1931, 0.3375, 0.2775]],
          
         [[0.9629, 0.0691, 0.8732, 0.2608, 0.8556],
          [0.1645, 0.9896, 0.8447, 0.9494, 0.2991]],
          
         [[0.5636, 0.4376, 0.6832, 0.7716, 0.4902],
          [0.7178, 0.0293, 0.1944, 0.7656, 0.7586]]],

-----------------------------------------------------------------

        [[[0.3418, 0.4774, 0.3494, 0.2134, 0.4898],
          [0.4064, 0.0722, 0.4028, 0.5270, 0.1629]],
          
         [[0.0349, 0.3834, 0.7956, 0.3460, 0.8818],
          [0.7591, 0.1060, 0.1520, 0.1477, 0.0876]],
          
         [[0.5369, 0.7785, 0.7006, 0.3392, 0.4646],
          [0.2737, 0.8856, 0.4953, 0.6137, 0.3623]]]])

zeros/zeros_like/ones/ones_like

A = torch.zeros(3, 3, 2, 5, dtype=torch.uint8)
B = torch.zeros_like(A)

C = torch.ones(3, 3, 2, 5, dtype=torch.uint8)
D = torch.ones_like(A)

numpy-> tensor

避免复制:收藏使用!!!

img = np.random.randint(0, 256, (360, 480, 3), dtype=np.uint8)
img = torch.as_tensor(img)

内存复制:

img = np.random.randint(0, 256, (360, 480, 3), dtype=np.uint8)
img = torch.from_numpy(img)

list -> tensor

A = torch.tensor([[1., -1.], [1., -1.]])

张量切片

任意多维度

A = torch.rand(1, 1, 5, 7)
# tensor([[[[0.6302, 0.1213, 0.0888, 0.0047, 0.8832, 0.1047, 0.8034],
#           [0.1957, 0.1347, 0.4584, 0.3493, 0.9529, 0.1589, 0.4328],
#           [0.7241, 0.4587, 0.2984, 0.6716, 0.3970, 0.2685, 0.8587],
#           [0.6361, 0.9595, 0.0982, 0.6371, 0.7920, 0.5851, 0.6135],
#           [0.4848, 0.7414, 0.5404, 0.2262, 0.6979, 0.0472, 0.7084]]]])

B = A[..., -1]  # [1, 1, 5]
# tensor([[[0.7572, 0.5068, 0.8162, 0.1338, 0.1350]]])

间隔采样

在这里插入图片描述

A1 = A[..., ::2, ::2]   # 橙色
A2 = A[..., ::2, 1::2]	# 黄色
A3 = A[..., 1::2, 1::2]	# 绿色
A4 = A[..., 1::2, ::2]	# 蓝色
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值