我已经阅读了之前关于theanoconv1d
问题的回复,但我似乎无法让它工作:
x = np.arange(50) * 1.
y = np.random.normal((x+0.1)/5, 1, 50)
def tophat(x, centre, width, amplitude):
return tt.switch((x < centre + (width/2)) & (x >= centre - (width/2)), np.float64(amplitude) / width, np.float64(0.))
import theano.tensor.signal.conv
def theano_convolve(x, y, filt_range, centre, width, amplitude):
a = tt.matrix('a', dtype='float64')
b = tt.matrix('b', dtype='float64')
filt = tophat(b, centre, width, amplitude)
func = tt.signal.conv.conv2d(a, filt, (1, y.shape[0]), (1, filt_range.shape[0]), border_mode='full') / filt.sum()
return theano.function([a, b], func)(y[None, :], filt_range[None, :])
from scipy.signal import convolve
def scipy_convolve(x, y, filt_range, centre, width, amplitude):
a = tt.vector('a')
filt = theano.function([a], tophat(a, centre, width, amplitude))(filt_range)
return convolve(y, filt, mode='same') / sum(filt)
convolved_theano = theano_convolve(x, y, np.linspace(-10, 10, len(x)), 0, 3, 1)
convolved_scipy = scipy_convolve(x, y, np.linspace(-10, 10, len(x)), 0, 3, 1)
plt.plot(x, y, '.', label='data')
plt.plot(r[0], label='theano')
plt.plot(convolved_scipy, label='scipy');
plt.legend();
这将导致零填充卷积与theano。我可以去掉零,但我更想知道是怎么回事!你知道吗
如何将tophat函数与ano中的一些数据(一维)进行卷积?你知道吗
谢谢
您看到的行为是由用于两个卷积的不同
mode
引起的。你知道吗在
scipy.signal.convolve
中使用mode='same'
,而在theano.tensor.signal.conv.conv2d
中使用mode='full'
。你知道吗将
scipy.signal.convolve
更改为使用mode='full'
会产生完全相同的结果 矢量。对于图像,我将.1
添加到theano向量中,以使线可见并且不与卷积一个。你知道吗不幸的是,查看theano的theano documentationconv2d不支持
border_mode='same'
。你知道吗相关问题 更多 >
编程相关推荐