惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
J
Java Code Geeks
Engineering at Meta
Engineering at Meta
博客园_首页
G
Google Developers Blog
有赞技术团队
有赞技术团队
Vercel News
Vercel News
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
Jina AI
Jina AI
IT之家
IT之家
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
A
About on SuperTechFans
The Register - Security
The Register - Security
腾讯CDC
月光博客
月光博客
GbyAI
GbyAI
博客园 - 叶小钗
Blog — PlanetScale
Blog — PlanetScale
D
Docker
D
DataBreaches.Net
博客园 - 聂微东
C
Check Point Blog
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
F
Full Disclosure
Martin Fowler
Martin Fowler
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网
美团技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 司徒正美
小众软件
小众软件
量子位
L
LangChain Blog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
F
Fortinet All Blogs

编程与诗|飒白的个人博客|飒白的闲话仓库|一位主播和程序员的奇妙组合体

侯捷c++笔记:c++泛型与高级用法 侯捷c++笔记:c++面向对象的指针类 侯捷c++笔记:c++面向对象的无指针类 03《穷查理宝典》 查理·芒格丨读书笔记 02《穷爸爸富爸爸》 罗伯特·清崎丨读书笔记 01《影响力》罗伯特・西奥迪尼丨读书笔记 基于结构光投影三维重建:格雷码编码与解码 给CNN添加TVloss的随记 三维重建论文笔记 DeMoN:Depth and Motion Network for Learning Monocular Stereo 三维扫描系列 点云绪论 修改DCNN使其输入从无监督变为有监督模型 茫茫摄影路:好照片的基本三要素 pytorch学习日记:张量数据类型 研究生涯005 利用蒙特卡洛方法实现21点问题的最优解 研究生涯004 基于深度学习的水质图像分类 研究生涯003 基于最近邻法手写数字识别设计 研究生涯002 汽车模糊推理系统实验 研究生涯001 浅析机器视觉在医疗影像处理中的应用 人工智能问答NO.8 决策树与随机森林
pytorch学习日记:创建Tensor
飒白 · 2021-01-06 · via 编程与诗|飒白的个人博客|飒白的闲话仓库|一位主播和程序员的奇妙组合体

import from Numpy

>>> a=np.array([2,3.3])
>>> torch.from_numpy(a)
tensor([2.0000, 3.3000], dtype=torch.float64)
>>> a=np.ones([2,3])
>>> torch.from_numpy(a)
tensor([[1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)

import from List

>>> torch.tensor([2,3.2])#torch.tensor接受现有数据,Tensor和FloatTensor接受一个shape(数据的维度)
tensor([2.0000, 3.2000])
>>> torch.FloatTensor([2.,3.2])#尽量不要使用
tensor([2.0000, 3.2000])
>>> torch.tensor([2,3.2])
tensor([2.0000, 3.2000])
>>> torch.tensor([[2,3.2],[3.2,1.]])
tensor([[2.0000, 3.2000],
        [3.2000, 1.0000]])

uninitialized

  • Torch.empty()
  • Torch.FloatTensor(d1,d2,d3)
    • Not torch.FloatTensor([1,2])=torch.tensor([1,2])
  • Torch.IntTensor(d1,d2,d3)
>>> torch.empty(1)
tensor([nan])
>>> torch.Tensor(2,3)
tensor([[0., 0., 0.],
        [0., 0., 0.]])
>>> torch.IntTensor(2,3)
tensor([[1064135033, 1056173720, 1064928610],
        [1038606400, 1060693140, 1040603820]], dtype=torch.int32)
>>> torch.FloatTensor(2,3)
tensor([[0., 0., 0.],
        [0., 0., 0.]])
  • 后续要使用其他数据覆盖掉未初始化数据

set default type

>>> torch.tensor([1.2,3]).type()
'torch.FloatTensor'
>>> torch.set_default_tensor_type(torch.DoubleTensor)
>>> torch.tensor([1.2,3]).type()
'torch.DoubleTensor'

rand/rand_like,randint

  • rand产生不包括1

    >>> a=torch.rand(3,3)
    >>> a
    tensor([[0.4744, 0.7030, 0.8309],
            [0.7480, 0.0447, 0.8336],
            [0.7141, 0.0262, 0.5663]])
    >>> torch.rand_like(a)
    tensor([[0.4703, 0.5290, 0.2008],
            [0.7135, 0.9063, 0.4426],
            [0.0830, 0.7966, 0.8128]])
    >>> torch.randint(1,10,[3,3])
    tensor([[8, 2, 4],
            [2, 5, 1],
            [3, 5, 2]])

randn

>>> torch.randn(3,3)#均值为0,方差为1
tensor([[ 0.4364,  0.0172,  1.0050],
        [ 0.2373, -2.0858, -0.9249],
        [ 0.8681,  1.2555, -1.0074]])

full

>>> torch.full([2,3],7)
tensor([[7, 7, 7],
        [7, 7, 7]])
>>> torch.full([],7)
tensor(7)
>>> torch.full([1],7)
tensor([7])

arange/range

>>> torch.arange(0,10)
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> torch.arange(0,10,2)
tensor([0, 2, 4, 6, 8])

linspace/logspace

>>> torch.linspace(0,10,steps=4)
tensor([ 0.0000,  3.3333,  6.6667, 10.0000])
>>> torch.linspace(0,10,steps=10)
tensor([ 0.0000,  1.1111,  2.2222,  3.3333,  4.4444,  5.5556,  6.6667,  7.7778,
         8.8889, 10.0000])
>>> torch.linspace(0,10,steps=11)
tensor([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
>>> torch.linspace(0,-1,steps=10)
tensor([ 0.0000, -0.1111, -0.2222, -0.3333, -0.4444, -0.5556, -0.6667, -0.7778,
        -0.8889, -1.0000])
>>> #生成10的0次方为起始值,10的-1次方为终止值的8个数构成的等比数列
... c = torch.logspace(0,-1,steps=8)
>>> print(c)
tensor([1.0000, 0.7197, 0.5179, 0.3728, 0.2683, 0.1931, 0.1389, 0.1000])

Ones/zeros/eye

>>> torch.ones(3,3)
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
>>> torch.zeros(3,3)
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
>>> torch.eye(3,3)#对角矩阵
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

randperm

  • random.shuffle nmpuy
>>> a=torch.rand(2,3)
>>> b=torch.rand(2,2)
>>> idx=torch.randperm(2)
>>> idx
tensor([0, 1])
>>> idx
tensor([0, 1])
>>> a[idx]
tensor([[0.9459, 0.4927, 0.4129],
        [0.3428, 0.3058, 0.5658]])
>>> b[idx]
tensor([[0.1292, 0.6434],
        [0.9975, 0.4396]])
>>> a,b
(tensor([[0.9459, 0.4927, 0.4129],
        [0.3428, 0.3058, 0.5658]]), tensor([[0.1292, 0.6434],
        [0.9975, 0.4396]]))