pytorch中张量的有关操作

创建张量

torch.tensor(data): 从数据创建张量

这个函数会根据提供的数据创建一个新的张量。数据可以是列表、数组等。

import torch

data = [1, 2, 3, 4, 5]
tensor_data = torch.tensor(data)
print(tensor_data)

torch.zeros(size): 创建元素全为0的张量

创建一个指定大小的张量,其中所有元素的值都为0。

import torch

size = (2, 3)
zeros_tensor = torch.zeros(size)
print(zeros_tensor)

torch.ones(size): 创建元素全为1的张量

创建一个指定大小的张量,其中所有元素的值都为1。

import torch

size = (2, 3)
ones_tensor = torch.ones(size)
print(ones_tensor)

torch.empty(size): 创建未初始化的张量

创建一个指定大小的未初始化张量,其值取决于内存的状态。

import torch

size = (2, 3)
empty_tensor = torch.empty(size)
print(empty_tensor)

torch.randn(size): 创建服从标准正态分布的张量

创建一个指定大小的张量,其中的元素值是从标准正态分布中随机抽取的。

import torch

size = (2, 3)
randn_tensor = torch.randn(size)
print(randn_tensor)

torch.arange(start, end, step): 创建一个范围内的一维张量

创建一个一维张量,其中的元素值从起始值到结束值,步长为给定的步长。

import torch

start = 0
end = 5
step = 1
arange_tensor = torch.arange(start, end, step)
print(arange_tensor)

torch.linspace(start, end, steps): 创建一个在指定范围内均匀间隔的张量

创建一个一维张量,其中的元素值在指定范围内均匀分布。

import torch

start = 0
end = 5
steps = 5
linspace_tensor = torch.linspace(start, end, steps)
print(linspace_tensor)

张量属性相关

.dtype: 获取张量的数据类型

返回张量中元素的数据类型。

import torch

tensor = torch.tensor([1, 2, 3])
print(tensor.dtype)

.shape: 获取张量的形状

返回一个元组,表示张量的形状。

import torch

tensor = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(tensor.shape)

.device: 获取张量所在的设备

返回一个字符串,表示张量所在的设备,如’cpu’或’cuda:0’。

import torch

tensor = torch.tensor([1, 2, 3])
print(tensor.device)

张量索引、切片与拼接

tensor[index]: 索引操作

使用索引来访问张量中的元素。

import torch

tensor = torch.tensor([[1, 2, 3], [4, 5, 6]])
element = tensor[0, 1]  # Accesses the element at row 0, column 1
print(element)
tensor[start:end]: 切片操作

使用切片来获取张量的子张量

import torch

tensor = torch.tensor([[1, 2, 3], [4, 5, 6]])
sub_tensor = tensor[:, 1:]  # Slices the tensor to get all rows and columns starting from the second column
print(sub_tensor)
torch.cat(tensors, dim): 在给定维度上连接张量

沿着指定维度将多个张量连接在一起

import torch

tensor1 = torch.tensor([[1, 2], [3, 4]])
tensor2 = torch.tensor([[5, 6], [7, 8]])
concatenated_tensor = torch.cat((tensor1, tensor2), dim=0)  # Concatenates along the row dimension
print(concatenated_tensor)
torch.stack(tensors, dim): 在新维度上堆叠张量

在一个新的维度上堆叠多个张量

import torch

tensor1 = torch.tensor([1, 2, 3])
tensor2 = torch.tensor([4, 5, 6])
stacked_tensor = torch.stack((tensor1, tensor2), dim=1)  # Stacks tensors along a new dimension
print(stacked_tensor)

张量变换

tensor.view(shape): 返回给定形状的张量视图

返回一个具有指定形状的新张量,原始张量的形状必须与新形状兼容。

import torch

tensor = torch.tensor([[1, 2], [3, 4]])
reshaped_tensor = tensor.view(1, 4)  # Reshapes the tensor to a 1x4 tensor
print(reshaped_tensor)
tensor.reshape(shape): 改变张量的形状

返回一个具有指定形状的新张量,原始张量的元素数量必须与新形状一致

import torch

tensor = torch.tensor([[1, 2], [3, 4]])
reshaped_tensor = tensor.reshape(1, 4)  # Reshapes the tensor to a 1x4 tensor
print(reshaped_tensor)
tensor.transpose(dim0, dim1): 交换两个维度

交换张量中两个维度的位置

import torch

tensor = torch.tensor([[1, 2], [3, 4]])
transposed_tensor = tensor.transpose(0, 1)  # Swaps the first and second dimensions
print(transposed_tensor)
tensor.permute(*dims): 按照指定顺序排列张量的维度

按照给定顺序重新排列张量的维度

import torch

tensor = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
permuted_tensor = tensor.permute(1, 0, 2)  # Permutes the dimensions to (1, 0, 2)
print(permuted_tensor)
tensor.squeeze(): 删除所有长度为1的维度

删除张量中所有长度为1的维度

import torch

tensor = torch.tensor([[[1, 2], [3, 4]]])
squeezed_tensor = tensor.squeeze()  # Removes the single-dimensional entries
print(squeezed_tensor)
tensor.unsqueeze(dim): 在指定位置增加一个维度

在指定位置增加一个长度为1的新维度

import torch

tensor = torch.tensor([[1, 2], [3, 4]])
unsqueezed_tensor = tensor.unsqueeze(0)  # Adds a dimension at index 0
print(unsqueezed_tensor)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一本糊涂张~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值