print('pretrain Autoencoder model (epoch %d, total_steps %d)' % (epoch, pre_epoch_AE)) commonZ = [] if epoch > 0: for i in range(len(train_dataset_a)): tempimage_a = torch.from_numpy(train_dataset_a[i]).view(1, 1, dim1) tempimage_b = torch.from_numpy(train_dataset_b[i]).view(1, 1, dim2) model.set_input(tempimage_a, tempimage_b) t_200 = np.array(model.test_commonZ().data.view(n_com).tolist()) commonZ.append(t_200) ##kmeans result estimator = KMeans(n_clusters) estimator.fit(commonZ) centroids = estimator.cluster_centers_ label_pred = estimator.labels_ acc = metrics.acc(label_true, label_pred) nmi = metrics.nmi(label_true, label_pred) ACC_all.append(acc) NMI_all.append(nmi) # Z_path = 'commonZ' + str(epoch) # sio.savemat(Z_path + '.mat', {'Z': commonZ}) print(' ' * 8 + '|==> acc: %.4f, nmi: %.4f <==|' % (acc, nmi)) if acc > 0.95: break centroids0 = estimator.cluster_centers_ ######## center0 = torch.FloatTensor(centroids0).cuda() model.clu.weights.data = center0 ######### sio.savemat('commonZAE.mat', {'Z': commonZ})
list(train_dataset_b) + list(fb_500) + list(untrain_dataset_b)) sio.savemat('fakea2.mat', {'fa': fa_500}) sio.savemat('fakeb2.mat', {'fb': fb_500}) commonZ_step2 = [] for i in range(len(test_dataset_A2000)): tempimage_a = torch.from_numpy(test_dataset_A2000[i]).view(1, 1, dim1) tempimage_b = torch.from_numpy(test_dataset_B2000[i]).view(1, 1, dim2) model.set_input(tempimage_a, tempimage_b) t_200 = np.array(model.test_commonZ().data.view(n_com).tolist()) commonZ_step2.append(t_200) estimator = KMeans(n_clusters) estimator.fit(commonZ_step2) centroids_step2 = estimator.cluster_centers_ label_pred = estimator.labels_ acc = metrics.acc(label_true_all, label_pred) nmi = metrics.nmi(label_true_all, label_pred) ACC_all.append(acc) NMI_all.append(nmi) print(' ' * 8 + '|==> acc: %.4f, nmi: %.4f <==|' % (acc, nmi)) sio.savemat('commonZ.mat', {'commonZ': commonZ_step2}) # test #groundtruth = [] #rmse_x = [] #rmse_y = [] #fa_600 = [] #fb_600 = [] #print(len(untrain_dataset_a)) #print(len(test_dataset_a))
if epoch % 1 == 0: print("Epoch {}/{}".format(epoch, n_epochs2)) print("Loss is:{:.4f}".format(loss.item())) print("Loss_cca is:{:.4f}".format(loss_cca.item())) print("loss_selfc is:{:.4f}".format(loss_selfc.item())) print("loss_selfc12 is:{:.4f}".format(loss_selfc12.item())) print("loss_self is:{:.4f}".format(loss_self.item())) print("loss_d is:{:.4f}".format(loss_d.item())) coef = model.weight - torch.diag(torch.diag(model.weight)) commonC = coef.cpu().detach().numpy() alpha = max(0.4 - (k_num - 1) / 10 * 0.1, 0.1) All_C = thrC(commonC, alpha) preds, _ = post_proC(All_C, k_num) acc = metrics.acc(label_true, preds) nmi = metrics.nmi(label_true, preds) pur = metrics.purity_score(label_true, preds) ACC.append(acc) NMI.append(nmi) PUR.append(pur) print(' ' * 8 + '|==> acc: %.4f, nmi: %.4f, pur: %.4f <==|' % (acc, nmi, pur)) if acc > 0.955: C_path = 'All_C' + str(epoch) sio.savemat(C_path + '.mat', {'C': All_C}) sio.savemat('Pred' + str(epoch) + '.mat', {'Pred': preds}) sio.savemat('ACC' + '.mat', {'acc': ACC}) sio.savemat('NMI' + '.mat', {'nmi': NMI})