edouardfouche / neural-based-outlier-discovery

Experiments about the use of neural networks to discover outliers in high-dimensional data
10 stars 8 forks source link

Unsupervised LSTM Based Outlier Detection - How do I get the middle most hidden layer for feature extraction? #2

Open mohammadriazi opened 6 years ago

mohammadriazi commented 6 years ago

I am trying to implement a denoising LSTM based outlier detection method. my dataset consists of 730 rows of samples, each sample contains 128 values. This is my code so far:

Building the model here: image

I want to return the model and the middle most hidden layer: image

I dont receive any errors when I train the model, however, I would like to know

  1. Are my input dimensions correct?
  2. I use the model's predict function on a test data, it returns a very different output compared to the training data.
  3. How do I extract the middle most hidden layer (in this case the layer with 32 nodes) to use for features?

Thanks

edouardfouche commented 6 years ago

Hey. I am sorry for my late answer. Can you provide me the data set so that I just run the same code as yours?

mohammadriazi commented 6 years ago

Hey Edourd! Thanks for getting back to me. Unfortunately I can't disclose the data I'm working with. Imagine a mXn dataset (m= # of samples, n = 128 integer values). I was thinking maybe I could use the hidden layers as sort of lower dimension features instead of 128 raw values.

I found the same issue opened on a different git repo https://github.com/fchollet/keras/issues/41

Here is the code they suggested:

# this is your initial model model = Sequential() model.add(Dense(20, 64, init='uniform')) model.add(Activation('tanh')) model.add(Dense(64, 1, init='uniform')) model.add(Activation('softmax'))

# we train it model.compile(loss='mse', optimizer='sgd') model.fit(X_train, y_train, nb_epoch=20, batch_size=16)

# we build a new model with the activations of the old model # this model is truncated after the first layer model2 = Sequential() model2.add(Dense(20, 64, weights=model.layers[0].get_weights())) model2.add(Activation('tanh'))

activations = model2._predict(X_batch)

I haven't tested this myself, but when I do I'll write back. Thanks