我认为第一个密集层会发现这种关系,当然如果你正确定义和训练模型。但是,如果您想分别处理前 100 个特征,另一种方法是使用 Keras functional API 并定义两个输入层,一个用于前 100 个特征,另一个用于其余 900 个特征:
input_100 = Input(shape=(100,))
input_900 = Input(shape=(900,))
现在您可以分别处理每一项。例如,您可以定义两个单独的 Dense 层连接到每个层,然后合并它们的输出:
dense_100 = Dense(50, activation='relu')(input_100)
dense_900 = Dense(200, activation='relu')(input_900)
concat = concatenate([dense_100, dense_900])
# define the rest of your model ...
model = Model(inputs=[input_100, input_900], outputs=[the_outputs_of_model])
当然,您需要单独输入输入层。为此,您可以轻松地对训练数据进行切片:
model.fit([X_train[:,:100], X_train[:,100:]], y_train, ...)
更新:如果您特别希望特征 1 和 51、2 和 52 等具有单独的神经元(至少,我无法评论它的效率没有对数据进行实验),您可以使用LocallyConnected1D 层与内核大小和没有。过滤器为 1(即,它与在每两个相关特征上应用单独的 Dense 层具有相同的行为):
input_50_2 = Input(shape=(50,2))
local_out = LocallyConnected1D(1, 1, activation='relu')(input_50_2)
local_reshaped = Reshape((50,))(local_out) # need this for merging since local_out has shape of (None, 50, 1)
# or use the following:
# local_reshaped = Flatten()(local_out)
concat = concatenation([local_reshaped, dense_900])
# define the rest of your model...
X_train_50_2 = np.transpose(X_train[:,:100].reshape((2, 50)))
model.fit([X_train_50_2, X_train[:,100:]], y_train, ...)