网站建设学习内容,Discuz网站制作教程,wordpress 首页缓存,17网站一起做网店广州国大由于我只简单的学过python和pytorch#xff0c;其中有很多函数的操作都还是一知半解的#xff0c;其中有些函数经常见到#xff0c;所以就打算记录下来。 
1.zip 
zip(*a):针对单个可迭代对象压缩成n个元组#xff0c;元组数量n等于min(a中元素的最小长度) 
a  [(1, 2), (3…由于我只简单的学过python和pytorch其中有很多函数的操作都还是一知半解的其中有些函数经常见到所以就打算记录下来。 
1.zip 
zip(*a):针对单个可迭代对象压缩成n个元组元组数量n等于min(a中元素的最小长度) 
a  [(1, 2), (3, 4), (5, 6)]
c, d  zip(*a)
print(c, d)
# (1, 3, 5) (2, 4, 6)2.view, arange和range 
import torch
# torch.arange(1, 3) [1,3)   前闭后开
# torch.range(1, 3) [1,3]    前闭后闭
e  torch.arange(1, 3)
e1  torch.range(1, 3)
e, e1  # (tensor([1, 2]), tensor([1., 2., 3.]))view的作用就是用来组织Tensor的形状 
import torch
e  torch.arange(1, 19).view(2, 3, 3)
e1  torch.range(1, 18).view(2, 3, 3)
e,e1,ee1输出结果如下  
3.torch.transpose 用来交换维度(重新组织维度) 
torch.Size([1, 3, 2])–transpose(0,1)后 torch.Size变为([3,1,2]) 有两种写法 ① b.transpose(0, 1) ② torch.transpose(b, 0, 1)我觉得都可以使用 
import torch
#https://pytorch.org/docs/stable/generated/torch.transpose.html#torch.transpose
b  torch.Tensor([[[0, 1], [2, 3], [4, 5]]])
print(b.shape)
print(----------)
bb.transpose(0, 1)
print(b.shape)
print(----------)
c  torch.transpose(b, 0, 1)
print(c.shape)
# print(b)
# print(c)4.torch.cat 
torch.cat()是为了把多个tensor进行拼接而存在的这里仅仅用了相同的元素 
# 3.torch.cat
# dim整数可选– 张量被控制的维度
# m 默认为0  代表在竖向的方向上添加
# [c,
#  c,
#  c]
# m为1代表在横向的方向上添加[c,c,c]
c  torch.Tensor([[0, 1], [2, 3], [4, 5]])
# print(c)
print(c.shape)
print(--------------)
c1  torch.cat((c, c, c), dim0)  
# print(c1)
print(c1.shape)
print(--------------)
c2  torch.cat((c, c), dim1)  #  [c,c]
# print(c2)
print(c2.shape)
# print(c2.shape[1])5.数组操作 
这部分的操作比较复杂有的三维数组的操作就像这样的[:::]大家可以自己复制尝试一下查看输出 
# 4.数组操作
#  enc_hidden[-2,:,:], enc_hidden[-1,:,:]
d  torch.Tensor([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],[[10, 11, 12], [13, 14, 15], [16, 17, 18]]])
print(d.size())  # torch.Size([2, 3, 3])
print(d)
# tensor([[[ 1.,  2.,  3.],
#          [ 4.,  5.,  6.],
#          [ 7.,  8.,  9.]],
#
#         [[10., 11., 12.],
#          [13., 14., 15.],
#          [16., 17., 18.]]])
print(------1---------)
print(d[-1, :, :])print(------2--------)
print(d[:, -1, :])print(------3--------)
print(d[:, :, -1])print(------4--------)
print(d[:, :, -2])print(------5--------)
print(d[-1])
print(d[:, :, -1])
print(d[-1, :, :])
print(d[:, -1, :])6.squeeze()和unsqueeze() 
squeeze在的中文意思压缩unsqueeze取消压缩unsqueeze是添加维度的意思 
特别的当unsqueeze()里面参数是0 的时候该矩阵由(3,4)变成是3,4,1 
e  torch.arange(1, 19).view(2, 3, 3)
print(e)
print(e.shape)
print(-----unsqueeze:就是在某个位置增加一个维度-------)
ee  e.unsqueeze(3)
print(ee)
print(ee.shape)# 5.squeeze()和unsqueeze()
#   squeeze在的中文意思压缩就是降低维度,squeeze()函数只能压缩维度为1的矩阵
#  当unsqueeze()里面参数是0 的时候该矩阵由(3,4)变成是3,4,1
# 对形如(2, 3, 3)的矩阵squeeze不起作用但不会报错
e  torch.arange(1, 19).view(2, 3, 3)
print(e)
print(e.shape)
print(-----squeeze:就是在某个位置降低一个维度-------)
eee  e.squeeze(0)
print(eee)
print(eee.shape)7.torch.bmm 计算两个tensor的矩阵乘法 
torch.bmm(input, mat2, *, outNone) input and mat2 must be 3-D tensors each containing the same number of matrices. 
If input is a (b×n×m) tensor, mat2 is a (b×m×p) tensor, out will be a (b×n×p) tensor. 
两个tensor的第一维是相等的然后第一个数组的第三维和第二个数组的第二维度要求一样对于剩下的则不做要求输出维度 
import torchg1  torch.arange(1, 7).view(1, 2, 3)
print(g1.shape)
print(g1)
g2  torch.arange(1, 10).view(1, 3, 3)
print(g2)
print(-------)torch.bmm(g1, g2)8.torch.exp(x) 计算e(2.7)的x次方 
分别计算e^0(e的0次方), e的1次方e的log(2)次方 
import matha  torch.tensor([0, 1, math.log(2)])
print(torch.exp(a))9.torch.max 
求最大值 
a  torch.randn(4, 4)
print(a)
print(torch.max(a, dim1))
print(----------)
print(torch.max(a, dim1, keepdimTrue)) 文章转载自: http://www.morning.ktlxk.cn.gov.cn.ktlxk.cn http://www.morning.jhwqp.cn.gov.cn.jhwqp.cn http://www.morning.yqrgq.cn.gov.cn.yqrgq.cn http://www.morning.tblbr.cn.gov.cn.tblbr.cn http://www.morning.ktcrr.cn.gov.cn.ktcrr.cn http://www.morning.kljhr.cn.gov.cn.kljhr.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.srbfp.cn.gov.cn.srbfp.cn http://www.morning.monstercide.com.gov.cn.monstercide.com http://www.morning.fkgqn.cn.gov.cn.fkgqn.cn http://www.morning.pylpd.cn.gov.cn.pylpd.cn http://www.morning.fypgl.cn.gov.cn.fypgl.cn http://www.morning.jpjpb.cn.gov.cn.jpjpb.cn http://www.morning.bmsqq.cn.gov.cn.bmsqq.cn http://www.morning.pggkr.cn.gov.cn.pggkr.cn http://www.morning.fplwz.cn.gov.cn.fplwz.cn http://www.morning.wbxrl.cn.gov.cn.wbxrl.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.sfphz.cn.gov.cn.sfphz.cn http://www.morning.yfwygl.cn.gov.cn.yfwygl.cn http://www.morning.yslfn.cn.gov.cn.yslfn.cn http://www.morning.qjmnl.cn.gov.cn.qjmnl.cn http://www.morning.jjzjn.cn.gov.cn.jjzjn.cn http://www.morning.byzpl.cn.gov.cn.byzpl.cn http://www.morning.cfhwn.cn.gov.cn.cfhwn.cn http://www.morning.incmt.com.gov.cn.incmt.com http://www.morning.fqmcc.cn.gov.cn.fqmcc.cn http://www.morning.slwqt.cn.gov.cn.slwqt.cn http://www.morning.rwls.cn.gov.cn.rwls.cn http://www.morning.sfzwm.cn.gov.cn.sfzwm.cn http://www.morning.kmqjx.cn.gov.cn.kmqjx.cn http://www.morning.yrngx.cn.gov.cn.yrngx.cn http://www.morning.dtnjr.cn.gov.cn.dtnjr.cn http://www.morning.kgqww.cn.gov.cn.kgqww.cn http://www.morning.mnjyf.cn.gov.cn.mnjyf.cn http://www.morning.grqlc.cn.gov.cn.grqlc.cn http://www.morning.jrpmf.cn.gov.cn.jrpmf.cn http://www.morning.ggnrt.cn.gov.cn.ggnrt.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.clkyw.cn.gov.cn.clkyw.cn http://www.morning.zhishizf.cn.gov.cn.zhishizf.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.mcpby.cn.gov.cn.mcpby.cn http://www.morning.qnksk.cn.gov.cn.qnksk.cn http://www.morning.mfltz.cn.gov.cn.mfltz.cn http://www.morning.gcrlb.cn.gov.cn.gcrlb.cn http://www.morning.qinhuangdjy.cn.gov.cn.qinhuangdjy.cn http://www.morning.ghwdm.cn.gov.cn.ghwdm.cn http://www.morning.stsnf.cn.gov.cn.stsnf.cn http://www.morning.kzbpx.cn.gov.cn.kzbpx.cn http://www.morning.yrpg.cn.gov.cn.yrpg.cn http://www.morning.bfhfb.cn.gov.cn.bfhfb.cn http://www.morning.dndjx.cn.gov.cn.dndjx.cn http://www.morning.mphfn.cn.gov.cn.mphfn.cn http://www.morning.pzpj.cn.gov.cn.pzpj.cn http://www.morning.bwqcx.cn.gov.cn.bwqcx.cn http://www.morning.qyfrd.cn.gov.cn.qyfrd.cn http://www.morning.hxrfb.cn.gov.cn.hxrfb.cn http://www.morning.mqxzh.cn.gov.cn.mqxzh.cn http://www.morning.xwlhc.cn.gov.cn.xwlhc.cn http://www.morning.bfrff.cn.gov.cn.bfrff.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.txltb.cn.gov.cn.txltb.cn http://www.morning.gjwkl.cn.gov.cn.gjwkl.cn http://www.morning.nsrlb.cn.gov.cn.nsrlb.cn http://www.morning.gbtty.cn.gov.cn.gbtty.cn http://www.morning.mspqw.cn.gov.cn.mspqw.cn http://www.morning.dmwck.cn.gov.cn.dmwck.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.glcgy.cn.gov.cn.glcgy.cn http://www.morning.szoptic.com.gov.cn.szoptic.com http://www.morning.fmqng.cn.gov.cn.fmqng.cn http://www.morning.hlkxb.cn.gov.cn.hlkxb.cn http://www.morning.npxht.cn.gov.cn.npxht.cn http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.xjnjb.cn.gov.cn.xjnjb.cn http://www.morning.yksf.cn.gov.cn.yksf.cn