0

i use this code with keras for feature laerning and now i wantٍ do classification ,i dont know how add softmax layer to my auto encoder,please help me

pady
  • 15
  • 1
  • 5
  • Where do you want the layer? And which model is doing classification? What is your input? What is your output? ---- PS: the decoder side should be using `decoder` as input, not `encoder`. – Daniel Möller Nov 12 '17 at 10:34
  • Only the first layer in `decoder` should take `encoder` as input. All others should use `decoder` as input. You're simply discarding the outputs of intermediate decoder layers. In your model, the only valid decoder layers are the last two (Dense(30) and Dense(41)). All others are discarded because you're inputting `encoder` to the following ones. – Daniel Möller Nov 12 '17 at 14:36

1 Answers1

0

Autoencoders are not meant for classification.

They just condense data in an encoded format so you can use this data later for other things (one of the other things may be classification, but it's really pointless to create an autoencoder for this unless you have more use for the data than just classification)

To create a classificator from your input data, just make a model that ends in the number of classes you want.

For "normal" and "attack" (just two classes), you can end the model in one class and make 0 be normal and 1 be attack.

input_tensor = Input(shape=(input_size,))
output_tensor = Dense(40, activation="relu", activity_regularizer=regularizers.l1(10e-5))(input_tensor)
output_tensor= Dense(30, activation="relu")(output_tensor)
output_tensor = Dense(20, activation="relu")(output_tensor)
output_tensor= Dense(10, activation="relu")(output_tensor)
output_tensor= Dense(5, activation="relu")(output_tensor)
output_tensor= Dense(3, activation="relu")(output_tensor)
output_tensor= Dense(1,activation='sigmoid')(output_tensor)

model = Model(input_tensor,output_tensor)

I used 1 output (Dense(1)) that I want to be 0 (normal) or 1 (attack). The 'sigmoid' activation is important to keep the results inside this range or 0 and 1.

Now you just need to make sure that your y_true data is an array shaped as (records,) or (records,1), depending on the model.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214