|
| 1 | +import numpy as np |
| 2 | +import os, sys |
| 3 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
| 4 | +from control_algorithm.adaptive_tau import ControlAlgAdaptiveTauServer |
| 5 | +from config import * |
| 6 | + |
| 7 | + |
| 8 | +class CollectStatistics: |
| 9 | + def __init__(self, results_file_name=os.path.dirname(__file__)+'/results.csv', is_single_run=False): |
| 10 | + self.results_file_name = results_file_name |
| 11 | + self.is_single_run = is_single_run |
| 12 | + |
| 13 | + if not os.path.exists(os.path.dirname(results_file_name)): |
| 14 | + os.makedirs(os.path.dirname(results_file_name)) |
| 15 | + if is_single_run: |
| 16 | + with open(results_file_name, 'a') as f: |
| 17 | + f.write( |
| 18 | + 'case,tValue,lossValue,predictionAccuracy,betaAdapt,deltaAdapt,rhoAdapt,tau,it_each_local,it_each_global\n') |
| 19 | + f.close() |
| 20 | + else: |
| 21 | + with open(results_file_name, 'a') as f: |
| 22 | + f.write( |
| 23 | + 'Type,Simulation,case,tau_setup,lossValue,predictionAccuracy,avg_tau,stddev_tau,' + |
| 24 | + 'avg_each_local,stddev_each_local,avg_each_global,stddev_each_global,' + |
| 25 | + 'avg_betaAdapt,stddev_betaAdapt,' + |
| 26 | + 'avg_deltaAdapt,stddev_deltaAdapt,avg_rhoAdapt,stddev_rhoAdapt,' + |
| 27 | + 'total_time_recomputed\n') |
| 28 | + f.close() |
| 29 | + |
| 30 | + def init_stat_new_global_round(self): |
| 31 | + |
| 32 | + if self.is_single_run: |
| 33 | + self.loss_values = [] |
| 34 | + self.prediction_accuracies = [] |
| 35 | + self.t_values = [] |
| 36 | + |
| 37 | + self.taus = [] |
| 38 | + self.each_locals = [] |
| 39 | + self.each_globals = [] |
| 40 | + self.beta_adapts = [] |
| 41 | + self.delta_adapts = [] |
| 42 | + self.rho_adapts = [] |
| 43 | + |
| 44 | + def init_stat_new_global_round_comp(self): |
| 45 | + |
| 46 | + if self.is_single_run: |
| 47 | + self.loss_values = [] |
| 48 | + self.prediction_accuracies = [] |
| 49 | + self.t_values = [] |
| 50 | + self.k = [] |
| 51 | + self.immediate_cost =[] |
| 52 | + |
| 53 | + def collect_stat_end_local_round(self, case, tau, it_each_local, it_each_global, control_alg, model, train_image, |
| 54 | + train_label, test_image, test_label, w_global, total_time_recomputed): |
| 55 | + |
| 56 | + self.taus.append(tau) # Use calculated tau |
| 57 | + self.each_locals.append(it_each_local) |
| 58 | + self.each_globals.append(it_each_global) |
| 59 | + |
| 60 | + if control_alg is not None: |
| 61 | + # TODO: Should define a getter in control algorithm class and use it here |
| 62 | + if isinstance(control_alg, ControlAlgAdaptiveTauServer): |
| 63 | + if control_alg.beta_adapt_mvaverage is not None: |
| 64 | + self.beta_adapts.append(control_alg.beta_adapt_mvaverage) |
| 65 | + elif self.is_single_run: |
| 66 | + self.beta_adapts.append(np.nan) |
| 67 | + |
| 68 | + if control_alg.delta_adapt_mvaverage is not None: |
| 69 | + self.delta_adapts.append(control_alg.delta_adapt_mvaverage) |
| 70 | + elif self.is_single_run: |
| 71 | + self.delta_adapts.append(np.nan) |
| 72 | + |
| 73 | + if control_alg.rho_adapt_mvaverage is not None: |
| 74 | + self.rho_adapts.append(control_alg.rho_adapt_mvaverage) |
| 75 | + elif self.is_single_run: |
| 76 | + self.rho_adapts.append(np.nan) |
| 77 | + |
| 78 | + else: |
| 79 | + if self.is_single_run: # When doing a single run, the array needs to align with the timestamp, |
| 80 | + # thus adding an entry on None |
| 81 | + self.beta_adapts.append(np.nan) |
| 82 | + self.delta_adapts.append(np.nan) |
| 83 | + self.rho_adapts.append(np.nan) |
| 84 | + |
| 85 | + if self.is_single_run: |
| 86 | + |
| 87 | + loss_value = model.loss(train_image, train_label, w_global) |
| 88 | + self.loss_values.append(loss_value) |
| 89 | + |
| 90 | + prediction_accuracy = model.accuracy(test_image, test_label, w_global) |
| 91 | + self.prediction_accuracies.append(prediction_accuracy) |
| 92 | + |
| 93 | + self.t_values.append(total_time_recomputed) |
| 94 | + |
| 95 | + print("***** lossValue: " + str(loss_value)) |
| 96 | + |
| 97 | + with open(self.results_file_name, 'a') as f: |
| 98 | + f.write(str(case) + ',' + str(total_time_recomputed) + ',' + str(loss_value) + ',' |
| 99 | + + str(prediction_accuracy) + ',' |
| 100 | + + str(self.beta_adapts[-1]) + ',' + str(self.delta_adapts[-1]) + ',' + str(self.rho_adapts[-1]) |
| 101 | + + ',' + str(tau) + ',' + str(it_each_local) + ',' + str(it_each_global) + '\n') |
| 102 | + f.close() |
| 103 | + |
| 104 | + def collect_stat_end_local_round_comp(self, case, num_iter, model, train_image, train_label, test_image, test_label, |
| 105 | + w_global, total_time_recomputed, k=None, cost=None): |
| 106 | + if self.is_single_run: |
| 107 | + loss_value = model.loss(train_image, train_label, w_global) |
| 108 | + self.loss_values.append(loss_value) |
| 109 | + |
| 110 | + prediction_accuracy = model.accuracy(test_image, test_label, w_global) |
| 111 | + self.prediction_accuracies.append(prediction_accuracy) |
| 112 | + |
| 113 | + self.t_values.append(total_time_recomputed) |
| 114 | + self.k.append(k) |
| 115 | + self.immediate_cost.append(cost) |
| 116 | + |
| 117 | + print("***** lossValue: " + str(loss_value)) |
| 118 | + |
| 119 | + with open(self.results_file_name, 'a') as f: |
| 120 | + f.write(str(case) + ',' + str(num_iter) + ',' + str(total_time_recomputed) + ',' + str(loss_value) + ',' |
| 121 | + + str(prediction_accuracy) + ',' + str(k) + ',' + str(cost) + '\n') |
| 122 | + f.close() |
| 123 | + |
| 124 | + def collect_stat_end_global_round(self, sim, case, tau_setup, total_time, model, train_image, train_label, |
| 125 | + test_image, test_label, w_eval, total_time_recomputed): |
| 126 | + loss_final = model.loss(train_image, train_label, w_eval) |
| 127 | + accuracy_final = model.accuracy(test_image, test_label, w_eval) |
| 128 | + |
| 129 | + if not self.is_single_run: |
| 130 | + taus_array = np.array(self.taus) |
| 131 | + avg_tau = np.sum(np.dot(taus_array, taus_array)) / np.sum(taus_array) |
| 132 | + stddev_tau = np.std(taus_array) |
| 133 | + avg_each_local = np.mean(np.array(self.each_locals)) |
| 134 | + stddev_each_local = np.std(np.array(self.each_locals)) |
| 135 | + avg_each_global = np.mean(np.array(self.each_globals)) |
| 136 | + stddev_each_global = np.std(np.array(self.each_globals)) |
| 137 | + avg_beta_adapt = np.mean(np.array(self.beta_adapts)) |
| 138 | + stddev_beta_adapt = np.std(np.array(self.beta_adapts)) |
| 139 | + avg_delta_adapt = np.mean(np.array(self.delta_adapts)) |
| 140 | + stddev_delta_adapt = np.std(np.array(self.delta_adapts)) |
| 141 | + avg_rho_adapt = np.mean(np.array(self.rho_adapts)) |
| 142 | + stddev_rho_adapt = np.std(np.array(self.rho_adapts)) |
| 143 | + |
| 144 | + if case is None or np.isnan(case): |
| 145 | + case = None |
| 146 | + type_str = 'centralized' |
| 147 | + else: |
| 148 | + type_str = 'distributed' |
| 149 | + |
| 150 | + with open(self.results_file_name, 'a') as f: |
| 151 | + f.write(type_str + ',' + str(sim) + ',' + str(case) + ',' + str(tau_setup) + ',' |
| 152 | + + str(loss_final) + ',' + str(accuracy_final) + ',' + str(avg_tau) + ',' + str(stddev_tau) + ',' |
| 153 | + + str(avg_each_local) + ',' + str(stddev_each_local) + ',' |
| 154 | + + str(avg_each_global) + ',' + str(stddev_each_global) + ',' |
| 155 | + + str(avg_beta_adapt) + ',' + str(stddev_beta_adapt) + ',' |
| 156 | + + str(avg_delta_adapt) + ',' + str(stddev_delta_adapt) + ',' |
| 157 | + + str(avg_rho_adapt) + ',' + str(stddev_rho_adapt) + ',' |
| 158 | + + str(total_time_recomputed) + ',' |
| 159 | + + '\n') |
| 160 | + f.close() |
| 161 | + |
| 162 | + print('total time', total_time) |
| 163 | + print('loss value', loss_final) |
| 164 | + print('accuracy', accuracy_final) |
0 commit comments