pytorch | Tensor和Variable

pytorch | Tensor和Variable,第1张

Tensor是pytorch中最基本的构件,可以像numpy一样进行矩阵计算,最主要的是Tensor支持GPU加速运算。而且Tensor的运算与numpy的运算基本相似,但有部分高级的矩阵计算Tensor并不支持,比如计算特征值特征向量等。因此numpy还是有存在的必要的。

tensor是Pytorch中非常高效数据格式,但用tensor构建神经网络还远远不够,为了构建计算图,所以Variable是不可或缺的数据形式。Variable是对tensor的封装。

Variable有三个属性:

使用对数,从start到end。Python是一门解释型、面向对象、带有动态语义的高级程序设计语言,python使用对数,从start到end把张量转为普通值,Python提供了高效的高级数据结构,还能简单有效地面向对象编程。

可以利用in运算符来进行判断。

如果在指定的序列中找到值返回True,否则返回False。运算符notin表示如果在指定的序列中没有找到值返回True,否则返回False。

Python字典in *** 作符用于判断键是否存在于字典中,如果键在字典dict里返回true,否则返回false。此方法用于列表可以判断元素是否在列表中。

torchcat(seq,dim=0,out=None)->Tensor

在给定维度上对输入张量序列seq进行连接 *** 作由这可以想到卷积神经网络

的全连接层

torchcat() 可以看做 torchsplit() 和 torchchunk() 的逆运算

torchchunk(tensor,chunks,dim)->>tensors

将张量沿给定维度分块

torchgather(input,dim,index,out=None,sparse_grad=Flase,out=None)->Tensor

通俗点解释就是把指定索引dim的下标进行替换

torchindex_select(input,dim,index,out=None)->>Tensor

torchmasked_select(input,mask,out=None)->>Tensor

根据mask输出一个一维张量

torchsplit(tensor,split_size,dim=0)->>tensor

如果可分,张量沿着指定维度指定大小进行分割,直到大小不足则停止

torcht(input,out=None)->Tensor

张量转置相当于

torchtranspose(input,o,1)

torchbernoulli(input,out=None)

从伯努利分布中抽取二元随机数(0或者1)这里的bernoulli概率p是随机的

输入张量值需是一个概率

torchmultinomial(input,num_samples,replacement=Flase,out=None)->>LongTensor

从输入张量中每行取num_samples个样本,可以设置replacement设置是否重复取值

返回取值的下标

torchnormal(means,std,out)->>tensor

按照指定均值和方差选取样本,均值个数决定样本个数

若均值和方差都为张量则两个张量元素个数必须相等

torchabs(input,out)->tensor 输出张量元素绝对值

torchacos(input,out) 求反余弦

torchadd(input,value,out) 对每个张量元素逐个加上value

torchaddcdiv(tensor,value=1,tensor1,tensor2) 张量(tensor1/tensor2)value+tensor

torchaddmul 相乘相加

torchceil(input,out) 向上取整

torchclamp(input,min,max,out=None)

将元素调整至[min,max]区间

torchdiv(input,value) 除

torchexp(tensor,out) 指数

torchfloor(input,out) 向下去整

torchfmod(input,divisor,out) 取余数

torchfrac 取分数部分

torchlerp(start, end, weight, out=None)

线性插值:out = start+weight(end-start)

torchlog 取自然对数

torchmul(input, value, out=None)

torchmul(input, other, out=None) 哈达玛积

torchneg 取复数

torchpow(input, exponent, out=None) 求幂

torchreciprocal(input, out=None) → Tensor 去倒数

torchremainder(input, divisor, out=None) → Tensor 取余数

torchrsqrt(input, out=None) → Tensor 平方根倒数

torchsigmoid(input, out=None) → Tensor sigmoid值

torchsigmoid(input, out=None) → Tensor 符号函数

torchcumprod(input, dim, out=None) → Tensor 按指定维度累积

torchcumsum(input, dim, out=None) → Tensor 指定维度累加

torchdist(input, other, p=2, out=None) → Tensor 求P范数

torchmean(input) → float 均值

torchmean(input, dim, out=None) → Tensor 指定维度均值

torchmedian(input, dim=-1, values=None, indices=None) -> (Tensor, LongTensor) 指定维度中位数

torchmode(input, dim=-1, values=None, indices=None) -> (Tensor, LongTensor) 众数

torchnorm(input, p, dim, out=None) → Tensor 指定维度p范数

torchprod(input) → float 所有积

torchprod(input, dim, out=None) → Tensor 指定维度积

torchstd(input, dim, out=None) → Tensor 标准差

torchsum(input, dim, out=None) → Tensor 按维度求和

torchsum(input) → float 所有元素和

var 按行方差,所有元素方差

torcheq(input, other, out=None) → Tensor 相等比较 *** 作 返回01

torchequal(tensor1, tensor2) → bool 张量比较shape and value返回bool

torchge(input, other, out=None) → Tensor 大于

torchgt(input, other, out=None) → Tensor 与equal类似返回不同

torchkthvalue(input, k, dim=None, out=None) -> (Tensor, LongTensor) 取指定维度最小值

torchle(input, other, out=None) → Tensor 小于等于

torchlt(input, other, out=None) → Tensor 小于

torchmax(input, dim, max=None, max_indices=None) -> (Tensor, LongTensor) 返回指定维度最大值和索引

以上就是关于pytorch | Tensor和Variable全部的内容,包括:pytorch | Tensor和Variable、python怎么把张量转为普通值、python如何判断tensor是否存在某个元素等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9518771.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存