1
我讀過關於theano conv1d
問題以往的迴應,但我似乎無法使其工作:如何重現scipy.convolve與theano
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的零填充卷積。我可以刪除零,但我寧願知道發生了什麼!
如何在theano中使用一些數據(一維)來對高帽函數進行卷積?
感謝
可能可以在發送數據到卷積之前修復邊界。 –