目录
1.torch.cat函数可以将两个张良根据指定的维度拼接起来
2.torch.stack函数可以将两个张量根据指定的维度叠加起来
一、张量的操作
1. 创建方式
1.torch.tensor 根据指定数据创建张量
def test01():
#1 使用标量
data = torch.tensor(10)
print(data)
#2numpy数组
data = np.random.randn(2,3)
data = torch.tensor(data)
print(data)
#3 list列表
data = [[10.,20.,30.,],[40.,50.,60.]]
data = torch.tensor(data)
print(data)
tensor(10)
tensor([[ 0.5863, 0.6107, -0.1272],
[ 0.8330, -1.2949, -0.2462]], dtype=torch.float64)
tensor([[10., 20., 30.],
[40., 50., 60.]])
2.torch.Tensor 根据形状创建张量,也可以用来创建指定数据的张量
#创建指定形状张量
def test02():
#2.1 创建2行3列的张量
data = torch.Tensor(2,3)
print(data)
#2.2 创建指定值的张量
data = torch.Tensor([2,3])
print(data)
tensor([[0., 0., 0.],
[0., 0., 0.]])
tensor([2., 3.])
3.torch.IntTensor、torch.FloatTensor、torch.DoubleTensor创建指定类型的张量
2.创建线性和随机张量
1.torch.arange 和torch.linspace创建线性张量
def test01():
# 1.1 创建指定步长的张量
# 第一参数:开始值
# 第二参数:结束值
# 第三参数:步长
data = torch.arange(0,10,2)
print(data)
#2.2 在指定区间指定元素个数
data = torch.linspace(0,11,10)
print(data)
tensor([0, 2, 4, 6, 8])
tensor([ 0.0000, 1.2222, 2.4444, 3.6667, 4.8889, 6.1111, 7.3333, 8.5556,
9.7778, 11.0000])
2.torch.random.init_seed和torch.manaual_seed随机种子设置
3.torch.randn创建随机张量
#创建随机张量
def test02():
# 固定随机数种子
torch.random.manual_seed(0)
#2.1 创建随即张量
data = torch.randn(2,3)
print(data)
print('随机数种子',torch.random.initial_seed())
tensor([[ 1.5410, -0.2934, -2.1788],
[ 0.5684, -1.0845, -1.3986]])
随机数种子 0
3.创建01张量
1.torch.ones和torch.ones_like 创建全1张量
2.torch.zeros和torch.zeros_like创建全0张量
3.torch.full和torch.full_like创建全为指定值张量
#1.1创建全为0的张量
data = torch.zeros(2,3)
print(data)
#1.2根据其他张量的形状创建全0张量
data = torch.zeros_like(data)
print(data)
#2.创建全为1的张量
data = torch.ones(2,3)
print(data)
#2.2根据其他张量的形状创建全0张量
data = torch.ones_like(data)
print(data)
#2.创建全为指定值的张量
data = torch.full([2,3],9)
print(data)
#创建一个形状和data一样,但是值全为200的张量
data = torch.full([2,3],200)
print(data)
tensor([[0., 0., 0.],
[0., 0., 0.]])
tensor([[0., 0., 0.],
[0., 0., 0.]])
tensor([[1., 1., 1.],
[1., 1., 1.]])
tensor([[1., 1., 1.],
[1., 1., 1.]])
tensor([[9, 9, 9],
[9, 9, 9]])
tensor([[200, 200, 200],
[200, 200, 200]])
4.张量元素类型转换
1.tensor.type(torch.DoubleTensor)
2.torch.double()
data = torch.full((2,3),8)
print(data.dtype)
#返回一个新的值
data = data.type(torch.DoubleTensor)
print(data.dtype)
#返回一个新的张量
data = data.int()
print(data.dtype)
torch.int64
torch.float64
torch.int32
二、张量的计算
1.张量的基本运算
add、sub、mul、div、neg 不该原数据
add_、sub_、mul_、div_、neg_ 修改原数据
data = torch.randint(0,10,(2,3))
print(data)
data = data.add(10)
print(data)
# data.sub() 减法
# data.mul() 乘法
# data.div() 除法
# data.neg() 取反
2、阿达玛积
矩阵对应位置的元素相乘
data1 = torch.tensor([[1,2],[3,4]])
data2 = torch.tensor([[5,6],[7,8]])
data = data1.mul(data2)
print(data)
data3 = data1 * data2
print(data3)
tensor([[ 5, 12],
[21, 32]])
tensor([[ 5, 12],
[21, 32]])
3.点积运算
矩阵n*m 和矩阵m*p 点积计算得到矩阵n*p
1.运算符@用于进行两个矩阵的点乘运算
2.torch.mm用于两个矩阵点乘运算,要求输入矩阵为2维
3.torch.bmm用于批量进行矩阵点乘运算,要求属于矩阵为3维
4.torch.matmul对矩阵进行点乘运算
- 对于输入都是二维相当于mm
- 对于输入都是三位相当于bmm
- 对输入的shape不同的张量,对应的最后几个维度必须符合矩阵运算规则
4.指定运算设备
PyTorch会默认将张量创建在CPU的内存中。想要把张量移动到GPU上
1.使用cuda方法
2.直接在GPU上创建张量,
3使用to方法指定设备
data = torch.tensor([10,20,30])
print("存储设备:",data.device)
data = data.cuda()
print("存储设备:", data.device)
data = torch.tensor([10,20,30],device='cuda:0')
print("存储设备:", data.device)
data = torch.tensor([10,20,30])
print("存储设备:",data.device)
data = data.to('cuda:0')
print("存储设备:",data.device)
存储设备: cpu
存储设备: cuda:0
存储设备: cuda:0
存储设备: cpu
存储设备: cuda:0
三、张量类型的转换
1.张量转换为numpy
使用Tensor.numpy函数可以将张良转换为ndarray,但是共享内存,可以使用copy函数避免共享
def test01():
data_tensor = torch.tensor([2,3,4])
data_numpy = data_tensor.numpy()
data_numpy1 = data_tensor.numpy().copy() #不共享内存
data_tensor[0] = 100
print(data_tensor)
print(data_numpy)
print(data_numpy1)
tensor([100, 3, 4])
[100 3 4]
[2 3 4]
2.numpy转换为张量
1.使用from_numpy可以将ndarray数组转换为Tensor,默认共享内存,使用copy函数避免共享
2.使用torch.tensor 可以将ndarray数组转换为Tensor,默认不共享内存
data_numpy = np.array([2,3,4])
data_tensor = torch.from_numpy(data_numpy) #共享内存
data_tensor1 = torch.from_numpy(data_numpy.copy()) #不共享内存
data_tensor2 = torch.tensor(data_numpy) #不共享内存
data_numpy[0] = 100
print(data_numpy)
print(data_tensor)
print(data_tensor1)
print(data_tensor2)
[100 3 4]
tensor([100, 3, 4], dtype=torch.int32)
tensor([2, 3, 4], dtype=torch.int32)
tensor([2, 3, 4], dtype=torch.int32)
3.标量张量和数字的转换
对于只有一个元素的张量,使用item方法将该值从张量中提取出来。
t1 = torch.tensor(30)
t2 = torch.tensor([30])
t3 = torch.tensor([[30]])
print(t1.shape)
print(t2.shape)
print(t3.shape)
print(t1.item())
print(t2.item())
print(t3.item())
torch.Size([])
torch.Size([1])
torch.Size([1, 1])
30
30
30
四、张量的拼接
1.torch.cat函数可以将两个张良根据指定的维度拼接起来
torch.manual_seed(0)
data1 = torch.randint(0,10,[3,4,5])
data2 = torch.randint(0,10,[3,4,5])
print(data1.shape)
print(data2.shape)
torch.Size([3, 4, 5])
torch.Size([3, 4, 5])
torch.Size([6, 4, 5])
2.torch.stack函数可以将两个张量根据指定的维度叠加起来
torch.manual_seed(0)
data1 = torch.randint(0,10,[2,3])
data2 = torch.randint(0,10,[2,3])
print(data1)
print(data2)
print('-' * 30)
new_data = torch.stack([data1,data2],dim=0) #按照第0个维度进行叠加
print(new_data.shape)
print(new_data)
print('-' * 30)
new_data = torch.stack([data1,data2],dim=1) #按照第0个维度进行叠加
print(new_data.shape)
print(new_data)
print('-' * 30)
new_data = torch.stack([data1,data2],dim=2) #按照元素进行叠加
print(new_data.shape)
print(new_data)
print('-' * 30)
tensor([[4, 9, 3],
[0, 3, 9]])
tensor([[7, 3, 7],
[3, 1, 6]])
------------------------------
torch.Size([2, 2, 3])
tensor([[[4, 9, 3],
[0, 3, 9]],
[[7, 3, 7],
[3, 1, 6]]])
------------------------------
torch.Size([2, 2, 3])
tensor([[[4, 9, 3],
[7, 3, 7]],
[[0, 3, 9],
[3, 1, 6]]])
------------------------------
torch.Size([2, 3, 2])
tensor([[[4, 7],
[9, 3],
[3, 7]],
[[0, 3],
[3, 1],
[9, 6]]])
------------------------------
五、张量索引操作
1.简单行、列索引
torch.manual_seed(0)
data = torch.randint(0,10,[4,5])
print(data)
print('-'*30)
# 获得指定行的元素
print(data[2])
# 获得某个列的元素
print(data[:,2])
# 获得指定位置的某个元素
print(data[1,2],data[1][2])
# 表示先获得前三行,在获得第三列的数据
print(data[:3,2])
# 如果索引的行都是一个1维索引,那么两个列表的长度必须相等
# 表示获得(0,0)(2,1)(3,2)三个位置的元素
print(data[[0,2,3],[0,1,2]])
#表示第0,2,3行的0,1,2列
print(data[[[0],[2],[3]],[0,1,2]])
tensor([[4, 9, 3, 0, 3],
[9, 7, 3, 7, 3],
[1, 6, 6, 9, 8],
[6, 6, 8, 4, 3]])
------------------------------
tensor([1, 6, 6, 9, 8])
tensor([3, 3, 6, 8])
tensor(3) tensor(3)
tensor([3, 3, 6])
tensor([4, 6, 8])
tensor([[4, 9, 3],
[1, 6, 6],
[6, 6, 8]])
2.布尔索引、多维索引
torch.manual_seed(0)
data = torch.randint(0,10,[4,5])
print(data)
# 获取张量中所有大于3的元素
print(data[data >3])
# 获取张量中第2列大于6的行
print(data[data[:,1] >6])
# 获取张量中第2行大于3的所有列
print(data[:,data[1] >3])
tensor([[4, 9, 3, 0, 3],
[9, 7, 3, 7, 3],
[1, 6, 6, 9, 8],
[6, 6, 8, 4, 3]])
tensor([4, 9, 9, 7, 7, 6, 6, 9, 8, 6, 6, 8, 4])
tensor([[4, 9, 3, 0, 3],
[9, 7, 3, 7, 3]])
tensor([[4, 9, 0],
[9, 7, 7],
[1, 6, 9],
[6, 6, 4]])
torch.manual_seed(0)
data = torch.randint(0,10,[3,4,5])
print(data)
print('-'*30)
# 按第0个维度选择第0元素,4行5列元素
print(data[0,:,:])
print('-' * 30)
# 按第1个维度选择第0元素,4行5列元素
print(data[:,0,:])
print('-' * 30)
# 按第2个维度选择第0元素,4行5列元素
print(data[:,:,0])
print('-' * 30)