-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparse_autoencoder.py
124 lines (102 loc) · 2.94 KB
/
sparse_autoencoder.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from importfile import *
# Load data
data = sio.loadmat("data/FM/train_data_psd.mat")
data_train = np.asarray(data['train_data'])
data = sio.loadmat("data/FM/awgn/testdata_psd_awgn_24dB.mat")
data_test = np.asarray(data['test_data'])
# Data preprocessing
scalar = MinMaxScaler()
x_train = scalar.fit_transform(data_train[0:10000, :])
x_valid = scalar.transform(data_train[90000:100000, :])
x_test = scalar.transform(data_test)
# Parameters of network
num_input = 512
num_hidden_1 = 128
num_hidden_2 = 2
learning_rate = 0.1
epochs = 20
batch_size = 256
# Define Layers
input_layer = Input(shape=(num_input,))
hidden_1 = Dense(num_hidden_1, activation='relu',
activity_regularizer=regularizers.l1(0))(input_layer)
hidden_2 = Dense(num_hidden_2, activation='relu',
activity_regularizer=regularizers.l1(0))(hidden_1)
hidden_3 = Dense(num_hidden_1, activation='relu',
activity_regularizer=regularizers.l1(0))(hidden_2)
output_layer = Dense(num_input, activation='relu')(hidden_3)
# Define Optimizer and Loss
optimizer = optimizers.RMSprop(lr=learning_rate)
loss = losses.mean_squared_error
# Model Setup
autoencoder = Model(input_layer, output_layer)
autoencoder.compile(optimizer=optimizer, loss=loss)
autoencoder.fit(x_train, x_train,
epochs=epochs,
batch_size=batch_size,
shuffle=True,
verbose=2,
validation_data=(x_valid, x_valid))
score = []
for i in range(len(x_test)):
# Predict
test_x = np.asarray(x_test[i]).reshape([1, 512])
x_test_recon = autoencoder.predict(test_x)
# Compute MSE
score.append(mean_squared_error(test_x, x_test_recon))
# Compute AUC
y_test = np.concatenate((np.zeros(2000), np.ones(2000)))
fpr, tpr, threshold = roc_curve(y_test, score, pos_label=1)
auc_value = auc(fpr, tpr)
print(auc_value)
'''
plt.figure()
plt.plot(fpr, tpr)
plt.show()
'''
'''
# Save figure to pdf
pp = PdfPages("result/bar.pdf")
# Plot normal data
plot1 = plt.figure()
plt.subplot(2, 1, 1)
plt.plot(canvas_orig[1, :])
plt.title("Normal: original data")
plt.subplot(2, 1, 2)
plt.plot(canvas_recon[1, :])
plt.title("Normal: reconstructed data")
pp.savefig(plot1)
plt.close()
# Plot Abnormal data
plot2 = plt.figure()
plt.subplot(2, 1, 1)
plt.plot(canvas_orig[1001, :])
plt.title("Abnormal: original data")
plt.subplot(2, 1, 2)
plt.plot(canvas_recon[1001, :])
plt.title("Abnormal: reconstructed data")
pp.savefig(plot2)
plt.close()
# Plot loss
plot3 = plt.figure()
plt.plot(n_store, l_store)
plt.xlabel("Epoch")
plt.ylabel("Loss")
pp.savefig(plot3)
plt.close()
pp.close()
# Plot time-frequency figure
plt.figure()
plt.subplot(2, 1, 1)
sns.heatmap(data_train, cmap="YlGnBu")
plt.subplot(2, 1, 2)
sns.heatmap(data_test, cmap="YlGnBu")
plt.figure()
plt.subplot(3, 1, 1)
sns.heatmap(canvas_orig, cmap="YlGnBu")
plt.subplot(3, 1, 2)
sns.heatmap(canvas_recon, cmap="YlGnBu")
plt.subplot(3, 1, 3)
sns.heatmap(canvas_encode, cmap="YlGnBu")
plt.show()
'''