简体   繁体   English

tf.keras - 仅将特定层应用于特定特征

[英]tf.keras - Apply specific layers only to specific features

I'll explain better: I want to apply some layers inside my model to some features, use other layers on other features, and finally use the outputs of both of these on some final layers.我会更好地解释:我想将 model 中的一些层应用于某些特征,在其他特征上使用其他层,最后在一些最终层上使用这两个层的输出。

In particular: I have a dataset with some categorical features that I have One Hot Encoded, and then 50 other numerical columns representing 10 years (each with the same 5 features).特别是:我有一个包含一些分类特征的数据集,我有一个热编码,然后是代表 10 年的 50 个其他数字列(每个具有相同的 5 个特征)。

What I'd like to do is:我想做的是:

  1. Run the dummies of the categorical (and their interactions) on some Dense layers;在一些 Dense 层上运行分类(及其交互)的假人;
  2. Run an LSTM on the numerical yearly features to predict the 11 th year;在数字年度特征上运行LSTM以预测第 11 年;
  3. Finally mix the outputs of 1. and 2. into some final Dense layers and then pass them to the final Dense(1), which will be the output of my regression.最后将 1. 和 2. 的输出混合到一些最终的 Dense 层中,然后将它们传递给最终的 Dense(1),这将是我回归的 output。

My question is: *How can I make some specific features to go into some specific layers and how can I direct the output of these layers (along with the others analyzing other features) to some final layers that should gather all the info drawn from the previous ones?我的问题是:*如何将 go 的某些特定功能放入某些特定层,以及如何将这些层的 output(以及其他分析其他功能)引导到一些最终层,这些层应该收集从以前的?

I hope I was clear enough.我希望我足够清楚。 Many thanks to those who'll answer.非常感谢那些会回答的人。

Edit:编辑:


This is what I did so far, please tell me if you could come up with suggestions on how to improve it.这是我到目前为止所做的,请告诉我您是否可以提出如何改进它的建议。

Ps.附言。 Input data are 100% of correct shape.输入数据是 100% 的正确形状。

cat_in = k.layers.Input(shape=(X_train_p3.shape[1],), name='cat_in')
num_in = k.layers.Input(shape=(X_train_m.shape[1], X_train_m.shape[2]), name='num_in')
ovr_in = k.layers.Input(shape=(X_train_f.shape[1],), name='ovr_in')

a = k.layers.Dense(128, activation='relu', kernel_regularizer=l1(.1),
                   bias_regularizer=l1(.1), kernel_initializer='he_uniform')(cat_in)
a = k.layers.Dropout(.2)(a)
a = k.layers.Dense(64, activation='relu', kernel_regularizer=l1(.1),
                   bias_regularizer=l1(.1), kernel_initializer='he_uniform')(a)
a = k.layers.Dropout(.2)(a)
a = k.layers.Dense(64, activation='relu', kernel_regularizer=l1(.1),
                   bias_regularizer=l1(.1), kernel_initializer='he_uniform')(a)


b = k.layers.LSTM(128, activation='tanh', return_sequences=True, kernel_regularizer=l1(.1),
                   bias_regularizer=l1(.1), kernel_initializer='glorot_uniform')(num_in) #original --> needs changes?
b = k.layers.Dropout(.15)(b)
b = k.layers.LSTM(64, activation='tanh', return_sequences=True, kernel_regularizer=l1(.1),
                   bias_regularizer=l1(.1), kernel_initializer='he_uniform')(b)
b = k.layers.Dropout(.15)(b)
b = k.layers.LSTM(64, activation='tanh', kernel_regularizer=l1(.1),
                   bias_regularizer=l1(.1), kernel_initializer='he_uniform')(b)
b = k.layers.Dropout(.15)(b)
b = k.layers.Dense(64, activation='relu', kernel_regularizer=l1(.1),
                   bias_regularizer=l1(.1), kernel_initializer='he_uniform')(b)
b = k.layers.Dropout(.15)(b)
b = k.layers.Dense(64, activation='relu', kernel_regularizer=l1(.1),
                   bias_regularizer=l1(.1), kernel_initializer='he_uniform')(b)


c = k.layers.Dense(128, activation='relu', kernel_regularizer=l1(.1),
                   bias_regularizer=l1(.1), kernel_initializer='he_uniform')(ovr_in)
c = k.layers.Dropout(.2)(c)
c = k.layers.Dense(64, activation='relu', kernel_regularizer=l1(.1),
                   bias_regularizer=l1(.1), kernel_initializer='he_uniform')(c)
c = k.layers.Dropout(.2)(c)
c = k.layers.Dense(64, activation='relu', kernel_regularizer=l1(.1),
                   bias_regularizer=l1(.1), kernel_initializer='he_uniform')(c)


d = k.layers.concatenate([a, b, c])
d = k.layers.Dense(192, activation='relu', kernel_regularizer=l1(.1),
                   bias_regularizer=l1(.1), kernel_initializer='he_uniform')(d)
d = k.layers.Dropout(.2)(d)
d = k.layers.Dense(64, activation='relu', kernel_regularizer=l1(.1),
                   bias_regularizer=l1(.1), kernel_initializer='he_uniform')(d)
d = k.layers.Dropout(.2)(d)
d = k.layers.Dense(64, activation='relu', kernel_regularizer=l1(.1),
                   bias_regularizer=l1(.1), kernel_initializer='he_uniform')(d)
out = k.layers.Dense(1)(d)


model = k.models.Model(inputs=[cat_in, num_in, ovr_in], outputs=out)

model.compile(optimizer=k.optimizers.Adam(learning_rate=.001, beta_1=.999, beta_2=.9999),
              loss='mse',
              metrics=['mae'])

early_stop = k.callbacks.EarlyStopping(monitor='val_mae', patience=100, restore_best_weights=True)
lr_callback = k.callbacks.ReduceLROnPlateau(monitor='val_mae', factor=.1**.5, patience=20, min_lr=1e-9)

Pps: Increasing the number of neurons do not lead to improvements. Pps:增加神经元的数量并不会带来改善。

I want to provide you a dummy example我想为您提供一个虚拟示例

n_sample = 100
cat_feat = 30
dense_feat = 50
lstm_timestep = 20


X_cat = np.random.randint(0,2, (n_sample,cat_feat)) 
X_dense = np.random.uniform(0,1, (n_sample, lstm_timestep, dense_feat))
y = np.random.uniform(0,1, n_sample)
print(X_cat.shape, X_dense.shape)

inp_cat = Input((cat_feat))
x_cat = Dense(64, activation='relu')(inp_cat)


inp_dense = Input((lstm_timestep, dense_feat))
x_dense = LSTM(32, activation='relu')(inp_dense)

concat = Concatenate()([x_cat, x_dense])
x = Dense(32, activation='relu')(concat)
out = Dense(1)(x)

model = Model([inp_cat,inp_dense], out)
model.compile('adam', 'mse')
model.fit([X_cat,X_dense],y, epochs=10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM