python torch基础
import numpy as np
import torch
import numpy
t1 = np.array([1, 2, 3])
# 可遍历的
a = torch.Tensor([4, 5, 6])
# 可将numpy类型转为tensor <class 'torch.Tensor'>
b = torch.from_numpy(t1)
# 创建全1数值张量 2行4列
ones = torch.ones((2, 4))
print(ones)
# tensor([[1., 1., 1., 1.],
# [1., 1., 1., 1.]])
# 创建填充张量 size=[2, 3]2行3列,full_value=(int/float)填充的值
fulls = torch.full([2, 3], 2.0)
print(fulls)
# tensor([[2., 2., 2.],
# [2., 2., 2.]])
# 创建等差数列
# 0~10(不包括10)等差为2 的数列
arange = torch.arange(0, 10, 2)
print(arange)
# tensor([0, 2, 4, 6, 8])
# 创建线性间距向量
# 起始2,终止10,个数5个
lines = torch.linspace(2, 10, 5)
print(lines)
# tensor([ 2., 4., 6., 8., 10.])
# 创建3行3列的对角矩阵,对角线上全是1,其余全是0
eyes = torch.eye(3, 3)
print(eyes)
# tensor([[1., 0., 0.],
# [0., 1., 0.],
# [0., 0., 1.]])
# 创建随即值,包含了从标准正态分布(均值为0,方差为1)中取出的一组随机值
randn = torch.randn(4)
print(randn)
# tensor([ 1.4672, -0.7797, 0.4773, 1.3434])
# 创建随即值,在0~10之间均匀填充,张量的shape由size定义
randint = torch.randint(10, size=(3, 3))
print(randint)
# tensor([[6, 7, 2],
# [5, 8, 9],
# [1, 7, 3]])
# 创建随即值,包含了从区间(0,1)的均匀分布中抽取的一组随机数
rand = torch.rand(4)
print(rand)
# tensor([0.4084, 0.9024, 0.9798, 0.0089])
# 创建正态分布,
# 四种模式:
# 1.mean为标量,std为标量,这种模式必须加size
# 2.mean为标量,std为张量
# 3.mean为张量,std为标量
# 4.mean为张量,std为张量
normal = torch.normal(0, 1, size=[2, 3])
print(normal)
# tensor([[-0.0536, -1.2717, 1.4601],
# [-0.5463, 0.3849, -0.0123]])
#-- 张量的运算操作--#
# 判断电脑是否有GPU
# 判断当前环境GPU是否可用,然后将tensor导入GPU运行
if torch.cuda.is_available():
print('computer have GPU')
tensor = normal.to('cuda')
else:
print('computer not have GPU')
input = torch.rand(2, 3)
# 生成与input形状相同,元素全为1的张量
a = torch.ones_like(input)
print(a)
# tensor([[1., 1., 1.],
# [1., 1., 1.]])
# 生成与input形状相同,元素全为0的张量
b = torch.zeros_like(input)
print(b)
# tensor([[0., 0., 0.],
# [0., 0., 0.]])
# 生成一个两行三列的全1张量
t = torch.ones((2, 3))
# 拼接函数cat,在给定维度上对输入的张量进行连接操作 dim=0横轴1纵轴
tt = torch.cat([t, t], dim=0)
print(tt)
# tensor([[1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.]])
# 拼接函数stack,与cat不同的是,stack会增加维度。简单来说就是增加新的维度进行堆叠
# 扩维拼接
stacks = torch.stack([t, t], dim=2)
print(stacks)
t = torch.ones((2, 5))
# 在给定维度上将输入张量进行分块
# 被分块的张量,维度,要切分的份数
chunks = torch.chunk(t, dim=1, chunks=5)
print(chunks)
# (tensor([[1.],
# [1.]]),
# tensor([[1.],
# [1.]]),
# tensor([[1.],
# [1.]]),
# tensor([[1.],
# [1.]]),
# tensor([[1.],
# [1.]]))
# 将tensor切分
# 被切分的张量,每块的大小,维度
splits = torch.split(t, 2, dim=1)
print(splits)
# (tensor([[1., 1.],
# [1., 1.]]),
# tensor([[1., 1.],
# [1., 1.]]),
# tensor([[1.],
# [1.]]))
# 返回一个删除了所有大小为1的输入维度的张量
# 如果输入为(A✖B✖1✖C),则输出张量为(A✖B✖C)
t1 = torch.zeros(2, 1, 2)
print(t1)
# tensor([[[0., 0.]],
#
# [[0., 0.]]])
t2 = torch.squeeze(t1)
print(t2)
# tensor([[0., 0.],
# [0., 0.]])
t2 = torch.squeeze(t1, 0)
print(t2)
# tensor([[[0., 0.]],
#
# [[0., 0.]]])
t2 = torch.squeeze(t1, 1)
print(t2)
# tensor([[0., 0.],
# [0., 0.]])
# 自动赋值运算通常在方法后有 _ 作为后缀, 例如: x.copy_(y), x.t_()操作会改变 x 的取值。
print(t, '\n')
# tensor([[1., 1., 1., 1., 1.],
# [1., 1., 1., 1., 1.]])
t.add_(5)
print(t)
# tensor([[6., 6., 6., 6., 6.],
# [6., 6., 6., 6., 6.]])