在 Keras 中合并特定的输入坐标
Merge specific input coordinates in Keras
我有一个很大的序列模型输入向量(1000 个特征)。该模型主要是一个密集的网络。
我知道功能 1-50 在坐标方面与功能 51-100 高度相关(1 与 51、2 与 52 等),我想利用这一点。
有没有办法在我现有的模型中添加一个图层来反映这一点? (将输入 1 和 51 连接到一个神经元,2 和 52 等)
或者也许唯一的选择是将输入结构更改为 50 个张量(1x2)和一个包含 900 个特征的大向量? (我想避免这种情况,因为这意味着重新编写我的功能准备代码)
我认为第一个密集层会发现这种关系,当然,如果你正确地定义和训练模型。但是,如果您想单独处理前 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 的过滤器(即它与在每两个相关特征上应用单独的密集层具有相同的行为):
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, ...)
我有一个很大的序列模型输入向量(1000 个特征)。该模型主要是一个密集的网络。 我知道功能 1-50 在坐标方面与功能 51-100 高度相关(1 与 51、2 与 52 等),我想利用这一点。
有没有办法在我现有的模型中添加一个图层来反映这一点? (将输入 1 和 51 连接到一个神经元,2 和 52 等)
或者也许唯一的选择是将输入结构更改为 50 个张量(1x2)和一个包含 900 个特征的大向量? (我想避免这种情况,因为这意味着重新编写我的功能准备代码)
我认为第一个密集层会发现这种关系,当然,如果你正确地定义和训练模型。但是,如果您想单独处理前 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 的过滤器(即它与在每两个相关特征上应用单独的密集层具有相同的行为):
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, ...)