装修公司的网站怎么做,深圳网站制作 公司,学建设网站去哪里学,德州品牌策划公司torch.max函数的用法 第一种第二种 官方介绍#xff1a;Link
有两种使用场景#xff0c;输入的参数不同以及返回值不同#xff1a;
第一种
没有参数dim#xff0c;但这种只适合一维张量。 torch.max(input) → Tensor Returns the maximum value of all elements in the… torch.max函数的用法 第一种第二种 官方介绍Link
有两种使用场景输入的参数不同以及返回值不同
第一种
没有参数dim但这种只适合一维张量。 torch.max(input) → Tensor Returns the maximum value of all elements in the input tensor.
举例 a torch.randn(1, 3)a
tensor([[ 0.6763, 0.7445, -2.2369]])torch.max(a)
tensor(0.7445)第二种
指定了参数dim这种就适合多维张量了。
Notesdim参数的值跟函数选取最大值的结果关系。我觉的还是挺让我意外的和我想的不太一样。 torch.max(input, dim, keepdimFalse, *, outNone) 这种情况下函数会返回一个元组valuesindices其中每一个value是input张量中在给定的dim维度中的最大值。并且indices是找到的每一个最大值的索引。 如果keepdimTrue那么输出的tensors和input保持相同的size除了在dim维度上size为1哦否则如果keepdimFalse那么dim所在的维度是会被squeeze的也就是输出的tensors比input少一个维度。
Notes但是再次注意dim的数值和挑选最大值方式之间的关系。请看下面的例子
import torchtensor torch.randn(4, 4)
tensortensor([[ 0.1789, 0.7102, 0.7627, 0.4721],[-0.2287, -0.7618, 0.1439, -0.5439],[-0.4963, 0.3786, 0.1666, -0.5676],[ 0.6240, 0.0017, 1.0748, 0.4061]])torch.max(tensor, dim1)torch.return_types.max(
valuestensor([0.7627, 0.1439, 0.3786, 1.0748]),
indicestensor([2, 2, 1, 2]))所以从这个结果可以看出对于这个二维张量而言dim1表示最大值的选取方式是固定行然后从所有列中选取最大值。
再举一个三维数组的例子看看
import torch
mine torch.rand(3, 4, 4)
minetensor([[[0.0945, 0.1062, 0.1506, 0.1382],[0.2846, 0.4346, 0.1247, 0.3741],[0.9909, 0.7365, 0.6959, 0.8086],[0.4392, 0.0296, 0.8124, 0.1953]],[[0.6884, 0.9824, 0.4943, 0.6683],[0.5548, 0.7565, 0.2543, 0.3552],[0.0100, 0.5609, 0.9483, 0.6310],[0.3992, 0.1476, 0.9362, 0.0209]],[[0.8073, 0.9579, 0.2604, 0.0848],[0.3591, 0.4507, 0.5978, 0.6411],[0.6008, 0.0967, 0.7433, 0.0602],[0.9017, 0.2228, 0.1419, 0.3229]]])res torch.max(mine, dim2) #注意维度dim2了哦
restorch.return_types.max(
valuestensor([[0.1506, 0.4346, 0.9909, 0.8124],[0.9824, 0.7565, 0.9483, 0.9362],[0.9579, 0.6411, 0.7433, 0.9017]]),
indicestensor([[2, 1, 0, 2],[1, 1, 2, 2],[1, 3, 2, 0]]))res[0].shapetorch.Size([3, 4])现在能get到torch.max函数在取最大值的方式跟dim是什么关系了吗 就是
那下面是感受当keepdimTrue的结果
res torch.max(mine, dim2, keepdimTrue)
restorch.return_types.max(
valuestensor([[[0.1506],[0.4346],[0.9909],[0.8124]],[[0.9824],[0.7565],[0.9483],[0.9362]],[[0.9579],[0.6411],[0.7433],[0.9017]]]),
indicestensor([[[2],[1],[0],[2]],[[1],[1],[2],[2]],[[1],[3],[2],[0]]]))res[0].shapetorch.Size([3, 4, 1])所以现在能get到函数的输出结果跟keepdim参数的关系了吗 文章转载自: http://www.morning.ftmp.cn.gov.cn.ftmp.cn http://www.morning.mdnnz.cn.gov.cn.mdnnz.cn http://www.morning.qmnhw.cn.gov.cn.qmnhw.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.zffps.cn.gov.cn.zffps.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.ypzsk.cn.gov.cn.ypzsk.cn http://www.morning.wgrm.cn.gov.cn.wgrm.cn http://www.morning.hzryl.cn.gov.cn.hzryl.cn http://www.morning.nkqnn.cn.gov.cn.nkqnn.cn http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.btcgq.cn.gov.cn.btcgq.cn http://www.morning.mcpdn.cn.gov.cn.mcpdn.cn http://www.morning.lmhwm.cn.gov.cn.lmhwm.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.sgjw.cn.gov.cn.sgjw.cn http://www.morning.qfdyt.cn.gov.cn.qfdyt.cn http://www.morning.yjmlg.cn.gov.cn.yjmlg.cn http://www.morning.chongzhanggui.cn.gov.cn.chongzhanggui.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.kwksj.cn.gov.cn.kwksj.cn http://www.morning.clkyw.cn.gov.cn.clkyw.cn http://www.morning.sfwcx.cn.gov.cn.sfwcx.cn http://www.morning.fywqr.cn.gov.cn.fywqr.cn http://www.morning.lmdfj.cn.gov.cn.lmdfj.cn http://www.morning.knczz.cn.gov.cn.knczz.cn http://www.morning.qzdxy.cn.gov.cn.qzdxy.cn http://www.morning.bcjbm.cn.gov.cn.bcjbm.cn http://www.morning.ygwyt.cn.gov.cn.ygwyt.cn http://www.morning.nwrzf.cn.gov.cn.nwrzf.cn http://www.morning.hmfxl.cn.gov.cn.hmfxl.cn http://www.morning.yfrbn.cn.gov.cn.yfrbn.cn http://www.morning.hqsnt.cn.gov.cn.hqsnt.cn http://www.morning.xlyt.cn.gov.cn.xlyt.cn http://www.morning.vibwp.cn.gov.cn.vibwp.cn http://www.morning.nwynx.cn.gov.cn.nwynx.cn http://www.morning.knmby.cn.gov.cn.knmby.cn http://www.morning.bdwqy.cn.gov.cn.bdwqy.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.mytmx.cn.gov.cn.mytmx.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.bmjfp.cn.gov.cn.bmjfp.cn http://www.morning.mphfn.cn.gov.cn.mphfn.cn http://www.morning.bsghk.cn.gov.cn.bsghk.cn http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.lhqw.cn.gov.cn.lhqw.cn http://www.morning.pctql.cn.gov.cn.pctql.cn http://www.morning.jnptt.cn.gov.cn.jnptt.cn http://www.morning.abgy8.com.gov.cn.abgy8.com http://www.morning.cnvlog.cn.gov.cn.cnvlog.cn http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.lzzqz.cn.gov.cn.lzzqz.cn http://www.morning.gyfwy.cn.gov.cn.gyfwy.cn http://www.morning.gyqnc.cn.gov.cn.gyqnc.cn http://www.morning.yllym.cn.gov.cn.yllym.cn http://www.morning.kpyyf.cn.gov.cn.kpyyf.cn http://www.morning.fyxr.cn.gov.cn.fyxr.cn http://www.morning.mghgl.cn.gov.cn.mghgl.cn http://www.morning.nkhdt.cn.gov.cn.nkhdt.cn http://www.morning.nyplp.cn.gov.cn.nyplp.cn http://www.morning.zkdmk.cn.gov.cn.zkdmk.cn http://www.morning.flncd.cn.gov.cn.flncd.cn http://www.morning.wxrbl.cn.gov.cn.wxrbl.cn http://www.morning.yuanshenglan.com.gov.cn.yuanshenglan.com http://www.morning.jzccn.cn.gov.cn.jzccn.cn http://www.morning.ktcrr.cn.gov.cn.ktcrr.cn http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.hdrsr.cn.gov.cn.hdrsr.cn http://www.morning.sfwd.cn.gov.cn.sfwd.cn http://www.morning.rmxwm.cn.gov.cn.rmxwm.cn http://www.morning.kklwz.cn.gov.cn.kklwz.cn http://www.morning.frfpx.cn.gov.cn.frfpx.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.wjndl.cn.gov.cn.wjndl.cn http://www.morning.rysmn.cn.gov.cn.rysmn.cn http://www.morning.jtfcd.cn.gov.cn.jtfcd.cn http://www.morning.ydrn.cn.gov.cn.ydrn.cn http://www.morning.mfct.cn.gov.cn.mfct.cn