2017-08-11 35 views
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(); 

enter image description here

這導致與theano的零填充卷積。我可以刪除零,但我寧願知道發生了什麼!

如何在theano中使用一些數據(一維)來對高帽函數進行卷積?

感謝

回答

1

您所看到的行爲是通過使用兩個卷積不同mode引起的。

scipy.signal.convolve中,您使用mode='same',而在theano.tensor.signal.conv.conv2d中使用mode='full'

scipy.signal.convolve更改爲使用mode='full'會得到完全相同的 向量。對於圖像,我將.1添加到theano矢量中以使線條可見並且不與sicpy.convolve重疊。

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) 

scipy vs theano convolution

不幸的是看theano documentation conv2d爲theano不支持border_mode='same'

+0

可能可以在發送數據到卷積之前修復邊界。 –