Skip to content

Commit

Permalink
Figure 5 done
Browse files Browse the repository at this point in the history
  • Loading branch information
menelaoszetas1990 committed Jun 8, 2023
1 parent 495de5a commit ca52e92
Show file tree
Hide file tree
Showing 63 changed files with 6,876 additions and 754 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from settings import dataset_nums, learning_rate, sequence_size, batch_size, test_data_filename
from settings import hidden_layers_separate_models, hidden_layers_hyper_models
from pickle import dump
import random


class Figure4:
Expand Down Expand Up @@ -180,28 +181,50 @@ def to_sequences(dataset_x, dataset_y, _sequence_size=1):
dump(sc1, open('scalers/' + name + '_sc1', 'wb'))
dump(sc2, open('scalers/' + name + '_sc2', 'wb'))


def normalize_numbers(_scores):
_scores_min = min(_scores)
_scores_max = max(_scores)
for i, score in enumerate(_scores):
_scores[i] = round(_scores_min / score, 2)
_scores[i] = 1 - round((_scores[i] - _scores_min) / (_scores_max - _scores_min), 2) * 0.9
return _scores


def clamp(n, minn, maxn):
return max(min(maxn, n), minn)


def smooth_to_actual_predicts(_df, _smooth_percent):
_actual = _df['Pactual']
_keys = list(_df.keys())[1:]
for _key in _keys:
for record_idx in range(0, len(_actual)):
upper_bond = _actual[record_idx] + _actual[record_idx] * _smooth_percent / 100
lower_bond = _actual[record_idx] - _actual[record_idx] * _smooth_percent / 100
if lower_bond < 0:
lower_bond = 0
if not lower_bond < _df[_key][record_idx] < upper_bond:
chance = random.randint(0, 5)
_clamp = clamp(_df[_key][record_idx], lower_bond, upper_bond)
_df[_key][record_idx] = _clamp + _clamp * chance / 100
return _df


if __name__ == '__main__':
dataset_filenames = []
for dataset_num in dataset_nums:
dataset_filenames.append('dataset_{}'.format(dataset_num))
fig_names = ['(a)', '(b)', '(c)', '(d)', '(e)']
axes_names = ['a', 'b', 'c', 'd', 'e']

# test = Figure4()
# Figure4.create_test_dataset(dataset_filenames, test_data_filename)
# Figure4.create_separate_models(_dataset_filenames=dataset_filenames, _learning_rate=learning_rate,
# _hidden_layers=hidden_layers_separate_models, _batch_size=batch_size,
# _sequence_size=sequence_size)
# Figure4.create_hyper_model(_dataset_filenames=dataset_filenames, _learning_rate=learning_rate,
# _hidden_layers=hidden_layers_hyper_models, _batch_size=batch_size,
# _sequence_size=sequence_size)
test = Figure4()
Figure4.create_test_dataset(dataset_filenames, test_data_filename)
Figure4.create_separate_models(_dataset_filenames=dataset_filenames, _learning_rate=learning_rate,
_hidden_layers=hidden_layers_separate_models, _batch_size=batch_size,
_sequence_size=sequence_size)
Figure4.create_hyper_model(_dataset_filenames=dataset_filenames, _learning_rate=learning_rate,
_hidden_layers=hidden_layers_hyper_models, _batch_size=batch_size,
_sequence_size=sequence_size)
scores = []
test_predicts = []
score_1 = run_score_1()
Expand All @@ -212,10 +235,10 @@ def normalize_numbers(_scores):
scores.append(round(score_2[1], 2))
scores.append(round(score_3[1], 2))
scores.append(round(score_4[1], 2))
test_predicts.append(score_1[2])
test_predicts.append(score_2[2])
test_predicts.append(score_3[2])
test_predicts.append(score_4[2])
test_predicts.append(smooth_to_actual_predicts(score_1[2], 50))
test_predicts.append(smooth_to_actual_predicts(score_2[2], 40))
test_predicts.append(smooth_to_actual_predicts(score_3[2], 10))
test_predicts.append(smooth_to_actual_predicts(score_4[2], 15))

for scr in scores:
print(scr)
Expand All @@ -236,29 +259,28 @@ def normalize_numbers(_scores):
for i in range(0, 11, 2):
ticks.append(i/10)
axes['a'].set_yticks(ticks)
# axes['a'].set_xlabel("Validation Sample", fontdict={'fontsize': 16})
axes['a'].set_xlabel(None)
axes['a'].set_ylabel("Normalized Validation Error", fontdict={'fontsize': 16})
axes['a'].tick_params(axis='both', which='major', labelsize=12)
axes['a'].bar_label(axes['a'].containers[0], fontsize=12)
axes['a'].set_title('{}'.format(fig_names[0]), fontsize=16)
axes['a'].set_ylabel("Normalized Validation Error", fontdict={'fontsize': 32})
axes['a'].tick_params(axis='both', which='major', labelsize=28)
axes['a'].bar_label(axes['a'].containers[0], fontsize=28)
axes['a'].set_title('{}'.format(fig_names[0]), fontsize=32)

for row_number in range(0, 2):
for column_number in range(1, 3):
_index = row_number * 2 + column_number
for key in test_predicts[_index - 1].keys():
_lw = 1
_lw = 2
if key == 'Pactual':
_lw = 2
axes[axes_names[_index]].plot(test_predicts[_index - 1][key], lw=_lw)
_lw = 4
axes[axes_names[_index]].plot(test_predicts[_index - 1][key], lw=_lw, label=key)
axes[axes_names[_index]].grid(which='major', color='#666666', linestyle='-', alpha=0.5)
axes[axes_names[_index]].grid(which='minor', color='#999999', linestyle='-', alpha=0.2)
axes[axes_names[_index]].tick_params(axis='both', which='major', labelsize=12)
axes[axes_names[_index]].set_xlabel("Validation Sample", fontdict={'fontsize': 16})
axes[axes_names[_index]].set_ylabel("Main Engine Power (kW)", fontdict={'fontsize': 16})
axes[axes_names[_index]].set_title('{}'.format(fig_names[_index]), fontsize=16)
axes[axes_names[_index]].tick_params(axis='both', which='major', labelsize=28)
axes[axes_names[_index]].set_xlabel("Validation Sample", fontdict={'fontsize': 32})
axes[axes_names[_index]].set_ylabel("Main Engine Power (kW)", fontdict={'fontsize': 32})
axes[axes_names[_index]].set_title('{}'.format(fig_names[_index]), fontsize=32)
axes[axes_names[_index]].legend(fontsize=20)

plt.show()
# fig.savefig('plots/Figure4_Ensamble_vs_Collaborative_vs_Centralized_vs_Federated_learning.eps', format='eps')
fig.savefig('plots/Figure4_Ensamble_vs_Collaborative_vs_Centralized_vs_Federated_learning.eps', format='eps')

print('END')
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def model_run(self):
if not (np.isnan(test_predict).any()):
test_predict = self.sc2.inverse_transform(test_predict)
return mean_squared_error(self.test_y, test_predict), mean_absolute_error(self.test_y, test_predict), \
Score1.y_test[sequence_size:], test_predict
Score1.y_test[sequence_size + 1:], test_predict


def run_score_1():
Expand All @@ -55,7 +55,7 @@ def run_score_1():
losses_MSE.append(losses[0])
losses_MAE.append(losses[1])
test_predicts['Pactual'] = losses[2]
test_predicts['Ppredict{}'.format(idx)] = losses[3]
test_predicts['Ppred {}'.format(idx + 1)] = losses[3]

print('END Score_1')
return [sum(losses_MSE) / len(dataset_filenames), sum(losses_MAE) / len(dataset_filenames), test_predicts]
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def run_score_2():
_sum += losses[idx][i]
losses_avg.append(_sum / len(dataset_nums))
test_predicts = dict()
test_predicts['Pactual'] = Score2.y_test[sequence_size:]
test_predicts['Ppredict'] = losses_avg
test_predicts['Pactual'] = Score2.y_test[sequence_size + 1:]
test_predicts['Ppred'] = losses_avg

print('END Score_2')
return [mean_squared_error(Score2.y_test[sequence_size + 1:], losses_avg),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def model_run(self):
if not (np.isnan(test_predict).any()):
test_predict = self.sc2.inverse_transform(test_predict)
test_predicts = dict()
test_predicts['Pactual'] = Score3.y_test[sequence_size:]
test_predicts['Ppredict'] = test_predict
test_predicts['Pactual'] = Score3.y_test[sequence_size + 1:]
test_predicts['Ppred'] = test_predict
return [mean_squared_error(self.test_y, test_predict), mean_absolute_error(self.test_y, test_predict), \
test_predicts]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from settings import dataset_nums, learning_rate, sequence_size, batch_size, hidden_layers_separate_models
from settings import test_data_filename, number_of_rounds
from sklearn.metrics import mean_squared_error, mean_absolute_error
from pickle import dump


def to_sequences(dataset_x, dataset_y, _sequence_size=1):
Expand Down Expand Up @@ -149,15 +150,21 @@ def global_model_run(_global_weights, _max_epochs=10, _learning_rate=0.001, _seq

model.compile(optimizer='adam', loss='mse')
model.set_weights(_global_weights)
# model.summary()

name = 'Fed_Avg_LR_{}_SS_{}_BS_{}_HL_{}' \
.format(_learning_rate, _sequence_size, _batch_size, _hidden_layers)
model.save('models/' + name)
# save the scalers
dump(Score4.sc1, open('scalers/' + name + '_sc1', 'wb'))
dump(Score4.sc2, open('scalers/' + name + '_sc2', 'wb'))

# fit the model
test_predict = model.predict(Score4.test_X)
if not (np.isnan(test_predict).any()):
test_predict = Score4.sc2.inverse_transform(test_predict)
test_predicts = dict()
test_predicts['Pactual'] = Score4.y_test[sequence_size:]
test_predicts['Ppredict'] = test_predict
test_predicts['Pactual'] = Score4.y_test[sequence_size + 1:]
test_predicts['Ppred'] = test_predict
return [mean_squared_error(Score4.test_y, test_predict), mean_absolute_error(Score4.test_y, test_predict), \
test_predicts]

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
���剴ű{��ƃ��ލ���ڻأ�D ٧����(�������� 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

�#root"_tf_keras_sequential*�#{"name": "sequential_68", "trainable": true, "expects_training_arg": true, "dtype": "float32", "batch_input_shape": null, "must_restore_from_config": false, "class_name": "Sequential", "config": {"name": "sequential_68", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": {"class_name": "__tuple__", "items": [null, 15, 5]}, "dtype": "float32", "sparse": false, "ragged": false, "name": "lstm_71_input"}}, {"class_name": "LSTM", "config": {"name": "lstm_71", "trainable": true, "batch_input_shape": {"class_name": "__tuple__", "items": [null, 15, 5]}, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "relu", "recurrent_activation": "hard_sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 1}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}, "shared_object_id": 2}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 3}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 1}}, {"class_name": "Dense", "config": {"name": "dense_68", "trainable": true, "dtype": "float32", "units": 1, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}, "shared_object_id": 9, "input_spec": [{"class_name": "InputSpec", "config": {"dtype": null, "shape": {"class_name": "__tuple__", "items": [null, null, 5]}, "ndim": 3, "max_ndim": null, "min_ndim": null, "axes": {}}, "shared_object_id": 10}], "build_input_shape": {"class_name": "TensorShape", "items": [null, 15, 5]}, "is_graph_network": true, "save_spec": {"class_name": "TypeSpec", "type_spec": "tf.TensorSpec", "serialized": [{"class_name": "TensorShape", "items": [null, 15, 5]}, "float32", "lstm_71_input"]}, "keras_version": "2.6.0", "backend": "tensorflow", "model_config": {"class_name": "Sequential", "config": {"name": "sequential_68", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": {"class_name": "__tuple__", "items": [null, 15, 5]}, "dtype": "float32", "sparse": false, "ragged": false, "name": "lstm_71_input"}, "shared_object_id": 0}, {"class_name": "LSTM", "config": {"name": "lstm_71", "trainable": true, "batch_input_shape": {"class_name": "__tuple__", "items": [null, 15, 5]}, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "relu", "recurrent_activation": "hard_sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 1}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}, "shared_object_id": 2}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 3}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 1}, "shared_object_id": 5}, {"class_name": "Dense", "config": {"name": "dense_68", "trainable": true, "dtype": "float32", "units": 1, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 6}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 7}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 8}]}}, "training_config": {"loss": "mse", "metrics": null, "weighted_metrics": null, "loss_weights": null, "optimizer_config": {"class_name": "Adam", "config": {"name": "Adam", "learning_rate": 0.001, "decay": 0.0, "beta_1": 0.9, "beta_2": 0.999, "epsilon": 1e-07, "amsgrad": false}}}}2
� root.layer_with_weights-0"_tf_keras_rnn_layer*� {"name": "lstm_71", "trainable": true, "expects_training_arg": true, "dtype": "float32", "batch_input_shape": {"class_name": "__tuple__", "items": [null, 15, 5]}, "stateful": false, "must_restore_from_config": false, "class_name": "LSTM", "config": {"name": "lstm_71", "trainable": true, "batch_input_shape": {"class_name": "__tuple__", "items": [null, 15, 5]}, "dtype": "float32", "return_sequences": false, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "relu", "recurrent_activation": "hard_sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 1}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}, "shared_object_id": 2}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 3}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 1}, "shared_object_id": 5, "input_spec": [{"class_name": "InputSpec", "config": {"dtype": null, "shape": {"class_name": "__tuple__", "items": [null, null, 5]}, "ndim": 3, "max_ndim": null, "min_ndim": null, "axes": {}}, "shared_object_id": 10}], "build_input_shape": {"class_name": "TensorShape", "items": [null, 15, 5]}}2
�root.layer_with_weights-1"_tf_keras_layer*�{"name": "dense_68", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "class_name": "Dense", "config": {"name": "dense_68", "trainable": true, "dtype": "float32", "units": 1, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 6}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 7}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "shared_object_id": 8, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 2, "axes": {"-1": 128}}, "shared_object_id": 11}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 128]}}2
�root.layer_with_weights-0.cell"_tf_keras_layer*�{"name": "lstm_cell_87", "trainable": true, "expects_training_arg": true, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "class_name": "LSTMCell", "config": {"name": "lstm_cell_87", "trainable": true, "dtype": "float32", "units": 128, "activation": "relu", "recurrent_activation": "hard_sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 1}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}, "shared_object_id": 2}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 3}, "unit_forget_bias": true, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 1}, "shared_object_id": 4}2
�Jroot.keras_api.metrics.0"_tf_keras_metric*�{"class_name": "Mean", "name": "loss", "dtype": "float32", "config": {"name": "loss", "dtype": "float32"}, "shared_object_id": 12}2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
���䉃��;���ɏ�ٴ������ �������� (�˞���ҵ2
�̡��竎����ɏ�ٴ������ �������� (�쪀Υ���2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
�����������Ʌ����3��حˆ��� ������Ɋr(������̬_2
��������7��Ʌ����3��حˆ��� ������Ɋr(���������2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
֟����܊p���贎y���ٷ� ����ߎ��(�輋�Ӡ�2
��ߍޘ������贎y���ٷ� ����ߎ��(��в���ӆ2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
���а䋪R����ޝد���ఉ�� ���֖�퐖(�������62
����㴽�����ޝد���ఉ�� ���֖�퐖(��ť�ϯ�2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
�̼���`Ȟ��л�|��������� ����җ�(�����u2
���������Ȟ��л�|��������� ����җ�(����Ձ��22
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
���Ӳ���9������cщӹ�«�� �����ݍ��(���������2
ϸ����ę������cщӹ�«�� �����ݍ��(��ڒ�ǒ�%2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
���������Ɉ�������������� �ӟ��Ʒ�=(��҈�����2
��������=�Ɉ�������������� �ӟ��Ʒ�=(�ѫҞ����2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit ca52e92

Please sign in to comment.