我有一个网络(https://github.com/TheAbhiKumar/tensorflow-value-iteration-networks),我试图在pytorch中实现(我对pytorch很新,但是,根本不是机器学习的新手) .
简而言之,我似乎无法弄清楚如何在pytorch中实现"纯"卷积.在张量流中,它可以像这样完成:
def conv2d_flipkernel(x, k, name=None): return tf.nn.conv2d(x, flipkernel(k), name=name, strides=(1, 1, 1, 1), padding='SAME')
使用flipkernel函数:
def flipkernel(kern): return kern[(slice(None, None, -1),) * 2 + (slice(None), slice(None))]
如何在pytorch中完成类似的事情?
TLDR使用功能工具箱中的卷积torch.nn.fuctional.conv2d
,而不是torch.nn.conv2d
,并围绕垂直和水平轴翻转滤镜.
torch.nn.conv2d
是网络的卷积层.因为学习了权重,所以如果使用互相关来实现权重并不重要,因为网络将只是学习内核的镜像版本(感谢@etarion进行此澄清).
torch.nn.fuctional.conv2d
使用作为参数提供的输入和权重执行卷积,类似于示例中的tensorflow函数.我写了一个简单的测试,以确定是否像tensorflow函数一样,它实际上是执行互相关,并且有必要翻转滤波器以获得正确的卷积结果.
import torch
import torch.nn.functional as F
import torch.autograd as autograd
import numpy as np
#A vertical edge detection filter.
#Because this filter is not symmetric, for correct convolution the filter must be flipped before element-wise multiplication
filters = autograd.Variable(torch.FloatTensor([[[[-1, 1]]]]))
#A test image of a square
inputs = autograd.Variable(torch.FloatTensor([[[[0,0,0,0,0,0,0], [0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0],
[0,0,0,0,0,0,0]]]]))
print(F.conv2d(inputs, filters))
这输出
Variable containing:
(0 ,0 ,.,.) =
0 0 0 0 0 0
0 1 0 0 -1 0
0 1 0 0 -1 0
0 1 0 0 -1 0
0 0 0 0 0 0
[torch.FloatTensor of size 1x1x5x6]
此输出是互相关的结果.因此,我们需要翻转过滤器
def flip_tensor(t):
flipped = t.numpy().copy()
for i in range(len(filters.size())):
flipped = np.flip(flipped,i) #Reverse given tensor on dimention i
return torch.from_numpy(flipped.copy())
print(F.conv2d(inputs, autograd.Variable(flip_tensor(filters.data))))
新输出是卷积的正确结果.
Variable containing:
(0 ,0 ,.,.) =
0 0 0 0 0 0
0 -1 0 0 1 0
0 -1 0 0 1 0
0 -1 0 0 1 0
0 0 0 0 0 0
[torch.FloatTensor of size 1x1x5x6]