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()
  • 测试结果我就不贴图了,太费事,直接运行自己看

结束语

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

相关推荐
刘晓倩22 分钟前
实战任务二:用扣子空间通过任务提示词制作精美PPT
人工智能
shut up25 分钟前
LangChain - 如何使用阿里云百炼平台的Qwen-plus模型构建一个桌面文件查询AI助手 - 超详细
人工智能·python·langchain·智能体
Hy行者勇哥27 分钟前
公司全场景运营中 PPT 的类型、功能与作用详解
大数据·人工智能
Larry_Yanan1 小时前
QML学习笔记(三十四)QML的GroupBox、RadioButton
c++·笔记·qt·学习·ui
FIN66681 小时前
昂瑞微:实现精准突破,攻坚射频“卡脖子”难题
前端·人工智能·安全·前端框架·信息与通信
FIN66681 小时前
昂瑞微冲刺科创板:硬科技与资本市场的双向奔赴
前端·人工智能·科技·前端框架·智能
im_AMBER1 小时前
杂记 14
前端·笔记·学习·web
m0_677034351 小时前
机器学习-推荐系统(下)
人工智能·机器学习
XIAO·宝1 小时前
深度学习------专题《神经网络完成手写数字识别》
人工智能·深度学习·神经网络
流年染指悲伤、1 小时前
2024年最新技术趋势分析:AI、前端与后端开发新动向
人工智能·前端开发·后端开发·2024·技术趋势