Python 将滑动窗口应用于torch.tensor并调整tensor初始大小
寻找一种更简单的torch.tensor修改方法。也许有一种方法可以直接应用于初始张量 输入:Python 将滑动窗口应用于torch.tensor并调整tensor初始大小,python,pytorch,Python,Pytorch,寻找一种更简单的torch.tensor修改方法。也许有一种方法可以直接应用于初始张量 输入: tensor([[0., 1., 2.], [3., 4., 5.], [6., 7., 8.]]) tensor([[0., 1., 3., 4.], [1., 2., 4., 5.], [3., 4., 6., 7.], [4., 5., 7., 8.]]) import torch t = torch.lins
tensor([[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]])
tensor([[0., 1., 3., 4.],
[1., 2., 4., 5.],
[3., 4., 6., 7.],
[4., 5., 7., 8.]])
import torch
t = torch.linspace(0., 8., steps=9)
t1 = t.reshape(3,3) # starting point
t2 = torch.flatten(t1)
t3 = t2.reshape(1, 1, 1, -1) # unfold works with 4D only
unfold = torch.nn.Unfold(kernel_size=(1, 5), dilation=1)
t4 = unfold(t3)
indices = torch.tensor([0, 1, 3, 4]) # deleting 3d (or middle) row and 3d (middle) column
t5 = torch.index_select(torch.index_select(t4.squeeze(), 0, indices), 1, indices)
t5
输出:
tensor([[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]])
tensor([[0., 1., 3., 4.],
[1., 2., 4., 5.],
[3., 4., 6., 7.],
[4., 5., 7., 8.]])
import torch
t = torch.linspace(0., 8., steps=9)
t1 = t.reshape(3,3) # starting point
t2 = torch.flatten(t1)
t3 = t2.reshape(1, 1, 1, -1) # unfold works with 4D only
unfold = torch.nn.Unfold(kernel_size=(1, 5), dilation=1)
t4 = unfold(t3)
indices = torch.tensor([0, 1, 3, 4]) # deleting 3d (or middle) row and 3d (middle) column
t5 = torch.index_select(torch.index_select(t4.squeeze(), 0, indices), 1, indices)
t5
可能的解决方案:
tensor([[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]])
tensor([[0., 1., 3., 4.],
[1., 2., 4., 5.],
[3., 4., 6., 7.],
[4., 5., 7., 8.]])
import torch
t = torch.linspace(0., 8., steps=9)
t1 = t.reshape(3,3) # starting point
t2 = torch.flatten(t1)
t3 = t2.reshape(1, 1, 1, -1) # unfold works with 4D only
unfold = torch.nn.Unfold(kernel_size=(1, 5), dilation=1)
t4 = unfold(t3)
indices = torch.tensor([0, 1, 3, 4]) # deleting 3d (or middle) row and 3d (middle) column
t5 = torch.index_select(torch.index_select(t4.squeeze(), 0, indices), 1, indices)
t5
您可以使用,但方式更简单:
导入火炬
导入torch.nn。功能为nnf
t1=火炬。阿兰奇(9。)。重塑(3,3)#初始张量
out=nnf.unfold(t1[None,None,…],kernel_size=2,padding=0)#就是这样。完成。
干得好。一个问题-表示法t1[None,None,…]
和t1[None,None,:]
或…
和:
有什么不同吗?请参阅