# log time start_time = time.time() for epoch in range(num_epoch): for data in dataloader: inp = data[1] target = data[2] output = model(inp) loss = criterion(output, target) optimizer.zero_grad() loss.backward() optimizer.step() # log acc = models.cal_accuracy(dev_dataset, model) print('epoch [{}/{}], loss:{:.4f}, accuracy:{:.4f}'.format( epoch + 1, num_epoch, loss.data.item(), acc)) log_df.loc[epoch] = [epoch + 1, loss.data.item(), acc] # time log print("--- %s seconds ---" % (time.time() - start_time)) # save log df if is_consider_residual: log_df.to_pickle(f'{log_path}/conv_hybrid_loss_acc_log.pkl') else: log_df.to_pickle(f'{log_path}/conv_hybrid_no_res_loss_acc_log.pkl') def print_acc(model, dataset, print_note=''): acc = models.cal_accuracy(dataset, model)
def print_acc(model, dataset, print_note=''): acc = models.cal_accuracy(dataset, model) print(print_note, 'accuracy: ', acc)
# file paths model_path = 'networks/model' # prepare test set test_dataset, channel_len, _ = load_labeled_dataset() # load model autoencoder = models.sdae(dimensions=[channel_len, 10, 10, 20, 3]) model = models.sdae_lr(autoencoder) model.load_state_dict( torch.load(f'{model_path}/ae_on_{data_class}.pth', map_location='cpu')) model.eval() # view output acc = models.cal_accuracy(test_dataset, model) print('accuracy: ', acc) features = models.encode(test_dataset, model) features = features.numpy() features_with_label = {0: [], 1: [], 2: []} for i in range(len(test_dataset)): label = test_dataset.grdtruth[i].item() features_with_label[label].append(features[i]) fig = plt.figure() ax = Axes3D(fig) colors = ['r', 'g', 'b'] for i in range(3): class_f = np.array(features_with_label[i])