Autoencoder

Anh-Thi Dinh
draft
⚠️
This is a quick & dirty draft, for me only!

Basic idea of AE

Idea: using AE in Anomaly Detection

Using Keras

1import tensorflow as tf
2tf.keras.backend.set_floatx('float64')
3
4from tensorflow.keras.layers import Dense, Input
5from tensorflow.keras.models import Model
6
7from sklearn.model_selection import train_test_split
1encoding_dim = 30
2input_dim = arr_signal_nor.shape[1] # 200 features
3
4# this is our input placeholder
5input_arr = Input(shape=(input_dim,))
6
7encoded = Dense(encoding_dim, activation='relu')(input_arr)
8decoded = Dense(input_dim, activation='sigmoid')(encoded)
9
10# this model maps an input to its reconstruction
11autoencoder = Model(inputs=input_arr, outputs=decoded)
12# autoencoder.summary()
13
14autoencoder.compile(optimizer='adam', loss='mse')
15
16nb_epoch = 100
17batch_size = 50
18size_test = 0.1
19arr_train, arr_test = train_test_split(arr_signal_nor, test_size=size_test)
20
21autoencoder.fit(arr_train, arr_train,
22                epochs=nb_epoch,
23                batch_size=batch_size,
24                shuffle=True,
25                validation_data=(arr_test, arr_test),
26                verbose=0
27                )

Using PyTorch

1import torch
2import torch.nn as nn
3import torch.nn.functional as F
4import torch.optim as optim
1class Autoencoder(nn.Module):
2
3    def __init__(self, input_size=100, encoded_size=10):
4        super(Autoencoder, self).__init__()
5
6        s0 = input_size
7        s1 = int((input_size + encoded_size)/2)
8        s2 = encoded_size
9
10        self.e1 = nn.Linear(s0, s1)
11        self.e2 = nn.Linear(s1, s2)
12
13        self.d2 = nn.Linear(s2, s1)
14        self.d1 = nn.Linear(s1, s0)
15
16    def encode(self, x):
17        x = F.relu(self.e1(x))
18        x = F.relu(self.e2(x))
19        return x
20
21    def decode(self, x):
22        x = F.relu(self.d2(x))
23        x = torch.sigmoid(self.d1(x))
24        return x
25
26    def forward(self, x):
27        x = self.encode(x)
28        x = self.decode(x)
29        return x
1# USING SEQUENTIAL
2class Autoencoder(nn.Module):
3
4    def __init__(self):
5        super(Autoencoder, self).__init__()
6
7				s0 = input_size
8        s1 = int((input_size + encoded_size)/2)
9        s2 = encoded_size
10
11        self.encoder = nn.Sequential(
12            nn.Linear(s0, s1),
13            nn.ReLU(True),
14            nn.Linear(s1, s2),
15            nn.ReLU(True)
16        )
17
18        self.decoder = nn.Sequential(
19            nn.Linear(s2, s1),
20            nn.ReLU(True),
21            nn.Linear(s1, s0),
22            torch.sigmoid(True),
23        )
24
25    def forward(self, x):
26        x = self.encoder(x)
27        x = self.decoder(x)
28        return x