Say I have an MLP that looks like:
model = models.Sequential()
model.add(layers.Dense(200, activation = "relu", input_dim=250))
model.add(layers.Dense(100, activation="relu"))
model.add(layers.Dense(75, activation="relu"))
model.add(layers.Dense(50, activation="relu"))
model.add(layers.Dense(17, activation = "softmax"))
model.compile(optimizer = optimizers.Adam(lr=0.001),
loss = "categorical_crossentropy",
metrics = ['MeanSquaredError', 'AUC' , 'accuracy',tf.keras.metrics.Precision()])
history = model.fit(X_train, y_train, epochs = 100,
validation_data = (X_val, y_val))
Now I want, at the final layer, to add a binary classifier for each of the 17 classes, rather than having the 17 classes output altogether with the softmax; Meaning that the binary classifiers should all ramify from to the last layer. Is this possible to do in Keras? I am guessing it should be a different type of model, instead of Sequential()
?
EDIT:
I understood that I can't use the Sequential, and changed the model so that:
from tensorflow.keras import Input
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense, Dropout
def test_model(layer_in):
dense1 = Dense(200, activation = "relu") (layer_in)
drop1 = Dropout(rate=0.02)(dense1)
dense2 = Dense(100, activation="relu")(drop1)
drop2 = Dropout(rate=0.02)(dense2)
dense3 = Dense(75, activation="relu")(drop2)
drop3 = Dropout(rate=0.02)(dense3)
dense4 = Dense(50, activation="relu")(drop3)
drop4 = Dropout(rate=0.01)(dense4)
out = Dense(17, activation= "softmax")(drop4)
return out
layer_in = Input(shape=(250,))
layer_out = test_model(layer_in)
model = Model(inputs=layer_in, outputs=layer_out)
plot_model(model, show_shapes=True)
So I guess the end goal is to have 17 binary layers at the end with a sigmoid function each, that are all connected to drop4...