pytorch学习问题汇总

摘要:
torch.bernoulli怎么是这个结果?问题1:torch各个类型数据格式如何转换?#尝试一i32=torch.IntTensori64=torch.LongTensor#两种转换都报错#new_i64=torch.IntTensor#new_i32=torch.LongTensor#didn'tmatchbecausesomeoftheargumentshaveinvalidtypes:(!)#尝试二new_i32=i32.long()print#True#torch.Tensor对应八种数据转换,各种数据可以相互转换i32.float()i32.double()i32.half()i32.byte()i32.char()i32.short()i32.int()i32.long()问题2:官方文档中sequenceoftensors是什么意思?在torch.stack.是tensors构成的序列,可以为列表,也可以为元组。#torch.stack连接Tensorsi32=torch.Tensorprint#默认dim=0,以列为基准#123#123#123#[torch.FloatTensorofsize3x3]print#111#222#333#[torch.FloatTensorofsize3x3]print#111#222#333#[torch.FloatTensorofsize3x3]问题3:为什么有如下Tensor格式区别?有的是size3,有的是size4x1?torch.from_numpy#torch.IntTensorofsize3torch.from_numpy#torch.DoubleTensorofsize3torch.nonzero==torch.Tensor#nonzero非0元素所在位置#TypeError:eqreceivedaninvalidcombinationofarguments-got,butexpectedoneof:#*#didn'tmatchbecausesomeoftheargumentshaveinvalidtypes:(!判断两个Tensor是否相等,用equal问题4、

问题六:

问题五:这里是怎么得到的?

pytorch学习问题汇总第1张

问题四:为什么会是如下结果?

pytorch学习问题汇总第2张

torch.bernoulli(a)怎么是这个结果?

问题1:torch各个类型数据格式如何转换?数据类型在官方文档torch.Tensor中,有八种类型。

#尝试一
i32=torch.IntTensor([1,2,3])
i64=torch.LongTensor([1,2,3])
#两种转换都报错#new_i64=torch.IntTensor(i64)#new_i32=torch.LongTensor(i32)#didn't match because some of the arguments have invalid types: (!torch.LongTensor!)

#尝试二
new_i32=i32.long()
print(torch.equal(new_i32,i64))  #True#torch.Tensor对应八种数据转换,各种数据可以相互转换
i32.float()
i32.double()
i32.half()
i32.byte()
i32.char()
i32.short()
i32.int()
i32.long()

问题2:官方文档中sequence of tensors是什么意思?在torch.stack(sequence,dim=0,out=None).

是tensors构成的序列,可以为列表,也可以为元组

#torch.stack(sequence, dim=0, out=None) 连接Tensors
i32=torch.Tensor([1,2,3])
print(torch.stack([i32,i32,i32]))  #默认dim=0,以列为基准#1  2  3#1  2  3#1  2  3#[torch.FloatTensor of size 3x3]
print(torch.stack([i32,i32,i32],dim=1))
#1  1  1#2  2  2#3  3  3#[torch.FloatTensor of size 3x3]
print(torch.stack((i32,i32,i32),dim=1))
#1  1  1#2  2  2#3  3  3#[torch.FloatTensor of size 3x3]

问题3:为什么有如下Tensor格式区别?有的是size 3,有的是size4x1 ?

torch.from_numpy(np.array([1,2,3]))   #torch.IntTensor of size 3
torch.from_numpy(np.array([1.0,2,3])) #torch.DoubleTensor of size 3
torch.nonzero(torch.Tensor([1,2,3,0,4]))==torch.Tensor([0,1,2,4])  #nonzero  非0元素所在位置
# TypeError: eq received an invalid combination of arguments - got (torch.FloatTensor), but expected one of:
#  * (int value)
#       didn't match because some of the arguments have invalid types: (!torch.FloatTensor!)
#  * (torch.LongTensor other)
#       didn't match because some of the arguments have invalid types: (!torch.FloatTensor!)
#注意上面代码中两者数据格式类型不一致,torch.FloatTensor   torch.LongTensor
#torch.unsqueeze(input,dim,out=None)
m=torch.Tensor([1,2,3,4])
print(m)                     #torch.FloatTensor of size 4
m_zero=torch.unsqueeze(m,0)
print(m_zero)                #torch.FloatTensor of size 1x4
m_one=torch.unsqueeze(m,1)
print(m_one)                 #torch.FloatTensor of size 4x1
m_zero_to_m=torch.squeeze(m_zero)
print(m_zero_to_m)           #torch.FloatTensor of size 4
print(m==m_zero_to_m)        #torch.ByteTensor of size 4
# 1
# 1
# 1
# 1
print(m.equal(m_zero_to_m))  True

可见为两种不同数据类型,可以通过unsqueeze和squeeze来相互转化。判断两个Tensor是否相等,用equal

问题4、

免责声明:文章转载自《pytorch学习问题汇总》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇详解linux进程间通信-消息队列三菱5U PLC学习笔记下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

神经网络中的降维和升维方法 (tensorflow & pytorch)

  大名鼎鼎的UNet和我们经常看到的编解码器模型,他们的模型都是先将数据下采样,也称为特征提取,然后再将下采样后的特征恢复回原来的维度。这个特征提取的过程我们称为“下采样”,这个恢复的过程我们称为“上采样”,本文就专注于神经网络中的下采样和上采样来进行一次总结。写的不好勿怪哈。 神经网络中的降维方法 池化层   池化层(平均池化层、最大池化层),卷积...

torch笔记合集

Torch笔记 import torch import numpy as np import torch.nn as nn a_np = np.random.rand(10,100) numpy知识回顾 a_np.dtype # 数据类型 a_np.ndim #维度个数 a_np.shape # 形状 整数元祖 a_np.dtype=np.int...

pyTorch进阶-torch

一、Broadcast自动扩展 Expand和unsquee的结合,习惯性行是高维度,列是低维度 example: 小维度指定,大维度随意 二、拼接与拆分 Cat Stack:增加新的维度 Split(按照长度进行拆分) Chunk(按照数量进行拆分) torch.stack torch.stack(sequence, dim=0) 参数:...

Pytorch-Tensor基本操作

(此文为个人学习pytorch时的笔记,便于之后的查询) Tensor基本操作 创建tensor: ​ 1.numpy向量转tensor: a=np.array([2,2,2]) b=torch.from_numpy(a) ​ 2.列表转tensor: a=torch.tensor([2,2]) b=torch.FloatTensor([2,2.])#不...

《StackGAN》

StackGAN 周枫 少年,愿有一天,你能用内心的沉稳安宁,洗去身上的躁动和铅华 已关注 12 人赞同了该文章 未经授权,严禁任何形式转载!能力有限,欢迎指正批评! 参考 StackGAN: Text to Photo-realistic Image Synthesis with Stacked Generative Adversa...

pytorch和tensorflow的爱恨情仇之张量

pytorch和tensorflow的爱恨情仇之基本数据类型:https://www.cnblogs.com/xiximayou/p/13759451.html pytorch版本:1.6.0 tensorflow版本:1.15.0 基本概念:标量、一维向量、二维矩阵、多维张量。 1、pytorch中的张量 (1)通过torch.Tensor()来建立常量...