【问题标题】:Feature Convolution with Keras使用 Keras 进行特征卷积
【发布时间】:2023-12-22 13:19:01
【问题描述】:

我正在尝试 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.

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

【问题讨论】:

  • 请发布错误
  • 为什么卷积特征随机初始化权重?
  • 特征列表包含地理坐标序列的空间特征(以上特征列表为示例)。我需要通过卷积来捕获序列中的空间依赖关系。
  • predict() 遇到什么问题
  • ValueError: Error when checking model : the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 32 arrays 并且 model.predict() 不返回卷积矩阵

标签: deep-learning keras sequence convolution


【解决方案1】:

正如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))

我使用这些更改重新创建了您的代码,并且它可以正常工作。

【讨论】:

  • 太好了.. 感谢您的帮助