Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyTorch中是否有一种经过修正的方法来获得此结果?_Python_Python 3.x_Pytorch - Fatal编程技术网

Python PyTorch中是否有一种经过修正的方法来获得此结果?

Python PyTorch中是否有一种经过修正的方法来获得此结果?,python,python-3.x,pytorch,Python,Python 3.x,Pytorch,我想用torch函数得到这个结果。你有什么建议吗 import torch test_tensor=torch.tensor([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]] ) print(test_tensor) ''' I would like to get: t_1 = torch.tensor([[6], #1+2+3

我想用torch函数得到这个结果。你有什么建议吗

import torch
test_tensor=torch.tensor([[1,  2,  3, 4,  5,  6],
                         [7,  8,  9, 10,  11,  12]]
                        )
print(test_tensor)
'''
I would like to get:
t_1 = torch.tensor([[6], #1+2+3
                  [24]])#7+8+9
t_2 = torch.tensor([[9], #1+3+5
                  [27]])#7+9+11
'''
使用

使用


谢谢你@Gulzar有没有类似的方法来获得t_3=torch.tensor[[14],1+2+5+6[38]]7+8+11+12?-模式是:将两个元素相加,并将两个元素跳到tensor rowsee的末尾,也许会得到答案。在此之前,请尝试使用掩码:mask=torch.ones_liket,dtype=torch.bool,mask[:,2:4]=0,t3_temp=torch.tensort,t3_temp[mask]=t[mask],t3=t3_temp.sumdim=1@mark谢谢你@Gulzar有没有类似的方法来获得t_3=torch.tensor[[14],1+2+5+6[38]]7+8+11+12?-模式是:将两个元素求和,并将两个元素跳到张量rowsee的末尾,也许会得到答案。在此之前,请尝试使用掩码:mask=torch.ones_liket,dtype=torch.bool,mask[:,2:4]=0,t3_temp=torch.tensort,t3_temp[mask]=t[mask],t3=t3_temp.sumdim=1@mark
import torch
test_tensor=torch.tensor([[1,  2,  3, 4,  5,  6],
                         [7,  8,  9, 10,  11,  12]]
                        )

t1 = test_tensor[:, :3].sum(dim=1)
print(t1)
t2 = test_tensor[:, ::2].sum(dim=1)
print(t2)