PyTorch 入门学习数据操作之创建

简介

在深度学习中,我们通常会频繁地对数据进行操作;要操作一般就需要先创建。

官方介绍

The torch package contains data structures for multi-dimensional tensors and defines mathematical operations over these tensors. Additionally, it provides many utilities for efficient serialization of Tensors and arbitrary types, and other useful utilities

It has a CUDA counterpart, that enables you to run your tensor computations on an NVIDIA GPU with compute capability >= 3.0

我的介绍

在 PyTorch 中,torch.Tensor是进行存储和进行变换数据的主要工具

  • tensor 是什么意思?上翻译:

    一般可译作张量,张量可以看作是一个多维数组

创建

  • 这里直接上代码
python 复制代码
# 导入PyTorch
import torch



"""
    官方文档地址:https://pytorch.org/docs/2.1/torch.html#creation-ops
"""


#
def create_empty_torch(a,b):
    """
    Args:
    a:
    b:
    创建一个 [a] x [b] 的未初始化的 Tensor
    :return: Returns a tensor filled with uninitialized data.
    """

    empty = torch.empty(a,b)
    print(empty)



def create_zero_torch():
    """
    创建一个 7x5 的 long 类型全是 0 的 Tensor
    Returns:
        Returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size
    """

    zero = torch.zeros(7,5,dtype=torch.long)
    print(zero)

def create_data_torch():
    """
    Constructs a tensor with no autograd history (also known as a "leaf tensor", see Autograd mechanics) by copying data
    :return:
    """
    data = torch.tensor([12.5,7])
    print(data)

def create_data_2_torch():
    data = torch.tensor([12.5, 7])
    # 返回的 tensor 默认具有相同的 torch.dtype 和 torch.device
    data = data.new_ones(2, 1, dtype=torch.float64)
    print(data)
    # 指定新的数据类型
    data = torch.randn_like(data, dtype=torch.float)
    print(data)


if __name__ == '__main__':
    create_data_2_torch()
  • 测试结果我就不贴图了,太费事,直接运行自己看

结束语

本系列教程仅针对入门初学者或者非此行业人员,敬请期待!

相关推荐
DevNo1 分钟前
学生党课堂复习利器:三款音视频转文字工具实测对比
人工智能
谢白羽1 分钟前
Mooncake在LLM的kv cache offload
人工智能·llm·论文·agent·mooncake
IT_陈寒5 分钟前
Vue的v-for为啥把我的渲染顺序搞乱套了?
前端·人工智能·后端
Yangs_noerr6 分钟前
Hermes Agent 打造私人 AI 助手
人工智能
慕容引刀7 分钟前
或许你真的需要这个Git神器:让团队提交文案终于对齐了
人工智能·git·vscode·自然语言处理·github
Jaeger_8 分钟前
MCP 配置到底要重复配几次?一次配置,Claude/Cursor/Codex 全通用
人工智能
GeekPlusA11 分钟前
ai-opendesign学习
学习
Python私教17 分钟前
四个入口,一条流水线:如意智影的多入口架构取舍
人工智能·python·架构
XiaQ091917 分钟前
Linux 常用命令学习笔记(用户管理篇):用户、组与权限的钥匙
linux·开发语言·笔记·学习
软件聚导航18 分钟前
软件聚导航-前端工程师的“小家庭”,工具与生活并存
人工智能·小程序·学习方法·图片压缩