Repository navigation
Expand file tree
/
Copy pathcode.py
More file actions
50 lines (42 loc) · 1.78 KB
/
Copy pathcode.py
File metadata and controls
50 lines (42 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import numpy as np
from sklearn.neighbors import LocalOutlierFactor
from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
# load training data
train_data = np.load('train_data.npy')
# define autoencoder model
input_img = Input(shape=(28, 28, 1))
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
# train autoencoder model
autoencoder.fit(train_data, train_data, epochs=10, batch_size=128, shuffle=True)
# extract encoded features
encoder = Model(input_img, encoded)
encoded_data = encoder.predict(train_data)
# train novelty detection model using LOF algorithm
novelty_detector = LocalOutlierFactor(n_neighbors=20)
novelty_detector.fit(encoded_data)
# simulate data drift by adding new data
new_data = np.load('new_data.npy')
# predict whether samples belong to training set or not
new_encoded_data = encoder.predict(new_data)
pred = novelty_detector.predict(new_encoded_data)
# use prediction to update model if necessary
if -1 in pred:
print('Data drift detected. Updating model...')
# update model code goes here
else:
print('No data drift detected.')