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