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

结束语

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

相关推荐
qq_1629115914 分钟前
tigase源码学习杂记-IO处理的线程模型
java·学习·源码·xmpp·tigase·多线程io模型
滴水成川16 分钟前
基于 BERT 微调一个意图识别(Intent Classification)模型
人工智能·深度学习·bert
小oo呆16 分钟前
【自然语言处理与大模型】LangChain大模型应用框架入门①
人工智能·自然语言处理·langchain
2401_8786247917 分钟前
opencv 模板匹配
人工智能·opencv·计算机视觉
layneyao19 分钟前
AI艺术创作:Midjourney、Stable Diffusion与商业变现
人工智能·stable diffusion·midjourney
viperrrrrrrrrr724 分钟前
大数据学习(115)-hive与impala
大数据·hive·学习·impala
余弦的倒数26 分钟前
大语言模型(LLMs)微调技术总结
人工智能·语言模型·chatgpt
Dovis(誓平步青云)40 分钟前
AI遇见端动态神经网络:Cephalon(联邦学习+多模态编码)认知框架构建
图像处理·人工智能·机器学习·微服务·语言模型·数据挖掘·交互
gs8014044 分钟前
什么是 MCP?AI 应用的“USB-C”标准接口详解
人工智能