简体   繁体   中英

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.

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).

What I'd like to do is:

  1. Run the dummies of the categorical (and their interactions) on some Dense layers;
  2. Run an LSTM on the numerical yearly features to predict the 11 th year;
  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.

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?

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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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