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

结束语

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

相关推荐
2601_965449084 分钟前
AI视频制作:做系列历史地图视频想固定音色,AI 音色克隆的流程实测
人工智能·音视频
2601_9620710018 分钟前
从OpenRouter到Crazyrouter:Claude Code三处改动
人工智能·ai
阿里云大数据AI技术23 分钟前
云栖实录|从代码上下文到千亿级文档:Qoder × 阿里云 Elasticsearch 落地生产级Agent向量检索
人工智能·elasticsearch
汤姆宝库25 分钟前
具身智能深拆:VLA、世界模型与数据飞轮
开发语言·人工智能·php
鲜于言悠90531 分钟前
Jev爆火:不生成文字,直接输出概率的决策模型
人工智能
美团技术团队31 分钟前
如何在“不能出错”的系统里使用 AI Coding?分布式存储实践与边界
人工智能
枫叶丹438 分钟前
AI 提出科学假设之后:从数据筛选到实验验证
人工智能·chatgpt·开源·agent·codex
kaixin_啊啊40 分钟前
【零基础学AI】第 19 章——用分类模型识别类别
人工智能·分类·数据挖掘
郝学胜-神的一滴1 小时前
AI 编程智能体 17:四步搞定专属AI智能体
大数据·人工智能·python·神经网络·算法·langchain·强化学习
vx153027823621 小时前
CDGA|AI时代,数据治理的位置下沉了吗?
大数据·人工智能·cdga·数据治理