-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5layerAE_relu.py
89 lines (63 loc) · 3.16 KB
/
5layerAE_relu.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
from keras.layers import Input, Dense
from keras import optimizers,initializers
from keras.models import Model
input_data = Input(shape=(16,))
print('RELU AE')
encodedh = Dense(8, activation='relu',kernel_initializer=initializers.Constant(value=0.2),
bias_initializer='zero')(input_data)
encoded = Dense(4, activation='relu',kernel_initializer=initializers.Constant(value=0.2),
bias_initializer='zero')(encodedh)
decodedh = Dense(8, activation='relu',kernel_initializer=initializers.Constant(value=0.2),
bias_initializer='zero')(encoded)
decoded = Dense(16, activation='relu',kernel_initializer=initializers.Constant(value=0.2),
bias_initializer='zero')(decodedh)
autoencoder = Model(input_data, decoded)
encoderh = Model(input_data, encodedh)
encoder = Model(input_data, encoded)
decoderh = Model(input_data, decodedh)
# autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
rms = optimizers.RMSprop(lr=0.0001, rho=0.9, epsilon=1e-08, decay=0.0)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
x_train = [[1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],
[0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],
[0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],
[0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],
[0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],
[0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],
[0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],
[0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],
[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0],
[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0],
[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0],
[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0],
[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0],
[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0],
[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0],
[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0]]
autoencoder.fit(x_train, x_train,
nb_epoch=5000000,
verbose=2,
batch_size=256,
shuffle=True,
validation_data=(x_train, x_train))
half_encoded = encoderh.predict(x_train)
print(half_encoded)
encoded_data = encoder.predict(x_train)
print(encoded_data)
half_decoded = decoderh.predict(x_train)
print(half_decoded)
decoded_data = autoencoder.predict(x_train)
print(decoded_data)
with open('5l_i%dhalfencoded.csv' % int(initWeight*100), 'w', newline='') as f:
wr = csv.writer(f)
wr.writerows(half_encoded)
with open('5l_i%d_encoded.csv' % int(initWeight*100), 'w', newline='') as f:
wr = csv.writer(f)
wr.writerows(encoded_data)
with open('5l_i%d_halfdecoded.csv' % int(initWeight*100), 'w', newline='') as f:
wr = csv.writer(f)
wr.writerows(half_decoded)
with open('5l_i%d_decoded.csv' % int(initWeight*100), 'w', newline='') as f:
wr = csv.writer(f)
wr.writerows(decoded_data)
print('initial weight = %f' % initWeight)