Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
haifeng-jin committed May 9, 2018
1 parent 40655d7 commit 8f382c1
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion autokeras/bayesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def skip_connection_distance(a, b):
return 1.0
len_a = abs(a[1] - a[0])
len_b = abs(b[1] - b[0])
return abs(a[0] - b[0]) + abs(len_a - len_b)
return (abs(a[0] - b[0]) + abs(len_a - len_b)) / (max(a[0], b[0]) + max(len_a, len_b))


def skip_connections_distance(list_a, list_b):
Expand Down
6 changes: 4 additions & 2 deletions autokeras/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def fit(self, x_train=None, y_train=None, csv_file_path=None, images_path=None,
if not self.searcher:
input_shape = x_train.shape[1:]
n_classes = self.y_encoder.n_classes
searcher = self._get_searcher_class()(n_classes, input_shape, self.path, self.verbose)
searcher = self._get_searcher_class()(n_classes, input_shape, self.path, self.verbose, self.augment)
self.save_searcher(searcher)
self.searcher = True

Expand Down Expand Up @@ -273,5 +273,7 @@ class ImageClassifier(ClassifierBase):
for the best configuration for the dataset.
"""

def __init__(self, verbose=True, searcher_type='bayesian', path=constant.DEFAULT_SAVE_PATH, resume=False):
def __init__(self, verbose=True, searcher_type='bayesian', path=constant.DEFAULT_SAVE_PATH, resume=False,
augment=True):
super().__init__(verbose, searcher_type, path, resume=resume)
self.augment = augment
2 changes: 1 addition & 1 deletion autokeras/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
MAX_NO_IMPROVEMENT_NUM = 100
MAX_BATCH_SIZE = 32
LIMIT_MEMORY = False
SEARCH_MAX_ITER = 10
SEARCH_MAX_ITER = 30
14 changes: 8 additions & 6 deletions autokeras/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Searcher:
model_count: the id of model
"""

def __init__(self, n_classes, input_shape, path, verbose):
def __init__(self, n_classes, input_shape, path, verbose, augment=True):
"""Init Searcher class with n_classes, input_shape, path, verbose
The Searcher will be loaded from file if it has been saved before.
Expand All @@ -53,6 +53,7 @@ def __init__(self, n_classes, input_shape, path, verbose):
self.path = path
self.model_count = 0
self.descriptors = {}
self.augment = augment

def search(self, x_train, y_train, x_test, y_test):
"""an search strategy that will be overridden by children classes"""
Expand All @@ -77,7 +78,7 @@ def get_best_model_id(self):
def replace_model(self, model, model_id):
model.save(os.path.join(self.path, str(model_id) + '.h5'))

def add_model(self, model, x_train, y_train, x_test, y_test, max_no_improve=constant.MAX_ITER_NUM):
def add_model(self, model, x_train, y_train, x_test, y_test, max_iter_num=constant.MAX_ITER_NUM):
"""add one model while will be trained to history list
Returns:
Expand All @@ -90,7 +91,8 @@ def add_model(self, model, x_train, y_train, x_test, y_test, max_no_improve=cons
y_train,
x_test,
y_test,
self.verbose).train_model(max_no_improvement_num=max_no_improve)
self.verbose,
augment=self.augment).train_model(max_iter_num=max_iter_num)
loss, accuracy = model.evaluate(x_test, y_test, verbose=self.verbose)
model.save(os.path.join(self.path, str(self.model_count) + '.h5'))
plot_model(model, to_file=os.path.join(self.path, str(self.model_count) + '.png'), show_shapes=True)
Expand Down Expand Up @@ -180,8 +182,8 @@ def search(self, x_train, y_train, x_test, y_test):

class BayesianSearcher(Searcher):

def __init__(self, n_classes, input_shape, path, verbose):
super().__init__(n_classes, input_shape, path, verbose)
def __init__(self, n_classes, input_shape, path, verbose, augment=True):
super().__init__(n_classes, input_shape, path, verbose, augment)
self.gpr = IncrementalGaussianProcess()
self.search_tree = SearchTree()
self.init_search_queue = None
Expand All @@ -191,7 +193,7 @@ def __init__(self, n_classes, input_shape, path, verbose):
def search(self, x_train, y_train, x_test, y_test):
if not self.history:
model = DefaultClassifierGenerator(self.n_classes, self.input_shape).generate()
history_item = self.add_model(model, x_train, y_train, x_test, y_test)
history_item = self.add_model(model, x_train, y_train, x_test, y_test, constant.SEARCH_MAX_ITER)
self.search_tree.add_child(-1, history_item['model_id'])

graph = Graph(model)
Expand Down
7 changes: 3 additions & 4 deletions experiments/cifar10.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ def select_gpu():


if __name__ == '__main__':
# select_gpu()
os.environ["CUDA_VISIBLE_DEVICES"] = str(3)
constant.LIMIT_MEMORY = True
select_gpu()
# constant.LIMIT_MEMORY = True
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
X = np.concatenate((x_train, x_test))
Y = np.concatenate((y_train, y_test))
clf = ImageClassifier(searcher_type='bayesian', path='~/temp_models', verbose=True)
clf = ImageClassifier(searcher_type='bayesian', path='/home/haifeng/cifar10', verbose=False)

clf.fit(x_train, y_train, time_limit=12*60*60)
# clf.final_fit(x_train, y_train)
Expand Down
8 changes: 4 additions & 4 deletions experiments/fashion.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ def select_gpu():


if __name__ == '__main__':
# select_gpu()
os.environ["CUDA_VISIBLE_DEVICES"] = str(3)
constant.LIMIT_MEMORY = True
select_gpu()
# os.environ["CUDA_VISIBLE_DEVICES"] = str(3)
# constant.LIMIT_MEMORY = True
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
x_train = x_train.reshape(x_train.shape + (1,))
x_test = x_test.reshape(x_test.shape + (1,))
X = np.concatenate((x_train, x_test))
Y = np.concatenate((y_train, y_test))
clf = ImageClassifier(searcher_type='bayesian', path='~/temp_models', verbose=True)
clf = ImageClassifier(searcher_type='bayesian', path='/haifeng/home/fashion', verbose=False)

clf.fit(x_train, y_train, time_limit=12*60*60)
# clf.final_fit(x_train, y_train)
Expand Down
8 changes: 4 additions & 4 deletions experiments/mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ def select_gpu():


if __name__ == '__main__':
# select_gpu()
os.environ["CUDA_VISIBLE_DEVICES"] = str(3)
constant.LIMIT_MEMORY = True
select_gpu()
# os.environ["CUDA_VISIBLE_DEVICES"] = str(3)
# constant.LIMIT_MEMORY = True
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape + (1,))
x_test = x_test.reshape(x_test.shape + (1,))
X = np.concatenate((x_train, x_test))
Y = np.concatenate((y_train, y_test))
clf = ImageClassifier(searcher_type='bayesian', path='~/temp_models', verbose=True)
clf = ImageClassifier(searcher_type='bayesian', path='/home/haifeng/mnist', verbose=False)

clf.fit(x_train, y_train, time_limit=12*60*60)
# clf.final_fit(x_train, y_train)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def test_cross_validate(_, _1):
path = 'tests/resources/temp'
clean_dir(path)
clf = ImageClassifier(path=path, verbose=False)
train_x = np.random.rand(100, 25, 1)
train_x = np.random.rand(100, 25, 25, 1)
train_y = np.random.randint(0, 5, 100)
clf.fit(train_x, train_y)
results = clf.cross_validate(train_x, train_y, 2)
Expand Down

0 comments on commit 8f382c1

Please sign in to comment.