如何繁殖卷积和西安在一起

2024-05-20 21:37:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经阅读了之前关于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();

enter image description here

这将导致零填充卷积与theano。我可以去掉零,但我更想知道是怎么回事!你知道吗

如何将tophat函数与ano中的一些数据(一维)进行卷积?你知道吗

谢谢


Tags: returntophatdefnprangepltscipytheano
1条回答
网友
1楼 · 发布于 2024-05-20 21:37:56

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

scipy.signal.convolve中使用mode='same',而在theano.tensor.signal.conv.conv2d中使用mode='full'。你知道吗

scipy.signal.convolve更改为使用mode='full'会产生完全相同的结果 矢量。对于图像,我将.1添加到theano向量中,以使线可见并且不与卷积一个。你知道吗

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的theano documentationconv2d不支持border_mode='same'。你知道吗

相关问题 更多 >