def main(): batch_size = 32 # the number of training examples in one forward/backward pass num_classes = 10 # number of cifar-10 dataset classes epochs = 3 # number of forward and backward passes of all the training examples ''' dataset contains the hyper parameters for loading data and the dataset: dataset = { 'batch_size': batch_size, 'num_classes': num_classes, 'epochs': epochs, 'x_train': x_train, 'x_test': x_test, 'y_train': y_train, 'y_test': y_test } ''' dataset = load_dataset(batch_size, num_classes, epochs) num_population = 4 num_generation = 4 num_offspring = 2 # plot the best model obtained optCNN = genetic_algorithm(num_population, num_generation, num_offspring, dataset) # plot the training and validation loss and accuracy num_epoch = 3 model = optCNN.build_model() model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) history = model.fit(dataset['x_train'], dataset['y_train'], batch_size=dataset['batch_size'], epochs=num_epoch, validation_data=(dataset['x_test'], dataset['y_test']), shuffle=True) optCNN.model = model # model optCNN.fitness = history.history['val_loss'][-1] # fitness print("\n\n-------------------------------------") print("The initial CNN has been evolved successfully in the individual", optCNN.name) print("-------------------------------------\n") daddy = load_network('parent_0') model = tf.keras.models.load_model('parent_0.h5') print("\n\n-------------------------------------") print("Summary of initial CNN") print(model.summary()) print("Fitness of initial CNN:", daddy.fitness) print("\n\n-------------------------------------") print("Summary of evolved individual") print(optCNN.model.summary()) print("Fitness of the evolved individual:", optCNN.fitness) print("-------------------------------------\n") plot_training(history)
def asexual_reproduction(self, it, dataset): # if the individual already exists, just load it if os.path.isfile('net_' + str(it) + '.h5'): print("\n-------------------------------------") print("Loading individual net_" + str(it)) print("--------------------------------------\n") individual = load_network('net_' + str(it)) model = tf.keras.models.load_model(individual.name + '.h5') print("SUMMARY OF", individual.name) print(model.summary()) print("FITNESS: ", individual.fitness) return individual # otherwise, create the individual by mutating the parent individual = Network(it) print("\n-------------------------------------") print("\nCreating individual", individual.name) print("--------------------------------------\n") individual.block_list = deepcopy( self.block_list) # copy the layer list from parent print("----->Strong Mutation") individual.block_mutation(dataset) # mutate a block individual.layer_mutation(dataset) # mutate a layer individual.parameters_mutation() # mutate some parameters model = individual.build_model() if model == -1: return self.asexual_reproduction(it, dataset) individual.train_and_evaluate(model, dataset) return individual
def asexual_reproduction(self, it, dataset): # 如果个体存在就读取它 if os.path.isfile('net_' + str(it) + '.h5'): print("\n-------------------------------------") print("Loading individual net_" + str(it)) print("--------------------------------------\n") individual = load_network('net_' + str(it)) model = tf.keras.models.load_model(individual.name + '.h5') print("SUMMARY OF", individual.name) print(model.summary()) print("FITNESS: ", individual.fitness) return individual # 否则通过变异父代来产生个体 individual = Network(it) print("\n-------------------------------------") print("\nCreating individual", individual.name) print("--------------------------------------\n") individual.block_list = deepcopy(self.block_list) print("----->Strong Mutation") individual.block_mutation(dataset) # 块变异 individual.layer_mutation(dataset) # 层变异 individual.parameters_mutation() # 参数变异 model = individual.build_model() if model == -1: return self.asexual_reproduction(it, dataset) individual.train_and_evaluate(model, dataset) return individual
def compute_parent(dataset): if os.path.isfile('parent_0.h5'): daddy = load_network('parent_0') model = tf.keras.models.load_model('parent_0.h5') print("Loading parent_0") print("SUMMARY OF", daddy.name) print(model.summary()) print("FITNESS:", daddy.fitness) return daddy daddy = Network(0) layerList1 = [ Convolutional(filters=32, filter_size=(3, 3), stride_size=(1, 1), padding='same', input_shape=dataset['x_train'].shape[1:]), Convolutional(filters=32, filter_size=(3, 3), stride_size=(1, 1), padding='valid', input_shape=dataset['x_train'].shape[1:]) ] layerList2 = [ Pooling(pool_size=(2, 2), stride_size=(2, 2), padding='same') ] daddy.block_list.append(Block(0, 0, layerList1, layerList2)) ''' layerList1 = [ Convolutional(filters=64, filter_size=(3, 3), stride_size=(1, 1), padding='same', input_shape=dataset['x_train'].shape[1:]), Convolutional(filters=64, filter_size=(3, 3), stride_size=(1, 1), padding='valid', input_shape=dataset['x_train'].shape[1:]) ] layerList2 = [ Pooling(pool_size=(2, 2), stride_size=(2, 2), padding='same'), Dropout(rate=0.25) ] daddy.block_list.append(Block(1, 1, layerList1, layerList2)) layerList1 = [ Convolutional(filters=128, filter_size=(3, 3), stride_size=(1, 1), padding='same', input_shape=dataset['x_train'].shape[1:]), Convolutional(filters=128, filter_size=(3, 3), stride_size=(1, 1), padding='valid', input_shape=dataset['x_train'].shape[1:]) ] layerList2 = [ Pooling(pool_size=(2, 2), stride_size=(2, 2), padding='same'), Dropout(rate=0.25) ] daddy.block_list.append(Block(1, 1, layerList1, layerList2)) ''' layerList1 = [ FullyConnected(units=128, num_classes=dataset['num_classes']) ] layerList2 = [] daddy.block_list.append(Block(2, 1, layerList1, layerList2)) model = daddy.build_model() daddy.train_and_evaluate(model, dataset) return daddy
from model.network import network from utilities import save_network, load_network from networkx import to_numpy_array net = network(10) print('net gen') tmp_net = to_numpy_array(net.network_plot) print(tmp_net) save_network(net) print('save done') net_cpy = load_network() print(to_numpy_array(net_cpy)) print('load done')