Python PyTorch1:张量基本操作

PyTorch是一个开源的深度学习框架,可加快从研究原型设计到生产部署的过程。

import torch

1. Python数据类型与PyTorch数据类型对照

PyTorch没有string类型。可以使用one-hot等方式存储string类型。

2. 创建张量

2.1 从NumPy导入

torch.from_numpy(a)

import numpy as np
import torch

a = np.array([2, 3.3])
ta = torch.from_numpy(a)
print(ta)
b = np.ones([2, 3])
tb = torch.from_numpy(b)
print(tb)

2.2 从list创建

import torch

t1 = torch.tensor([2., 3.2])
print(t1)
t2 = torch.tensor([[2., 3.2], [1., 2.369]])
print(t2)
# 2行3列 不建议使用
t3 = torch.Tensor(2, 3)
print(t3)
t4 = torch.FloatTensor([2., 3.2])
print(t4)

 2.3 其他方法

import torch

# 初始化2行3列 值全为0
t5 = torch.empty(2, 3)
print(t5)
# 0~1随机3行3列
t6 = torch.rand(3, 3)
print(t6)
# 复制
t7 = torch.rand_like(t6)
print(t7)
# 最小值 最大值 3*3
t8 = torch.randint(1, 10, [3, 3])
print(t8)
# 正态分布
t9 = torch.randn(3, 3)
print(t9)
# 从给定参数 means, std 的离散正态分布中抽取随机数
t10 = torch.normal(mean=torch.full([10], 0.), std=torch.arange(1, 0, -0.1))
print(t10)
# 2*3 全为7
t11 = torch.full([2, 3], 7)
print(t11)
# 等差数列
t12 = torch.arange(0, 10, 2)
print(t12)
# 等分4份
t13 = torch.linspace(0, 10, steps=4)
print(t13)
# 等分4份 10的x次方
t14 = torch.logspace(0, -1, steps=10)
print(t14)
# 全1 3*3
t15 = torch.ones(3,3)
print(t15)
# 全0 3*3
t16 = torch.zeros(3,3)
print(t16)
# 单位矩阵
t17 = torch.eye(3)
print(t17)
# 跟t17一样形状的全1
t18 = torch.ones_like(t17)
print(t18)
# 随机打散
t18 = torch.randperm(10)
print(t18)

3. 索引与切片

import torch

t1 = torch.rand(3, 3)
print(t1)
print(t1.shape)
# 前2行
print(t1[:2])
# 前2列
print(t1[:, :2])
# 选择特定的行和列
print(t1[[0, 2], 2])
# ...代表若干个连续的:
print(t1[..., 2])

4. 维度变换

import torch

t1 = torch.rand(3, 4)
print(t1)
t2 = t1.reshape(2, 6)
print(t2)
# 在0索引处增加一个额外的维度 变为1, 3, 4
t3 = t1.unsqueeze(0)
print(t3)
# 在-1索引处增加一个额外的维度 变为3, 4, 1
t4 = t1.unsqueeze(-1)
print(t4)
# 在2索引处减少维度 变为3, 4
t5 = t4.squeeze(2)
print(t5)
# 复制4行
t6 = t1.expand(4, 4)
print(t6)
# 转置
t7 = t6.t()
print(t7)
# 交换维度
t8 = t6.permute(1, 0)
print(t8)

5. broadcast

import torch

t1 = torch.rand(4, 4)
print(t1)
t2 = torch.rand(4)
print(t2)
# t2自动扩张到高维度,复制相同的4行
t3 = t1 + t2
print(t3)

6. 合并与分割

import torch

t1 = torch.rand(4, 4)
print(t1)
t2 = torch.rand(4, 4)
print(t2)
# 合并,从0维度上叠加
t3 = torch.cat([t1, t2], dim=0)
print(t3)
# 增加一个维度,叠加两个形状相同的tensor
t4 = torch.stack([t1, t2], dim=0)
print(t4)
# 将0维度的4行拆分成3行和1行,按长度拆分
t5, t6 = t1.split([3, 1], dim=0)
print(t5, t6)
# 按数量拆分
t7, t8 = t1.chunk(2, dim=0)
print(t7, t8)

7. 数学运算

import torch

t1 = torch.rand(4, 4)
print(t1)
t2 = torch.rand(4, 4)
print(t2)
# + - * /
t3 = t1 + t2
print(t3)
# 矩阵乘法
t4 = t1 @ t2
print(t4)
# 矩阵n次方
t5 = t1 ** 2
print(t5)
# e^n
t6 = torch.exp(t1)
print(t6)
t7 = torch.log(t1)
print(t7)
# floor 向下取整 ceil 向下取整 frac 取小数部分
t8 = torch.round(torch.tensor(0.77))
print(t8)
# max min median
# clamp 将矩阵裁剪到指定范围
t9 = t1.clamp(0, 10)
print(t9)

8. 属性统计

import torch

t1 = torch.rand(4, 4)
print(t1)
# n维范数
t2 = t1.norm(2)
print(t2)
# 前2大的数据
t3 = t1.topk(2)
print(t3)
# 第2大的数据
t4 = t1.kthvalue(2)
print(t4)
# 判断是否>0 eq=
t5 = torch.gt(t1, 0.5)
print(t5)

9. 高阶操作

import torch

t1 = torch.rand(4, 4)
print(t1)
# where(条件, x, y) 如果条件符合则返回x,否则返回y
t2 = torch.where(t1 < 0.5, 0, 1)
print(t2)
label = (torch.arange(16) + 100).reshape(4, 4)
# 将t1映射到label对应的位置
t3 = torch.gather(label, dim=1, index=t1.long())
print(t3)
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hinomoto Oniko

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值