Splitting the data and train the ANN#
We can start by loading data and split into training set and evaluating set:
def split_data():
inputs, targets, timestamps = utils.read_input()
return preprocessing.split(
inputs,
targets,
timestamps,
TRAINING_SIZE,
EVALUATION_SIZE,
)
Then we only need to create tensorflow.Dataset from the splitted data and create an ANN:
def run():
(train_inputs, train_targets, train_timestamps), (test_inputs, test_targets, test_timestamps) = split_data()
# creating Tensorflow's Dataset
train_dataset = tf.data.Dataset.from_tensor_slices(
(
train_inputs.reshape(1, *train_inputs.shape),
train_targets.reshape(1, *train_targets.shape),
)
)
test_dataset = tf.data.Dataset.from_tensor_slices(
(
test_inputs.reshape(1, *test_inputs.shape),
test_targets.reshape(1, *test_targets.shape),
)
)
Once the datasets are created, you can initialize the ModelSet and train it:
# initialize a list of 25 multi-layers perceptron
model_set = nn.ModelSet(
nb_models=25,
input_shape=(len(INPUTS_VAR),),
output_shape=len(TARGETS_VAR),
)
# train the MLP
model_set.fit(train_dataset, epochs=500, steps_per_epoch=5)
# evaluate the MLP
model_set.evaluate(test_dataset)
# get the prediction
prediction = np.median(model_set(test_inputs)[:, 0], axis=0)
# save the result
utils.write_output(pd.DataFrame(prediction, columns=TARGETS_VAR))
During the evaluation, the models that fail to estimates fluxes are deleted. The output is saved in my_output_dir/output/output.csv.