使用 Keras 进行特征卷积

Feature Convolution with Keras

我正在尝试使用 Keras,但在执行以下操作时遇到困难。我有一个包含特征数组的列表,我需要将它与 Keras 进行卷积。特征数组列表由以下代码生成。

features= []
for i in range(32):       # Contains 32 feature arrays
    x = np.random.randint(low=-1, high=1, size=16)  #Each feature arraysize = 16 
    features.append(x)

我试过的卷积步骤如下,

conv = Sequential()
conv.add(Convolution1D(filters=32, kernel_size=3, input_shape=(16,1),
                activation='elu', use_bias=True))
conv.add(Dense(units=1))
conv.compile(optimizer='adam', loss='mse')
#conv.predict(features) is the way I tried but failed.

我需要获取输入特征列表的卷积列表。我该怎么做?

正如 Keras Sequential Model documentation 中所述,当您将维度传递给模型时,您应该包括批量大小。直觉上,您假设 Keras 获取您的全部数据并分批提供,但这必须使用 None 关键字明确说明,当您的输入中的实例数不恒定时使用该关键字:

conv.add(Convolution1D(filters=32, kernel_size=3, activation='elu', use_bias=True, input_shape=(None,16))).

接下来,当您尝试在一个批次中提供整个数据集时,您的 数据集维度 应该包括批次大小作为第一个维度,例如:(1,32,16) .这可以通过以下方式实现:x = np.reshape(features, newshape=(1,32,16)).

我使用这些更改重新创建了您的代码并且它有效。