b = a.view(3,2,-1)print(b)
tensor([[[-1.2323,-0.5237,0.0081,-0.7642],[1.2312,-1.2463,-1.2034,1.4166]],[[1.5238,0.1296,0.7698,0.4130],[0.7246,0.3782,0.9749,-1.3180]],[[0.4092,-0.9307,0.8133,-0.8961],[0.5478,0.9748,-0.2082,1.0809]]])
c = a.view(4,3,-1)print(c)
tensor([[[-1.2323,-0.5237],[0.0081,-0.7642],[1.2312,-1.2463]],[[-1.2034,1.4166],[1.5238,0.1296],[0.7698,0.4130]],[[0.7246,0.3782],[0.9749,-1.3180],[0.4092,-0.9307]],[[0.8133,-0.8961],[0.5478,0.9748],[-0.2082,1.0809]]])
c = a.view(4,3)
Traceback (most recent call last):
File "D:\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3296,in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-68-4f6ba6dedc05>", line 1,in<module>
c = a.view(4,3)
RuntimeError: shape '[4, 3]'is invalid forinput of size 24
c = a.view(5,3,-1)
Traceback (most recent call last):
File "D:\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3296,in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-69-5d309fd757fa>", line 1,in<module>
c = a.view(5,3,-1)
RuntimeError: shape '[5, 3, -1]'is invalid forinput of size 24
mean 当不指定参数时,输出为所有元素的平均值,为一个标量; 当指定dim时,假设dim=i。如果第i个维度为Ni,则从Ni个分量中各取一个元素,求平均值,所得tensor的shape为N1N2…N(i-1)N(i+1)…Nn 若keepdim=True,则所得tensor的shape为N1N2*…N(i-1)1N(i+1)…Nn