1
1
import os
2
2
from keras .optimizers import *
3
3
from keras .models import load_model , Sequential
4
+ # from keras.layers import Input, Dense, LSTM, Dropout
4
5
from keras .layers import *
5
6
import matplotlib .pyplot as plt
6
7
@@ -10,6 +11,7 @@ def __init__(self):
10
11
self .model = None
11
12
self .optimizer = None
12
13
self .activation = None
14
+ self .history = None
13
15
14
16
def compile (self , optimizer , loss = 'mean_squared_error' ):
15
17
pass
@@ -20,8 +22,8 @@ def save_model(self, model_name, path):
20
22
def load_model (self , model_name , path ):
21
23
pass
22
24
23
- def train (self , x_train , y_train , epochs = 100 , batch_size = 32 ,
24
- validation_split = 0.1 ):
25
+ def train (self , x_train , y_train , epochs , batch_size , reset_weights ,
26
+ class_weight , validation_data , verbose ):
25
27
pass
26
28
27
29
def predict (self , x ):
@@ -45,10 +47,8 @@ class XRayBinaryClassifier(Classifier):
45
47
"""
46
48
build pre-worked model
47
49
"""
48
- def __init__ (self , activation = 'relu' ,
49
- optimizer = Adam (lr = 0.0001 , decay = 1e-6 )):
50
+ def __init__ (self , activation = 'relu' ):
50
51
self .activation = activation
51
- self .optimizer = optimizer
52
52
53
53
model = Sequential ()
54
54
model .add (Dense (32 , input_shape = (3 ,), activation = self .activation ))
@@ -57,13 +57,27 @@ def __init__(self, activation='relu',
57
57
model .add (Dense (3 , activation = 'softmax' ))
58
58
self .model = model
59
59
60
- def compile (self , loss = 'mean_squared_error' ):
60
+ def compile (self , optimizer = Adam (lr = 0.0001 , decay = 1e-6 ),
61
+ loss = 'mean_squared_error' ):
61
62
"""
62
63
build the model
63
64
"""
65
+ self .optimizer = optimizer
64
66
self .model .compile (self .optimizer ,
65
67
loss = loss , metrics = ['accuracy' ])
66
68
69
+ def train (self , x_train , y_train , epochs = 50 , batch_size = 100 ,
70
+ reset_weights = True , class_weight = None , validation_data = None ,
71
+ verbose = 1 ):
72
+ if reset_weights :
73
+ self .reset ()
74
+
75
+ self .history = self .model .fit (x_train , y_train , batch_size = batch_size ,
76
+ epochs = epochs ,
77
+ validation_data = validation_data ,
78
+ class_weight = class_weight , shuffle = True ,
79
+ verbose = verbose )
80
+
67
81
def save_model (self , model_name , path = 'models/' ):
68
82
"""
69
83
save model
@@ -81,27 +95,16 @@ def load_model(self, model_name, path='models/'):
81
95
else :
82
96
raise FileNotFoundError ("Model does not exists" )
83
97
84
- def train (self , x_train , y_train , epochs = 100 , batch_size = 32 ,
85
- validation_split = 0.1 ):
86
-
87
- if not isinstance (x_train , np .ndarray ) and \
88
- isinstance (y_train , np .ndarray ):
89
- raise ValueError ('Input array should be numpy arrays' )
90
-
91
- self .model .fit (x_train , y_train , epochs = epochs , shuffle = True ,
92
- batch_size = batch_size ,
93
- validation_split = validation_split )
94
-
95
98
def test (self , x_test ):
96
99
return self .model .predict_classes (x_test )
97
100
98
101
99
102
class AtlasVarStarClassifier (Classifier ):
100
103
101
- def __init__ (self , activation = 'relu' , optimizer = Adam (lr = 0.01 , decay = 0.01 ),
102
- input_size = 22 , num_classes = 9 ):
104
+ def __init__ (self , activation = 'relu' , input_size = 22 , num_classes = 9 ):
103
105
self .activation = activation
104
- self .optimizer = optimizer
106
+ self .history = None
107
+
105
108
model = Sequential ()
106
109
model .add (Dense (64 , input_shape = (input_size ,),
107
110
activation = self .activation ))
@@ -111,13 +114,27 @@ def __init__(self, activation='relu', optimizer=Adam(lr=0.01, decay=0.01),
111
114
model .add (Dense (num_classes , activation = 'softmax' ))
112
115
self .model = model
113
116
114
- def compile (self , loss = 'mean_squared_error' ):
117
+ def compile (self , optimizer = Adam (lr = 0.01 , decay = 0.01 ),
118
+ loss = 'mean_squared_error' ):
115
119
"""
116
120
build the model
117
121
"""
122
+ self .optimizer = optimizer
118
123
self .model .compile (self .optimizer ,
119
124
loss = loss , metrics = ['accuracy' ])
120
125
126
+ def train (self , x_train , y_train , epochs = 50 , batch_size = 100 ,
127
+ reset_weights = True , class_weight = None , validation_data = None ,
128
+ verbose = 1 ):
129
+ if reset_weights :
130
+ self .reset ()
131
+
132
+ self .history = self .model .fit (x_train , y_train , batch_size = batch_size ,
133
+ epochs = epochs ,
134
+ validation_data = validation_data ,
135
+ class_weight = class_weight , shuffle = True ,
136
+ verbose = verbose )
137
+
121
138
def save_model (self , model_name , path = 'models/' ):
122
139
"""
123
140
save model
@@ -135,19 +152,45 @@ def load_model(self, model_name, path='models/'):
135
152
else :
136
153
raise FileNotFoundError ("Model does not exists" )
137
154
138
- def train (self , x_train , y_train , epochs = 100 , batch_size = 32 ,
139
- validation_split = 0.1 ):
155
+ def test (self , x_test ):
156
+ return self . model . predict_classes ( x_test )
140
157
141
- if not isinstance (x_train , np .ndarray ) and \
142
- isinstance (y_train , np .ndarray ):
143
- raise ValueError ('Input array should be numpy arrays' )
144
158
145
- self .model .fit (x_train , y_train , epochs = epochs , shuffle = True ,
146
- batch_size = batch_size ,
147
- validation_split = validation_split )
159
+ class OGLEClassifier (Classifier ):
148
160
149
- def test (self , x_test ):
150
- return self .model .predict_classes (x_test )
161
+ def __init__ (self , activation = 'relu' , input_size = 50 , num_classes = 5 ):
162
+ self .activation = activation
163
+ self .history = None
164
+
165
+ model = Sequential ()
166
+ model .add (LSTM (units = 64 , input_shape = (input_size , 1 )))
167
+ model .add (Dense (64 , activation = self .activation ))
168
+ model .add (Dropout (0.2 ))
169
+ model .add (Dense (16 , activation = self .activation ))
170
+ model .add (Dense (num_classes , activation = 'softmax' ))
171
+ self .model = model
172
+
173
+ def compile (self , optimizer = 'adam' , loss = 'categorical_crossentropy' ):
174
+ """
175
+ build the model
176
+ """
177
+ self .optimizer = optimizer
178
+ self .model .compile (self .optimizer , loss = loss , metrics = ['accuracy' ])
179
+
180
+ def train (self , x_train , y_train , epochs = 50 , batch_size = 100 ,
181
+ reset_weights = True , class_weight = None , validation_data = None ,
182
+ verbose = 1 ):
183
+ if reset_weights :
184
+ self .reset ()
185
+
186
+ self .history = self .model .fit (x_train , y_train , batch_size = batch_size ,
187
+ epochs = epochs ,
188
+ validation_data = validation_data ,
189
+ class_weight = class_weight , shuffle = True ,
190
+ verbose = verbose )
191
+
192
+ def predict (self , x ):
193
+ return self .model .predict_classes (x )
151
194
152
195
153
196
class HTRU1Classifier (Classifier ):
0 commit comments