聊一聊pytorch张量基本方法,持续更新
张量
四种初始化方法
1.直接生成,原始结构类型决定张量类型
import torch
data = [[1,2],
[3,4]]
x_data = torch.tensor(data)
>>>x_data
tensor([[1, 2],
[3, 4]])
2.通过numpy转化生成张量(可以互相转化)
import numpy as np
np_array = np.array(data)
x_np = torch.from_numpy(np_array)
#x_np
>>>x_np
tensor([[1, 2],
[3, 4]], dtype=torch.int32)
3.通过已有张量来生成新的张量
(同时新的张量将会继承已有张量的数据属性,包括结构、类型,也可以指定新的数据类型)
#torch.ones_like函数、torch.zeros_like函数、rand_like函数的基本功能是
#根据给定张量,生成与其形状相同的全1张量或全0张量或随机张量
>>>x_ones = torch.ones_like(x_data)
>>>x_ones
tensor([[1, 1],
[1, 1]])
>>>x_rand = torch.rand_like(x_data,dtype = torch.float)
>>>x_rand
tensor([[0.5700, 0.4488],
[0.3124, 0.0294]])
4.通过指定数据维度(shape)来生成张量
shape = (2,3)
one_tensor = torch.ones(shape)
one_tensor
'''
tensor([[1., 1., 1.],
[1., 1., 1.]])
'''
张量的属性和运算
张量可以查询张量的维度、数据类型和存储设备(CPU或GPU)
tensor_sa = torch.rand(3,4)
print(tensor_sa)
print(tensor_sa.shape)
print(tensor_sa.dtype)
print(tensor_sa.device)
'''
tensor([[0.8177, 0.5182, 0.9222, 0.5564],
[0.3470, 0.2929, 0.6438, 0.8626],
[0.4011, 0.6267, 0.6672, 0.6296]])
torch.Size([3, 4])
torch.float32
cpu
'''
张量运算
常用的操作有转置、索引、切片、数学运算、线性代数、随机取样等
加减乘除
#加减乘除
a + b = torch.add(a, b)
a - b = torch.sub(a, b)
a * b = torch.mul(a, b)
a / b = torch.div(a, b)
#实操
import torch
a = torch.rand(3, 4)
b = torch.rand(4)
a
# 输出:
tensor([[0.6232, 0.5066, 0.8479, 0.6049],
[0.3548, 0.4675, 0.7123, 0.5700],
[0.8737, 0.5115, 0.2106, 0.5849]])
b
# 输出:
tensor([0.3309, 0.3712, 0.0982, 0.2331])
# 相加
# b会被广播 变成维度相同的进行加减乘除
a + b
# 输出:
tensor([[0.9541, 0.8778, 0.9461, 0.8380],
[0.6857, 0.8387, 0.8105, 0.8030],
[1.2046, 0.8827, 0.3088, 0.8179]])
# 等价于上面相加
torch.add(a, b)
# 输出:
tensor([[0.9541, 0.8778, 0.9461, 0.8380],
[0.6857, 0.8387, 0.8105, 0.8030],
[1.2046, 0.8827, 0.3088, 0.8179]])
# 比较两个是否相等
torch.all(torch.eq(a + b, torch.add(a, b)))
# 输出:
tensor(True)
矩阵相乘
torch.mm(a, b) # 此方法只适用于2维
torch.matmul(a, b)
a @ b = torch.matmul(a, b) # 推荐使用此方法
#实操
a = torch.full((2, 2), 3)
a
# 输出
tensor([[3., 3.],
[3., 3.]])
b = torch.ones(2, 2)
b
# 输出
tensor([[1., 1.],
[1., 1.]])
torch.mm(a, b)
# 输出
tensor([[6., 6.],
[6., 6.]])
torch.matmul(a, b)
# 输出
tensor([[6., 6.],
[6., 6.]])
a @ b
# 输出
tensor([[6., 6.],
[6., 6.]])
幂次计算
pow, sqrt, rsqrt
a = torch.full([2, 2], 3)
a
# 输出
tensor([[3., 3.],
[3., 3.]])
a.pow(2)
# 输出
tensor([[9., 9.],
[9., 9.]])
aa = a ** 2
aa
# 输出
tensor([[9., 9.],
[9., 9.]])
# 平方根
aa.sqrt()
# 输出
tensor([[3., 3.],
[3., 3.]])
# 平方根
aa ** (0.5)
# 输出
tensor([[3., 3.],
[3., 3.]])
# 平方根
aa.pow(0.5)
# 输出
tensor([[3., 3.],
[3., 3.]])
# 平方根的倒数
aa.rsqrt()
# 输出
tensor([[0.3333, 0.3333],
[0.3333, 0.3333]])
tensor([[3., 3.],
[3., 3.]])
近似值
a.floor() # 向下取整:floor,地板
a.ceil() # 向上取整:ceil,天花板
a.trunc() # 保留整数部分:truncate,截断
a.frac() # 保留小数部分:fraction,小数
a.round() # 四舍五入:round,大约
限幅
a.max() # 最大值
a.min() # 最小值
a.median() # 中位数
a.clamp(10) # 将最小值限定为10
a.clamp(0, 10) # 将数据限定在[0, 10],两边都是闭区间
#实操
a
'''
tensor([[0.1042, 0.5222, 0.8518, 0.5040],
[0.4868, 0.9410, 0.1554, 0.2218],
[0.7903, 0.5346, 0.5185, 0.0802]])
'''
a.max()
'''
tensor(0.9410)
'''
a.min()
'''
tensor(0.0802)
'''
a.clamp(0.51)
'''
tensor([[0.5100, 0.5222, 0.8518, 0.5100],
[0.5100, 0.9410, 0.5100, 0.5100],
[0.7903, 0.5346, 0.5185, 0.5100]])
'''
a.clamp(0.2,0.51)
'''
tensor([[0.2000, 0.5100, 0.5100, 0.5040],
[0.4868, 0.5100, 0.2000, 0.2218],
[0.5100, 0.5100, 0.5100, 0.2000]])
'''
张量方法
torch.full() torch.full_like()
x = torch.full((3,2),fill_value = 5,dtype = torch.long)
'''
tensor([[5, 5],
[5, 5],
[5, 5]])
'''
y = torch.full_like(x,1.7,dtype = torch.float)
y
'''
tensor([[1.7000, 1.7000],
[1.7000, 1.7000],
[1.7000, 1.7000]])
'''
索引和切片
ten = torch.ones(4,4)
ten[:,1] = 0
ten
'''
tensor([[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.]])
'''
拼接 torch.cat torch.stack
torch.cat 仅仅是张量的连接,不会增加维度
torch.cat(tensors, dim = 0, out = None)
#除了dim那个维度,其余维度待拼接尺寸要相同
#不会对原张量产生影响
torch.stack 是堆叠,会增加维度,可以参考下面栗子理解一下
torch.stack((x1,x2),0)
'''
tensor([[[0.0894, 0.1871, 0.6987],
[0.9175, 0.0312, 0.4474]],
[[0.1643, 0.6791, 0.7948],
[0.1119, 0.5024, 0.1492]]])
'''
torch.stack((x1,x2),1)
'''
tensor([[[0.0894, 0.1871, 0.6987],
[0.1643, 0.6791, 0.7948]],
[[0.9175, 0.0312, 0.4474],
[0.1119, 0.5024, 0.1492]]])
'''
torch.stack((x1,x2),2)
'''
tensor([[[0.0894, 0.1643],
[0.1871, 0.6791],
[0.6987, 0.7948]],
[[0.9175, 0.1119],
[0.0312, 0.5024],
[0.4474, 0.1492]]])
'''
Tensor和Numpy转化
张量与Numpy array数据组在cpu上可以共用一块内存,所以改变一个另一个也会发生变化。
Tensor -> Numpy array
(tensor.add_() 自赋值计算)Numpy array -> Tensor
版权声明:本文为weixin_43786143原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。