gpt4 book ai didi

neural-network - 在 Keras (Theano) 的卷积层中使用均匀的内核大小进行填充

转载 作者:行者123 更新时间:2023-12-03 09:09:31 27 4
gpt4 key购买 nike

我现在需要了解如何使用 Keras 以 Theano 作为后端将数据填充到 1d 卷积层中。我使用“相同”的填充。

假设我们的 output_length 为 8,kernel_size 为 4。根据 the original Keras code我们的 padding 为 8//4 == 2。但是,当在水平数据的左侧和右侧添加两个零时,我可以计算 9 个卷积,而不是 8 个。

有人可以解释一下数据是如何填充的吗?在哪里添加零以及如何计算数据右侧和左侧的填充值数量?

最佳答案

如何测试 keras 填充序列的方式:

您可以做的一个非常简单的测试是创建一个具有单个卷积层的模型,强制其权重为 1,偏差为 0,并为其提供带有 1 的输入以查看输出:

from keras.layers import *
from keras.models import Model
import numpy as np


#creating the model
inp = Input((8,1))
out = Conv1D(filters=1,kernel_size=4,padding='same')(inp)
model = Model(inp,out)


#adjusting the weights
ws = model.layers[1].get_weights()

ws[0] = np.ones(ws[0].shape) #weights
ws[1] = np.zeros(ws[1].shape) #biases

model.layers[1].set_weights(ws)

#predicting the result for a sequence with 8 elements
testData=np.ones((1,8,1))
print(model.predict(testData))

这段代码的输出是:

[[[ 2.] #a result 2 shows only 2 of the 4 kernel frames were activated
[ 3.] #a result 3 shows only 3 of the 4 kernel frames were activated
[ 4.] #a result 4 shows the full kernel was used
[ 4.]
[ 4.]
[ 4.]
[ 4.]
[ 3.]]]

所以我们可以得出结论:

  • Keras 在执行卷积之前添加填充,而不是之后。所以结果不是“零”。
  • Keras 平均分配填充,当有奇数时,它先分配。

因此,在应用卷积之前,它使输入数据看起来像这样

[0,0,1,1,1,1,1,1,1,1,0]

关于neural-network - 在 Keras (Theano) 的卷积层中使用均匀的内核大小进行填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43994604/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com