-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoencoder_api_cdef.py
executable file
·123 lines (98 loc) · 4.58 KB
/
autoencoder_api_cdef.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
119
120
121
122
123
from keras.layers import Input, Dense
from keras.models import Model
from keras.datasets import mnist
from numpy import genfromtxt
import csv
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
########################################################
#
# Data Preparation
#
########################################################
cdef class NNDimReduce(object):
def __init__(self):
self.data = list()
self.cumulative_decoded = list()
self.encoders = list()
self.debug = False
if self.debug:
self.fig = plt.figure(1, figsize=(4, 3))
self.ax = Axes3D(self.fig, rect=[0, 0, .95, 1], elev=48, azim=134)
self.all_encoded = list()
def add_data(self, data_vec):
self.data.append(data_vec)
return 1
def build_dim_reduction(self, dim_max):
print(len(self.data))
self.x_train = np.array(self.data)
self.cumulative_decoded = self.x_train - self.x_train
for i in range(0,dim_max):
encoding_dim = 1
input_img = Input(shape=(dim_max, ))
encoded = Dense(encoding_dim, activation='sigmoid')(input_img)
decoded = Dense(dim_max, activation='linear')(encoded)
autoencoder = Model(input=input_img, output=decoded)
encoder = Model(input=input_img, output=encoded)
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1]
decoder = Model(input=encoded_input, output=decoder_layer(encoded_input))
autoencoder.compile(optimizer='RMSprop', loss='mean_absolute_error')
reconstructed = self.x_train - self.cumulative_decoded
autoencoder.fit(self.x_train, reconstructed,
nb_epoch=100,
verbose=0,
batch_size=2056,
shuffle=True,
validation_split=.2)
encoded_imgs = encoder.predict(self.x_train) #the reduced dimensionality representation: 1 dim
self.cumulative_decoded += decoder.predict(encoded_imgs) #the reconstructed representation: 4 dim
self.compute_mse()
self.encoders.append(encoder)
if self.debug:
self.all_encoded.append(encoded_imgs)
if i == dim_max-1:
print(str(self.all_encoded[0][0]) + "," + str(self.all_encoded[1][0]) + "," + str(self.all_encoded[2][0]))
self.ax.scatter(self.all_encoded[0][0:1000, 0], [0] * 1000, [0] * 1000, c='blue', cmap=plt.cm.spectral)
self.ax.scatter(self.all_encoded[0][0:1000, 0], self.all_encoded[1][0:1000, 0], [0] * 1000, c='green', cmap=plt.cm.spectral)
self.ax.scatter(self.all_encoded[0][0:1000, 0], self.all_encoded[1][0:1000, 0], self.all_encoded[2][0:1000, 0], c='yellow', cmap=plt.cm.spectral)
# self.ax.scatter(self.cumulative_decoded[0:1000, 0], self.cumulative_decoded[0:1000, 1], self.cumulative_decoded[0:1000, 2], c='pink', cmap=plt.cm.spectral)
# self.ax.scatter(self.x_train[0:1000, 0], self.x_train[0:1000, 1], self.x_train[0:1000, 2], c='red', cmap=plt.cm.spectral)
plt.show()
def compute_mse(self):
all_error = 0
tot = 0
for i in range(0,len(self.x_train)):
ele = self.x_train[i]
ele2 = self.cumulative_decoded[i]
tot = ele - ele2
tot = tot * tot
all_error += sum(tot)
print(all_error/len(self.x_train))
def transform_down(self, state, dim):
state = np.array(state)
state = state.reshape((1, -1))
transformed = list()
for i in range(0, dim):
transformed.append(self.encoders[i].predict(state))
#print(transformed)
return transformed
#For testing
# if __name__ == "__main__":
# nn_dim_reduce = NNDimReduce()
# filename = 'mountain_car_3d_good_1.csv'
# programData = list()
# with open(filename, 'rb') as f:
# reader = csv.reader(f)
# for row in reader:
# data_row = list()
# for ele in row:
# data_row.append(float(ele))
# nn_dim_reduce.add_data(data_row)
# programData.append(data_row)
# programData_np = np.array(programData)
# nn_dim_reduce.build_dim_reduction(3)
# print(nn_dim_reduce.transform_down(programData_np[0], 1))
# print(nn_dim_reduce.transform_down(programData[0], 2))
# print(nn_dim_reduce.transform_down(programData[0], 3))