Skip to content

Commit

Permalink
genetic working not working lol
Browse files Browse the repository at this point in the history
  • Loading branch information
Paco Duhard-jourdan committed Apr 15, 2021
1 parent 3783876 commit 30c536b
Show file tree
Hide file tree
Showing 13 changed files with 589 additions and 110 deletions.
165 changes: 141 additions & 24 deletions GAModel.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,154 @@
import Protodeep as ptd
from random import randrange

from random import randrange, choice, random
import numpy as np
# import tensorflow as tf

class GAModel():

def __init__(self, constraints, input_shape,
def __init__(self, constraints, input_shape, dataset,
metrics=['categorical_accuracy'],
loss='BinaryCrossentropy', optimizer='Adam'):
loss='BinaryCrossentropy', optimizer='Adam',
model_attr=None):

self.constraints = constraints
self.input_shape = input_shape
self.dataset = dataset
self.metrics = metrics
self.loss = loss
self.optimizer = optimizer
self.model_attr = model_attr
self.create_model()

def create_model(self, summary=True):
i = ptd.layers.Input(self.input_shape)()
out = i
print('esf')
for c in self.constraints:
units = c['unit_range'][0] if len(c['unit_range']) == 1 else randrange(c['unit_range'][0], c['unit_range'][1])
out = ptd.layers.Dense(
units=units,
activation=c['fas'][randrange(0, len(c['fas']))],
kernel_initializer=c['initializers'][randrange(0, len(c['initializers']))],
kernel_regularizer=c['regularizers'][randrange(0, len(c['regularizers']))]
)(out)
self.model = ptd.model.Model(inputs=i, outputs=out)
self.model.compile(self.input_shape, metrics=self.metrics, loss=self.loss,
optimizer=self.optimizer)
if summary:
self.model.summary()

def fit(self):
pass
def create_model(self, summary=False):

self.models = []
for m in range(1):
inpt = ptd.layers.Input(self.input_shape)()
out = inpt
new_model_attr = self.model_attr is None
if new_model_attr:
self.model_attr = []
for i, c in enumerate(self.constraints):
if new_model_attr:
layer_attr = {
'unit': c['unit'][0] if len(c['unit']) == 1 else randrange(c['unit'][0], c['unit'][1]),
'fa': c['fa'][randrange(0, len(c['fa']))],
'initializer': c['initializer'][randrange(0, len(c['initializer']))],
'regularizer': c['regularizer'][randrange(0, len(c['regularizer']))]
}
self.model_attr.append(layer_attr)
else:
layer_attr = self.model_attr[i]
out = ptd.layers.Dense(
units=layer_attr['unit'],
activation=layer_attr['fa'],
kernel_initializer=layer_attr['initializer'],
kernel_regularizer=layer_attr['regularizer']
)(out)
model = ptd.model.Model(inputs=inpt, outputs=out)
model.compile(self.input_shape, metrics=self.metrics, loss=self.loss,
optimizer=self.optimizer)
if summary:
model.summary()
self.models.append(model)

# def create_model(self, summary=False):

# self.models = []
# for m in range(1):
# inpt = tf.keras.Input(self.input_shape)
# out = inpt
# new_model_attr = self.model_attr is None
# if new_model_attr:
# self.model_attr = []
# for i, c in enumerate(self.constraints):
# if new_model_attr:
# layer_attr = {
# 'unit': c['unit'][0] if len(c['unit']) == 1 else randrange(c['unit'][0], c['unit'][1]),
# 'fa': c['fa'][randrange(0, len(c['fa']))],
# 'initializer': c['initializer'][randrange(0, len(c['initializer']))],
# 'regularizer': c['regularizer'][randrange(0, len(c['regularizer']))]
# }
# self.model_attr.append(layer_attr)
# else:
# layer_attr = self.model_attr[i]
# out = tf.keras.layers.Dense(
# units=layer_attr['unit'],
# activation=layer_attr['fa'],
# kernel_initializer=layer_attr['initializer'],
# kernel_regularizer=layer_attr['regularizer']
# )(out)
# model = tf.keras.Model(inputs=inpt, outputs=out)
# model.compile(metrics=self.metrics, loss=self.loss,
# optimizer=self.optimizer)
# if summary:
# model.summary()
# self.models.append(model)

def evaluate(self, x_train, y_train, x_test, y_test):

losses = []
for model in self.models:
self.logs = model.fit(
x_train, y_train, epochs=100, validation_data=(x_test, y_test),
callbacks=[ptd.callbacks.EarlyStopping(restore_best_weights=True)],
# callbacks=[ptd.callbacks.EarlyStopping(baseline=0.08, restore_best_weights=True)],
verbose=False
)
# print(history.history.keys())
# print(self.logs.history['val_loss'])
losses.append(self.logs['val_loss'][-1])

return sum(losses) / len(losses)


# def evaluate(self, x_train, y_train, x_test, y_test):

# losses = []
# for model in self.models:
# self.logs = model.fit(
# x_train, y_train, epochs=100, validation_data=(x_test, y_test),
# callbacks=[tf.keras.callbacks.EarlyStopping(restore_best_weights=True)],
# # callbacks=[ptd.callbacks.EarlyStopping(baseline=0.08, restore_best_weights=True)],
# verbose=False
# )
# # print(history.history.keys())
# # print(self.logs.history['val_loss'])
# losses.append(self.logs.history['val_loss'][-1])

# return sum(losses) / len(losses)


def fit(self, x_train, y_train, x_test, y_test):

loss = self.evaluate(x_train, y_train, x_test, y_test)

self.fitness = 1 / (loss ** 2 * 2)
if np.isnan(loss):
self.fitness = 0

print(self.model_attr)

print(f"fitness: {self.fitness} -- loss: {loss}")
return self.fitness

def mutate_attr(self, l, key):
if key == 'unit':
return randrange(*self.constraints[l][key]) if len(self.constraints[l][key]) > 1 else self.constraints[l][key][0]
else:
return choice(self.constraints[l][key])

def cross(self, b, mutation_rate):
cross_model = []
for l, (ma, mb) in enumerate(zip(self.model_attr, b.model_attr)):

farand = [randrange(0, 2) for i in range(4)]

cross_model.append({key: self.mutate_attr(l, key) if random() < mutation_rate else (ma[key] if farand[i] else mb[key]) for i, key in enumerate(mb)})
# print(farand)
# print(ma, mb, cross_model)

return GAModel(self.constraints, self.input_shape, self.dataset,
model_attr=cross_model)
# baby = GAModel(cross)
# baby.create_model()
96 changes: 68 additions & 28 deletions Genetic.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,111 @@
# import Protodeep as ptd
from GAModel import GAModel
from Preprocessing.Split import Split
from random import random

"""
[{
unit_range = [40, 60]
fa = ['relu'....]
init = ['random'...]
w_reg = [''...]
# b_reg = [''...]
# out_reg = [''...]
# use_bias = boolean
w_reg = [''...]]
}, {}, {}]
"""


class Genetic():

def __init__(self, constraints, dataset, population_size=20,
mutation_rate=0.05, generation=10):
def __init__(self, constraints, dataset, population_size=50,
mutation_rate=0.1, generation=20):
self.constraints = constraints
self.dataset = dataset
self.input_shape = dataset.features.shape[1:]
self.population_size = population_size
self.mutation_rate = mutation_rate
self.generation = generation
((self.x_train, self.y_train), (self.x_test, self.y_test)) = Split.train_test_split(
dataset.features, dataset.targets)
self.best_entity = None

# def new_rand_entity(self, constraints)
def init(self):
self.population = [
GAModel(self.constraints, self.input_shape) for i in range(self.population_size)
GAModel(self.constraints, self.input_shape, self.dataset) for i in range(self.population_size)
]

def evaluate(self):
return
return

def fit(self):
bestscore = self.best_entity.fitness if self.best_entity else -1

self.bestscore = self.best_entity.fitness if self.best_entity else -1
for entity in self.population:
fitness = entity.fit()
if fitness > self.bestscore:
wefjk nwef ergn p
egjr
wefjkgr pjwe
gjew
pgjpw
egjr p
iwgr p
iejwg p
jerg
pjegr
fitness = entity.fit(self.x_train, self.y_train, self.x_test, self.y_test)
if fitness > bestscore:
bestscore = fitness
self.best_entity = entity
# print(self.best_entity.evaluate(self.x_train, self.y_train, self.x_test, self.y_test))

def find_model(self):
self.init()
for g in range(self.generation):
self.fit()
self.cross()
self.mutate()
return self.best_entity.model
# for p in population:
# p.fitness = score(p)
# self.mutate()

# self.best_entity.model.summary()
print(f"Best model :\n{self.best_entity.model_attr}")
print(f"Loss: {self.best_entity.evaluate(self.x_train, self.y_train, self.x_test, self.y_test)}")
return self.best_entity.models[0]

def create_pool(self):
fit_sum = sum([entity.fitness for entity in self.population])
current = 0
pool = []
for entity in self.population:
current += entity.fitness
pool.append({
'entity': entity,
'fs': current,
'fitness': entity.fitness
})
return pool, fit_sum

def select_one_parent(self, pool, fit_sum):

rnd = random() * fit_sum
for p in pool:
if p['fs'] > rnd:
return p['entity']

raise Exception("Unable to find a parent")

def select_parents(self, pool, fit_sum):
a, b = None, None
while a is b:
rnda = random() * fit_sum
rndb = random() * fit_sum
a, b = None, None
for p in pool:
if a is None and p['fs'] > rnda:
a = p['entity']
if b is None and p['fs'] > rndb:
b = p['entity']
return a, b

def cross(self):
pass
pool, fit_sum = self.create_pool()
new_population = []
print(pool)
for i in range(self.population_size):
# a, b = self.select_parents(pool, fit_sum)
# new_population.append(a.cross(b, self.mutation_rate))
parent = self.select_one_parent(pool, fit_sum)
new_population.append(parent.cross(parent, self.mutation_rate))
self.population = new_population

def mutate(self):
pass
# def mutate(self):
# pass


if __name__ == '__main__':
Expand Down
26 changes: 13 additions & 13 deletions main_genetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@
gen = Genetic(
constraints=[
{
'unit_range': [20, 80],
'fas': ['Linear', 'Relu', 'Sigmoid', 'Softmax', 'Tanh'],
'initializers': ['GlorotNormal', 'GlorotUniform', 'HeNormal', 'RandomNormal', 'Zeros'],
'regularizers': ['L1', 'L2', 'L1L2']
'unit': [20, 80],
'fa': ['linear', 'relu', 'sigmoid', 'softmax', 'tanh'],
'initializer': ['GlorotNormal', 'GlorotUniform', 'HeNormal', 'RandomNormal'],
'regularizer': [None]
},
{
'unit_range': [10, 30],
'fas': ['Linear', 'Relu', 'Sigmoid', 'Softmax', 'Tanh'],
'initializers': ['GlorotNormal', 'GlorotUniform', 'HeNormal', 'RandomNormal', 'Zeros'],
'regularizers': ['L1', 'L2', 'L1L2']
'unit': [10, 30],
'fa': ['linear', 'relu', 'sigmoid', 'softmax', 'tanh'],
'initializer': ['GlorotNormal', 'GlorotUniform', 'HeNormal', 'RandomNormal'],
'regularizer': [None]
},
{
'unit_range': [2],
'fas': ['Softmax'],
'initializers': ['GlorotNormal', 'GlorotUniform', 'HeNormal', 'RandomNormal', 'Zeros'],
'regularizers': [None]
'unit': [2],
'fa': ['softmax'],
'initializer': ['GlorotNormal', 'GlorotUniform', 'HeNormal', 'RandomNormal'],
'regularizer': [None]
}
],
dataset=dataset
)

gen.find_model()
model = gen.find_model()
Loading

0 comments on commit 30c536b

Please sign in to comment.