当前位置: 首页 > news >正文

pytorch中张量的有关操作

pytorch中张量的有关操作

  • 创建张量
    • torch.tensor(data): 从数据创建张量
    • torch.zeros(size): 创建元素全为0的张量
    • torch.ones(size): 创建元素全为1的张量
    • torch.empty(size): 创建未初始化的张量
    • torch.randn(size): 创建服从标准正态分布的张量
    • torch.arange(start, end, step): 创建一个范围内的一维张量
    • torch.linspace(start, end, steps): 创建一个在指定范围内均匀间隔的张量
  • 张量属性相关
    • .dtype: 获取张量的数据类型
    • .shape: 获取张量的形状
    • .device: 获取张量所在的设备
  • 张量索引、切片与拼接
    • tensor[index]: 索引操作
    • 使用切片来获取张量的子张量
    • 沿着指定维度将多个张量连接在一起
    • 在一个新的维度上堆叠多个张量
  • 张量变换
    • tensor.view(shape): 返回给定形状的张量视图
    • 返回一个具有指定形状的新张量,原始张量的元素数量必须与新形状一致
    • 交换张量中两个维度的位置
    • 按照给定顺序重新排列张量的维度
    • 删除张量中所有长度为1的维度
    • 在指定位置增加一个长度为1的新维度

创建张量

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

张量属性相关

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

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

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

.shape: 获取张量的形状

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

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

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

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

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

张量索引、切片与拼接

tensor[index]: 索引操作

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

import torchtensor = 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 torchtensor = 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 torchtensor1 = 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 torchtensor1 = 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 torchtensor = 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 torchtensor = 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 torchtensor = 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 torchtensor = 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 torchtensor = torch.tensor([[[1, 2], [3, 4]]])
squeezed_tensor = tensor.squeeze()  # Removes the single-dimensional entries
print(squeezed_tensor)
tensor.unsqueeze(dim): 在指定位置增加一个维度

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

import torchtensor = torch.tensor([[1, 2], [3, 4]])
unsqueezed_tensor = tensor.unsqueeze(0)  # Adds a dimension at index 0
print(unsqueezed_tensor)
http://www.lryc.cn/news/458416.html

相关文章:

  • Windows多线程编程 互斥量和临界区使用
  • Java中集合类型的转换
  • 汽车售后TPMS浅谈
  • LUCEDA IPKISS Tutorial 77:在版图一定范围内填充dummy
  • TON生态小游戏开发:推广、经济模型与UI设计的建设指南
  • Python 量子机器学习:基础概念、关键算法与应用实践
  • 信息安全数学基础(29) x^2 + y^2 = p
  • ChatGPT国内中文版镜像网站整理合集(2024/10/06)
  • 图文深入理解Oracle DB Scheduler
  • gin如何具体利用Server-Send-Events(SSE)实时推送技术实现消息推送
  • 写端口-tcp udp不同方式发包和接包
  • 计算机的错误计算(一百二十)
  • Spring Boot 中使用 JSON Schema 来校验复杂 JSON 数据
  • QT实现Opencv图像处理
  • 刚转Mac的新手如何卸载不需要的应用程序
  • Unity 3d 继承MonoBahaviour的单例
  • grafana version 11.1.0 设置Y轴刻度为1
  • Elasticsearch的安装与配置
  • win0删除 Windows.old
  • 常见IDE及其编译器的讲解
  • 用SQLyog连接mysql提示2058错误
  • Web集群服务-Nginx
  • 获取时隔半个钟的三天
  • 构建可以ssh连接的容器镜像
  • 数据库中JOIN的用法?
  • java项目之纺织品企业财务管理系统源码(springboot+vue+mysql)
  • C语言 编程练习:解决五个有趣的问题
  • 二、安装vmtools
  • 用echarts画天气预报
  • 如果要存IP地址,用什么数据类型比较好?(java)