nul*_*ull 2 convolution neural-network theano keras
我现在需要了解如何使用 Keras 以 Theano 作为后端将数据填充到 1d 卷积层中。我使用“相同”的填充。
假设我们的 anoutput_length
为 8,akernel_size
为 4。根据原始 Keras 代码,我们有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))
Run Code Online (Sandbox Code Playgroud)
这段代码的输出是:
[[[ 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.]]]
Run Code Online (Sandbox Code Playgroud)
所以我们可以得出结论:
因此,在应用卷积之前,它使输入数据看起来像这样
[0,0,1,1,1,1,1,1,1,1,0]
Run Code Online (Sandbox Code Playgroud)