我有一个大的输入向量(1000个特征)的顺序模型。该模型主要是一个密集的网络。我知道特征1-50与特征51-100 (1与51,2与52等)高度相关。我想利用这一点。
有没有办法在我现有的模型中添加一个层来反映这一点?(将输入1和51连接到神经元、2和52等)
或者,唯一的选择是将输入结构更改为50个张量( 1x2)和一个包含900个特征的大矢量?(我想避免这种情况,因为这意味着重写我的特性准备代码)
发布于 2018-09-25 19:14:46
我认为第一层稠密层会发现这种关系,当然,如果你正确地定义和训练模型。但是,如果您想单独处理前100个特性,一种选择是使用Keras函数API并定义两个输入层,一个用于前100个特性,另一个用于其余900个特性:
input_100 = Input(shape=(100,))
input_900 = Input(shape=(900,))
现在,您可以分别处理每一个。例如,您可以定义连接到每个层的两个单独的密集层,然后合并它们的输出:
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, ...)
Update:如果您特别希望功能1和51、2和52等有一个单独的神经元(至少我不能在没有对数据进行实验的情况下评论它的效率),您可以使用内核大小和no的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, ...)
https://stackoverflow.com/questions/52504249
复制相似问题