Git Product home page Git Product logo

pytorchlearning's People

Contributors

clnfind avatar

Watchers

 avatar  avatar

pytorchlearning's Issues

pytorch tensor 维度理解

torch.randn

  • torch.randn(*sizes, out=None) → Tensor(张量)

  • 返回一个张量,包含了从标准正态分布(均值为0,方差为 1)中抽取一组随机数,形状由可变参数sizes定义。 参数:

    • sizes (int...) – 整数序列,定义了输出形状
    • out (Tensor, optinal) - 结果张量

二维

>>> import torch
>>> torch.randn(2,3)
tensor([[-1.0413,  0.8792,  2.1381],
             [ 0.9541, -2.3019,  0.5490]])
>>>

三维

>>> torch.randn(2,2,3)
tensor([[[ 0.4200,  0.4624,  0.3099],
               [-0.1227,  0.2452,  0.9840]],

             [[-0.8800, -0.5937, -1.4465],
              [ 1.6523, -0.0170, -0.6393]]])
>>>
  • 三维基于二维增加了一维,即(2,2,3)是包含了2个2行3列的矩阵;第一个数字即指包含几个二维矩阵

四维

>>> torch.randn(2,2,2,3)
tensor([[[[-1.1649, -1.1810, -0.3619],
               [-0.8433, -0.4411,  1.8187]],

              [[ 0.4896,  0.4773,  0.0032],
                 [ 1.1269,  1.3638,  1.4495]]],


            [[[-0.1959,  0.5646,  0.7001],
                [ 0.6796,  0.1164,  1.6833]],

             [[-0.2674, -0.2411,  1.5875],
              [-0.2804,  1.4775,  0.2448]]]])
>>>
  • 四维基于三维增加了一维,(2,2,2,3)即包含了2个三维的矩阵。
>>> a = torch.randn(2,2,2,3)
>>> torch.numel(a)             # 计算tensor的元素个数(即矩阵的元素个数)
24
  • 若是n维,以此类推,基于上一维增加一维计算。

pytorch 图片处理

➜  torch_test tree animal_data
animal_data
├── train
│   ├── ants
│   │   ├── 0013035.jpg
│   │   ├── 1030023514_aad5c608f9.jpg
│   │   ├── 1095476100_3906d8afde.jpg
│   └── bees
│       ├── 1092977343_cb42b38d62.jpg
│       ├── 1093831624_fb5fbe2308.jpg
│       ├── 1097045929_1753d1c765.jpg
└── val
    ├── ants
    │   ├── 10308379_1b6c72e180.jpg
    │   ├── 1053149811_f62a3410d3.jpg
    │   ├── 1073564163_225a64f170.jpg
    └── bees
        ├── 1032546534_06907fe3b3.jpg
        ├── 759745145_e8bc776ec8.jpg
        └── abeja.jpg
  • 加载图片处理,用到 datasets.ImageFolder() 方法,接受的参数有(path, transform, target_transform),该方法源代码如下:
    image
  • 加载方法如下:
    image
  • transform处理后的结果如下:
    image
  • 对transform后的数据做进一步处理,DataLoader处理:
    image
    结果如下:
    image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.