如何使用theano重现scipy.convolve

时间:2017-08-11 10:56:07

标签: python scipy theano convolution

我已经阅读了之前有关theano conv1d问题的回复,但我似乎无法使其发挥作用:

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();

enter image description here

这导致与theano的零填充卷积。我可以删除零,但我宁愿知道发生了什么!

如何在theano中使用一些数据(1维)卷积tophat函数?

由于

1 个答案:

答案 0 :(得分:1)

您看到的行为是由您用于两个卷积的不同mode引起的。

使用scipy.signal.convolve mode='same' theano.tensor.signal.conv.conv2d mode='full'使用scipy.signal.convolve {/ 1}}。{/ 1>

mode='full'更改为使用.1会产生完全相同的结果  向量。对于图像,我将import numpy as np import theano.tensor as tt import seaborn as sns plt = sns.plt 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='full') / 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(convolved_theano[0]+0.1, label='theano') plt.plot(convolved_scipy, label='scipy') plt.legend() plt.show(block=True) 添加到theano矢量中以使线条可见并且不与sicpy.convolve重叠。

border_mode='same'

scipy vs theano convolution

不幸的是,查看theano的theano documentation conv2d不支持C:\ProgramData\MyApp\MyDb.sdf