Python 和 Julia 卷积之间的区别?
Differences between Python and Julia convolutions?
我正在尝试将一段代码从 Python 转换为 Julia,但我很难理解以下卷积之间的输出差异:
Python:
conc = [10,100,1000,5,2000,200,20]
conc_filter = [1,2,3]
sp.ndimage.convolve1d(conc, conc_filter,axis=0,mode='constant')
输出:[ 120, 1230, 2305, 5010, 4215, 6420, 640]
朱莉娅:
conc = [10,100,1000,5,2000,200,20]
conc_filter = [1,2,3]
conv(conc,conc_filter)
输出:[10, 120, 1230, 2305, 5010, 4215, 6420, 640, 60]
谁能解释为什么输出长度不同?在尝试重新创建 Python 输出时,我试图提出一个逻辑公式,将 input/filter 大小与切片 conv 输出所需的索引范围相关联并获得相同的结果。但是,我没有太多运气。是否有可以产生相同输出的 Julia 函数?
假设我正确理解你的问题:
根据 this stackexchange 上的回答,Julia 示例的向量长度是正确的。
N + M - 1 给出的输出向量长度为 9,如 Julia 答案所示。
您确定 Python 代码 and/or 复制的输出正确吗?
您使用什么 Python conv 例程?这是 Python 的 numpy 卷积:
>>> conc = [10,100,1000,5,2000,200,20]
>>> conc_filter = [1,2,3]
>>> numpy.convolve(conc, conc_filter,axis=0,mode='constant')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: convolve() got an unexpected keyword argument 'axis'
>>> numpy.convolve(conc, conc_filter)
array([ 10, 120, 1230, 2305, 5010, 4215, 6420, 640, 60])
您可能需要查看 Python 包的文档,了解如何使 fft() 类型的例程执行您需要的操作。
对于那些感兴趣的人,我制定了 'constant' 模式中使用的填充方案,作为参数传递给 Python 卷积函数。难以找到 input/output 大小之间的关系是由于对称和非对称卷积滤波器的左右填充方式。
下面的 Julia 代码似乎与所有测试的 input/output 的 Python 等效。
conc = [10,100,1000,5,2000,200,20]
conc_filter = [1,2,3]
n=length(conc)
m=length(conc_filter)
padleft=ceil(Int32,m/2)-1
padright=floor(Int32,m/2)
conc =append!(zeros(padleft),conc)
conc = append!(conc,zeros(padright))
out = zeros(n)
for i in 1:n
for j in 1:m
out[i] += conc[i+j-1]*conc_filter[m-j+1]
end
end
out
结果:
[ 120, 1230, 2305, 5010, 4215, 6420, 640]
我正在尝试将一段代码从 Python 转换为 Julia,但我很难理解以下卷积之间的输出差异:
Python:
conc = [10,100,1000,5,2000,200,20]
conc_filter = [1,2,3]
sp.ndimage.convolve1d(conc, conc_filter,axis=0,mode='constant')
输出:[ 120, 1230, 2305, 5010, 4215, 6420, 640]
朱莉娅:
conc = [10,100,1000,5,2000,200,20]
conc_filter = [1,2,3]
conv(conc,conc_filter)
输出:[10, 120, 1230, 2305, 5010, 4215, 6420, 640, 60]
谁能解释为什么输出长度不同?在尝试重新创建 Python 输出时,我试图提出一个逻辑公式,将 input/filter 大小与切片 conv 输出所需的索引范围相关联并获得相同的结果。但是,我没有太多运气。是否有可以产生相同输出的 Julia 函数?
假设我正确理解你的问题:
根据 this stackexchange 上的回答,Julia 示例的向量长度是正确的。
N + M - 1 给出的输出向量长度为 9,如 Julia 答案所示。
您确定 Python 代码 and/or 复制的输出正确吗?
您使用什么 Python conv 例程?这是 Python 的 numpy 卷积:
>>> conc = [10,100,1000,5,2000,200,20]
>>> conc_filter = [1,2,3]
>>> numpy.convolve(conc, conc_filter,axis=0,mode='constant')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: convolve() got an unexpected keyword argument 'axis'
>>> numpy.convolve(conc, conc_filter)
array([ 10, 120, 1230, 2305, 5010, 4215, 6420, 640, 60])
您可能需要查看 Python 包的文档,了解如何使 fft() 类型的例程执行您需要的操作。
对于那些感兴趣的人,我制定了 'constant' 模式中使用的填充方案,作为参数传递给 Python 卷积函数。难以找到 input/output 大小之间的关系是由于对称和非对称卷积滤波器的左右填充方式。
下面的 Julia 代码似乎与所有测试的 input/output 的 Python 等效。
conc = [10,100,1000,5,2000,200,20]
conc_filter = [1,2,3]
n=length(conc)
m=length(conc_filter)
padleft=ceil(Int32,m/2)-1
padright=floor(Int32,m/2)
conc =append!(zeros(padleft),conc)
conc = append!(conc,zeros(padright))
out = zeros(n)
for i in 1:n
for j in 1:m
out[i] += conc[i+j-1]*conc_filter[m-j+1]
end
end
out
结果: [ 120, 1230, 2305, 5010, 4215, 6420, 640]