def save_results_dem(self): # plt.plot(np.arange(1,len(self.time_complex)+1),self.time_complex) # plt.show() root_train = np.asarray(self.gs_level_train)[K_Levels, :, 0] root_test = np.asarray(self.gs_level_test)[K_Levels, :, 0] # write_file(RS_PATH+self.args.algorithm+"_"+ complex_file_path, time_complex=self.time_complex) # print('Saving complexity..\nOK!') rs_path = RS_PATH + self.args.algorithm + "_" + rs_file_path write_file( file_name=rs_path, root_test=root_test, root_train=root_train, cs_avg_data_test=self.cs_avg_data_test, cs_avg_data_train=self.cs_avg_data_train, cg_avg_data_test=self.cg_avg_data_test, cg_avg_data_train=self.cg_avg_data_train, cs_data_test=self.cs_data_test, cs_data_train=self.cs_data_train, cg_data_test=self.cg_data_test, cg_data_train=self.cg_data_train, gs_level_train=self.gs_level_train, gs_level_test=self.gs_level_test, gg_level_train=self.gg_level_train, gg_level_test=self.gg_level_test, gks_level_train=self.gks_level_train, gks_level_test=self.gks_level_test, gkg_level_train=self.gkg_level_train, gkg_level_test=self.gkg_level_test, dendo_data=self.dendo_data, dendo_data_round=self.dendo_data_round, # Dendrogram data N_clients=[N_clients], TREE_UPDATE_PERIOD=[TREE_UPDATE_PERIOD]) # Setting plot_from_file(rs_path)
def train_model(model, trainloader): learning_rate = 0.001 num_epochs = 40 epochs_stop = 5 min_loss = np.Inf epoch_min_loss = np.Inf no_improve = 0 criterion = torch.nn.CrossEntropyLoss() optimizer = Adam(model.parameters(), lr=learning_rate) total_step = len(trainloader) loss_list = [] acc_list = [] for epoch in range(num_epochs): for i, (labels, sounds) in enumerate(trainloader): if torch.cuda.is_available(): labels = labels.cuda() sounds = sounds.cuda() # Run the forward pass outputs = model(sounds) loss = criterion(outputs, labels) loss_list.append(loss.item()) if loss < min_loss: min_loss = loss # Backprop and perform Adam optimization optimizer.zero_grad() loss.backward() optimizer.step() # Track the accuracy total = labels.size(0) _, predicted = torch.max(outputs.data, 1) correct = (predicted == labels).sum().item() acc_list.append(correct / total) if (i + 1) % 10 in [0, 10]: print( 'Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}, Accuracy: {:.2f}%' .format(epoch + 1, num_epochs, i + 1, total_step, loss.item(), (correct / total) * 100)) if min_loss < epoch_min_loss: epoch_min_loss = min_loss no_improve = 0 else: no_improve += 1 if no_improve == epochs_stop: break else: continue loss_list.append('-----' + str(epoch) + '-----') write_file('accuracy.txt', acc_list) write_file('loss.txt', loss_list)
def save_results1(self): rs_path = RS_PATH + rs_file_path write_file(file_name=rs_path, root_test=self.rs_glob_acc, root_train=self.rs_train_acc, cs_avg_data_test=self.cs_avg_data_test, cs_avg_data_train=self.cs_avg_data_train, cg_avg_data_test=self.cg_avg_data_test, cg_avg_data_train=self.cg_avg_data_train, cs_data_test=self.cs_data_test, cs_data_train=self.cs_data_train, cg_data_test=self.cg_data_test, cg_data_train=self.cg_data_train, N_clients=[N_clients]) plot_from_file(rs_path)
def test_model(model, testloader): correct = 0 total = 0 y = [] y_predicted = [] with torch.no_grad(): for labels, sounds in testloader: if torch.cuda.is_available(): labels = labels.cuda() sounds = sounds.cuda() outputs = model(sounds) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() y_predicted.append(outputs.numpy()) y.append(labels.numpy()) print('Accuracy: %d %%' % (100 * correct / total)) write_file('accuracy_test.txt', [(100 * correct / total)]) torch.save(model.state_dict(), '.')