def evaluation(nets, train_inputs, train_outputs, batch_size, iters, test_inputs, test_outputs, _): """ In this caxe two simple MLPs are declarated and added to the model but with different inputs and outputs, so they are separated. Both training and testing will be done with different data and two final results will be obtained from the evaluation of the model. :param nets: Dictionary with the networks that will be used to build the final network and that represent the individuals to be evaluated in the genetic algorithm. :param train_inputs: Input data for training, this data will only be used to give it to the created networks and train them. :param train_outputs: Output data for training, it will be used to compare the returned values by the networks and see their performance. :param batch_size: Number of samples per batch are used during training process. :param iters: Number of iterations that each network will be trained. :param test_inputs: Input data for testing, this data will only be used to give it to the created networks and test them. It can not be used during training in order to get a real feedback. :param test_outputs: Output data for testing, it will be used to compare the returned values by the networks and see their real performance. :param hypers: Hyperparameters that are being evolved and used in the process. :return: Two accuracy errors, one from each MLP in the model. """ inp_0 = Input(shape=train_inputs["i0"].shape[1:]) out_0 = Flatten()(inp_0) out_0 = nets["n0"].building(out_0) inp_1 = Input(shape=train_inputs["i1"].shape[1:]) out_1 = Flatten()(inp_1) out_1 = nets["n1"].building(out_1) model = Model(inputs=[inp_0, inp_1], outputs=[out_0, out_1]) opt = tf.keras.optimizers.Adam(learning_rate=0.01) model.compile(loss=[tf.nn.softmax_cross_entropy_with_logits, tf.nn.softmax_cross_entropy_with_logits], optimizer=opt, metrics=[]) # As the output has to be the same as the input, the input is passed twice model.fit([train_inputs['i0'], train_inputs['i1']], [train_outputs['o0'], train_outputs['o1']], epochs=iters, batch_size=batch_size, verbose=0) #tf.keras.utils.plot_model(model, "multi_input_and_output_model.png", show_shapes=True) pred_0, pred_1 = model.predict([test_inputs['i0'], test_inputs['i1']]) res_0 = tf.nn.softmax(pred_0) res_1 = tf.nn.softmax(pred_1) # Return both accuracies return accuracy_error(res_0, test_outputs["o0"]), accuracy_error(res_1, test_outputs["o1"])
def eval_sequential(nets, train_inputs, train_outputs, batch_size, iters, test_inputs, test_outputs, hypers): """ The model that will evaluate the data is formed by a MLP and then other MLP. As they are compatible between them, there is no need of extra layers between them. Softmax cross entropy is used for the training and accuracy error for evaluating the network. :param nets: Dictionary with the networks that will be used to build the final network and that represent the individuals to be evaluated in the genetic algorithm. :param train_inputs: Input data for training, this data will only be used to give it to the created networks and train them. :param train_outputs: Output data for training, it will be used to compare the returned values by the networks and see their performance. :param batch_size: Number of samples per batch are used during training process. :param iters: Number of iterations that each network will be trained. :param test_inputs: Input data for testing, this data will only be used to give it to the created networks and test them. It can not be used during training in order to get a real feedback. :param test_outputs: Output data for testing, it will be used to compare the returned values by the networks and see their real performance. :param hypers: Hyperparameters that are being evolved and used in the process. :return: Accuracy error obtained with the test data that evaluates the true performance of the network. """ inp = Input(shape=train_inputs["i0"].shape[1:]) out = Flatten()(inp) out = nets["n0"].building(out) out = nets["n1"].building(out) model = Model(inputs=inp, outputs=out) opt = optimizers[hypers["optimizer"]](learning_rate=hypers["lrate"]) model.compile(loss=tf.nn.softmax_cross_entropy_with_logits, optimizer=opt, metrics=[]) model.fit(train_inputs['i0'], train_outputs['o0'], epochs=iters, batch_size=batch_size, verbose=0) pred = model.predict(test_inputs['i0']) res = tf.nn.softmax(pred) return accuracy_error(test_outputs["o0"], res),
def eval_cnn(nets, train_inputs, train_outputs, batch_size, iters, test_inputs, test_outputs, hypers): """ Creates the model formed by the SkipCNN that has been created and then a MLP is sequenetialy added with a flatten and a dense layer in between. That model is trained by using cross entropy function and the final evaluation is done with the accuracy error. :param nets: Dictionary with the networks that will be used to build the final network and that represent the individuals to be evaluated in the genetic algorithm. :param train_inputs: Input data for training, this data will only be used to give it to the created networks and train them. :param train_outputs: Output data for training, it will be used to compare the returned values by the networks and see their performance. :param batch_size: Number of samples per batch are used during training process. :param iters: Number of iterations that each network will be trained. :param test_inputs: Input data for testing, this data will only be used to give it to the created networks and test them. It can not be used during training in order to get a real feedback. :param test_outputs: Output data for testing, it will be used to compare the returned values by the networks and see their real performance. :param hypers: Hyperparameters that are being evolved and used in the process. :return: Accuracy error obtained with the test data that evaluates the true performance of the network. """ inp = Input(shape=train_inputs["i0"].shape[1:]) out = nets["n0"].building(inp, hypers["skip"]) out = Flatten()(out) out = Dense(20)(out) out = nets["n1"].building(out) model = Model(inputs=inp, outputs=out) opt = optimizers[hypers["optimizer"]](learning_rate=hypers["lrate"]) model.compile(loss=tf.nn.softmax_cross_entropy_with_logits, optimizer=opt, metrics=[]) model.fit(train_inputs['i0'], train_outputs['o0'], epochs=iters, batch_size=batch_size, verbose=0) preds = model.predict(test_inputs["i0"]) res = tf.nn.softmax(preds) return accuracy_error(test_outputs["o0"], res),
def eval_model(nets, train_inputs, train_outputs, batch_size, iters, test_inputs, test_outputs, hypers): """ The model that will evaluate the data is a simple MLP. Data passed to those models in this case will be noise, models will have to predict a probability distribution with the size of the bounds of the build. :param nets: Dictionary with the networks that will be used to build the final network and that represent the individuals to be evaluated in the genetic algorithm. :param train_inputs: Input data for training, this data will only be used to give it to the created networks and train them. :param train_outputs: Output data for training, it will be used to compare the returned values by the networks and see their performance. :param batch_size: Number of samples per batch are used during training process. :param iters: Number of iterations that each network will be trained. :param test_inputs: Input data for testing, this data will only be used to give it to the created networks and test them. It can not be used during training in order to get a real feedback. :param test_outputs: Output data for testing, it will be used to compare the returned values by the networks and see their real performance. :param hypers: Hyperparameters that are being evolved and used in the process. :return: Accuracy error calculated from the networks prediction an real data. """ # In the autoencoder would be the flattened representation of the solutions inp = Input(shape=train_inputs['i0'].shape[1:]) out = nets['n0'].building(inp) out = Softmax()(out) out = Reshape((np.prod(bounds), n_wanted_blocks))(out) model = Model(inputs=inp, outputs=out) #model.summary() opt = optimizers[hypers["optimizer"]](learning_rate=hypers["lrate"]) model.compile(loss=tf.nn.softmax_cross_entropy_with_logits, optimizer=opt, metrics=[]) model.fit(train_inputs['i0'], train_outputs['o0'], epochs=iters, batch_size=batch_size, verbose=0) res = model.predict(train_inputs['i0']) ev = accuracy_error(train_outputs["o0"], res) return ev,
def eval_rnn(nets, train_inputs, train_outputs, batch_size, iters, test_inputs, test_outputs, hypers): """ The model is created with one RNN, but it needs a dense layer with a softmax activation function. That is needed because they are probability distributions and they have to be between 0 and 1. Finally accuracy error is used to measuare the performance of the model. :param nets: Dictionary with the networks that will be used to build the final network and that represent the individuals to be evaluated in the genetic algorithm. :param train_inputs: Input data for training, this data will only be used to give it to the created networks and train them. :param train_outputs: Output data for training, it will be used to compare the returned values by the networks and see their performance. :param batch_size: Number of samples per batch are used during training process. :param iters: Number of iterations that each network will be trained. :param test_inputs: Input data for testing, this data will only be used to give it to the created networks and test them. It can not be used during training in order to get a real feedback. :param test_outputs: Output data for testing, it will be used to compare the returned values by the networks and see their real performance. :param hypers: Hyperparameters that are being evolved and used in the process. :return: Accuracy error obtained with the test data that evaluates the true performance of the network. """ inp = Input(shape=train_inputs["i0"].shape[1:]) out = nets["n0"].building(inp) out = Dense(10, activation='softmax')(out) # As they are probability distributions, they have to be bewteen 0 an 1 model = Model(inputs=inp, outputs=out) model.summary() opt = optimizers[hypers["optimizer"]](learning_rate=hypers["lrate"]) model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), optimizer=opt, metrics=[]) model.fit(train_inputs['i0'], train_outputs['o0'], epochs=iters, batch_size=batch_size, verbose=0) preds = model.predict(test_inputs["i0"]) res = tf.nn.softmax(preds) return accuracy_error(test_outputs["o0"], res),