[PYTHON] Get the value of the middle layer of NN

This time I tried about the autoencoder. To briefly explain the autoencoder, it is a mechanism that matches the number of dimensions of the input layer and the output layer and learns the intermediate layer with fewer dimensions. It seems to be used for anomaly detection and feature extraction such as noise removal for actual use.

Autoencoder model

nn.py


model = Sequential()
model.add(Dense(50, activation='relu', input_dim=len(x_train[0])))
model.add(Dense(len(x_train[0]), activation='sigmoid'))
model.compile(optimizer='adadelta',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(x_train, y_train,
            batch_size=100,
            epochs=10,
            verbose=1,
            validation_data=(x_test, y_test))

Create and learn an autoencoder model above I set the middle layer to 50, but it is unknown how good it is

Extraction of middle layer

Suddenly I was wondering how to remove the middle layer. When I looked it up, nothing happened. All you have to do is change the output layer.

By the way, if you want to output normally, do this.

model.predict(data)

You need to edit the model to turn this into an intermediate layer output. First, let's take a look at the outline of the created model.

model.summary()

'''
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 50)               424500    
_________________________________________________________________
dense_2 (Dense)              (None, 848)               424848    
=================================================================
Total params: 849,348
Trainable params: 849,348
Non-trainable params: 0
_________________________________________________________________
​
'''

Confirm that the name of the middle layer is dense_1 Then change the model

middle_model = Model(inputs=model.input,
                     outputs=model.get_layer(layer_name).output)
middle_output = middle_model.predict(data)

middle_model will be one layer with only input layer and output layer This will give the middle layer value to middle_output

The end

Recommended Posts

Get the value of the middle layer of NN
[NNabla] How to get the output (variable) of the middle layer of a pre-built network
Get the number of digits
[C language] [Linux] Get the value of environment variable
[Django 2.2] Sort and get the value of the relation destination
Keras I want to get the output of any layer !!
About the return value of pthread_mutex_init ()
Get the attributes of an object
About the return value of the histogram.
Get the first element of queryset
Get the value from the [Django] Form
Get the number of Youtube subscribers
Get and set the value of the dropdown menu using Python and Selenium
Algorithm Gymnastics 24 Middle of the Linked List
Get the column list & data list of CASTable
Get the minutes of the Diet via API
Get the last day of the specified month
[Python] Get the character code of the file
Get the filename of a directory (glob)
[PowerShell] Get the reading of the character string
Make the default value of the argument immutable
I measured 6 methods to get the index of the maximum value (minimum value) of the list
[NNabla] How to add a quantization layer to the middle layer of a trained model
Get the return value of an external shell script (ls) with python3
Get the key for the second layer migration of JSON data in python
Get the contents of git diff from python
[Python] Get / edit the scale label of the figure
[Python] Get the main topics of Yahoo News
Get the caller of a function in Python
To output a value even in the middle of a cell with Jupyter Notebook
Watch out for the return value of __len__
Try singular value decomposition of the daimyo matrix
Find the divisor of the value entered in python
[Python] Get the last updated date of the website
Get only the address part of NIC (eth0)
Search by the value of the instance in the list
To get the path of the currently running python.exe
[Python] Get the day of the week (English & Japanese)
Get the update date of the Python memo file.
Get the value of a specific key in a list from the dictionary type in the list with Python
[NNabla] How to add a new layer between the middle layers of a pre-built network
Get the title of yahoo news and analyze sentiment
Get the variable name of the variable as a character string.
How to get the number of digits in Python
Around the place where the value of Errbot is stored
[Python] Get the official file path of the shortcut file (.lnk)
Get the sum of each of multiple columns with awk
[Python] Calculate the average value of the pixel value RGB of the object
Get the image of "Suzu Hirose" by Google image search.
Get the absolute path of the script you are running
[python] Get the list of classes defined in the module
Since there are many earthquakes, get the history of earthquakes
Get the return code of the Python script from bat
In the middle of development, we will introduce Alembic
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Make the default value of the argument immutable (article explanation)
Get to know the feelings of gradient boosting trees
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
[Golang] Specify an array in the value of map
Get the size (number of elements) of UnionFind in Python
Get the value selected in Selenium Python VBA pull-down