def forward(self, source, target, s_label): source = self.feature_layers(source) if bottle_neck: source = self.bottle(source) s_pred = self.cls_fc(source) if self.training == True: target = self.feature_layers(target) if bottle_neck: target = self.bottle(target) t_label = self.cls_fc(target) #原始的lmmd #loss = mmd.lmmd(source, target, s_label, torch.nn.functional.softmax(t_label, dim=1),num_classes = self.num_classes) #高斯核mmd loss = mmd.mmd_rbf(source, target) else: loss = 0 return s_pred, loss
#DifferenceDistribution diff_1 = [] for i in range(10): diff_1.append( [random.lognormvariate(mu, sigma) for _ in range(1, SAMPLE_SIZE)]) diff_2 = [] for i in range(10): diff_2.append( [random.betavariate(alpha, beta) for _ in range(1, SAMPLE_SIZE)]) X = torch.Tensor(diff_1) Y = torch.Tensor(diff_2) X, Y = Variable(X), Variable(Y) print(mmd_rbf(X, Y)) #SimilarDistributionData same_1 = [] for i in range(10): same_1.append( [random.lognormvariate(mu, sigma) for _ in range(1, SAMPLE_SIZE)]) same_2 = [] for i in range(10): same_2.append( [random.lognormvariate(mu, sigma) for _ in range(1, SAMPLE_SIZE)]) X = torch.Tensor(same_1) Y = torch.Tensor(same_2) X, Y = Variable(X), Variable(Y)
def train_nolabel(net1, net2, train_data, test_data, epoch, optimizer_en, optimizer_de, criterion): if torch.cuda.is_available(): net1 = torch.nn.DataParallel(net1, device_ids=device_ids) net2 = torch.nn.DataParallel(net2, device_ids=device_ids) net1 = net1.cuda() net2 = net2.cuda() # prev_time = datetime.now() # train_loss = 0 train_loss1 = 0 train_loss2 = 0 train_loss3 = 0 train_loss4 = 0 train_loss5 = 0 # net1 = net1.train() net2 = net2.train() # map_dict = read_pkl(PKL) tensor_empty_MMD = torch.Tensor([]).cuda() for label_index in range(classnum): tensor_empty_MMD = torch.cat( (tensor_empty_MMD, map_dict[label_index].float().cuda()), 0) # for im, label in tqdm(train_data, desc="Processing train data: "): im11 = torch.Tensor([]) im12 = torch.Tensor([]) im13 = torch.Tensor([]) im14 = torch.Tensor([]) im15 = torch.Tensor([]) for i in range(im.size(0)): im11 = torch.cat( (im11, normalize(loader(Transformations1( (unloader(im[i]))))).unsqueeze(0)), 0) im12 = torch.cat( (im12, normalize(loader(Transformations2( (unloader(im[i]))))).unsqueeze(0)), 0) im13 = torch.cat( (im13, normalize(loader(Transformations3( (unloader(im[i]))))).unsqueeze(0)), 0) im14 = torch.cat( (im14, normalize(loader(Transformations4( (unloader(im[i]))))).unsqueeze(0)), 0) im15 = torch.cat( (im15, normalize(loader(Transformations5( (unloader(im[i]))))).unsqueeze(0)), 0) im = im.cuda() im11 = im11.cuda() im12 = im12.cuda() im13 = im13.cuda() im14 = im14.cuda() im15 = im15.cuda() # net output output_classifier11, output_classifier11_softmax = net1(im11) output_classifier12, output_classifier12_softmax = net1(im12) output_classifier13, output_classifier13_softmax = net1(im13) output_classifier14, output_classifier14_softmax = net1(im14) output_classifier15, output_classifier15_softmax = net1(im15) output_classifier, output_classifier_softmax = net1(im) output_no_noise = net2(output_classifier) # KLD loss1 = 1 * (kl_categorical(output_classifier13_softmax, output_classifier_softmax) + kl_categorical(output_classifier12_softmax, output_classifier_softmax) + kl_categorical(output_classifier11_softmax, output_classifier_softmax) + kl_categorical(output_classifier14_softmax, output_classifier_softmax) + kl_categorical(output_classifier15_softmax, output_classifier_softmax)) # mse loss5 = 0.01*(criterion(output_classifier13, output_classifier14) \ +criterion(output_classifier12, output_classifier13) \ +criterion(output_classifier12, output_classifier11) \ +criterion(output_classifier14, output_classifier) \ +criterion(output_classifier15, output_classifier14)) ############################################################### # add noise --mse boundary sigma = generator_noise(output_classifier.size(0), output_classifier.size(1)) new_out = output_classifier + torch.from_numpy( (0.05 * sigma) / ((output_classifier.size(1)**0.5))).float().cuda() output_noise = net2(new_out) # ############################################################### # decoder no noise --mse loss2 = criterion(output_no_noise, im) ############################################################### loss3 = 0.05 * sobel_1vs1_1D(im, output_noise, criterion) # z_Pedcc = torch.Tensor([]).cuda() # z_real = output_classifier # use hook to see gradient # z_real.register_hook(print_grad) # batchsize = output_classifier.shape[0] for b in range(1 * (batchsize // classnum)): z_Pedcc = torch.cat((z_Pedcc, tensor_empty_MMD), 0) z_Pedcc = torch.cat( (z_Pedcc, tensor_empty_MMD[0:batchsize % classnum])) # MMD weight ############################################################### loss4 = 1 * mmd_rbf(z_real, z_Pedcc) z_real.register_hook(print_grad) # loss loss = loss5 + loss3 + loss4 + loss2 # backward optimizer_en.zero_grad() optimizer_de.zero_grad() # loss.backward() # optimizer_en.step() optimizer_de.step() # train_loss += loss.item() train_loss1 += loss1.item() train_loss2 += loss2.item() train_loss3 += loss3.item() train_loss4 += loss4.item() train_loss5 += loss5.item() # # deal grad # x_norm = np.linalg.norm(np.array(grad_list),ord=None,axis=1,keepdims=False).sum() # # print(x_norm) # # # x_norm=x_norm / np.array(grad_list).shape[0]*BatchSize # grad_res.append(x_norm) # grad_list.clear() # curr_time = datetime.now() h, remainder = divmod((curr_time - prev_time).seconds, 3600) m, s = divmod(remainder, 60) time_str = " Time %02d:%02d:%02d" % (h, m, s) epoch_str = ("Epoch %d. " % (epoch)) Loss = ("Train Loss1: %f, Train Loss2: %f,Train Loss3: %f,Train Loss4: %f," "Train Loss5: %f," % (train_loss1 / len(train_data), train_loss2 / len(train_data), train_loss3 / len(train_data), train_loss4 / len(train_data), train_loss5 / len(train_data))) grad_loss1.append(train_loss1 / len(train_data)) grad_loss4.append(train_loss4 / len(train_data)) ############### test ############# torch.save(net1, './model_encoder.pth') net11 = torch.load('./model_encoder.pth') for i in net11.parameters(): i.requires_grad = False Acc, NMI = test(net11, test_data) grad_png.append(Acc) ############### test ############# # build dir File = './model/MNIST/60/MSE1/' if not os.path.isdir(File): os.makedirs(File) if epoch % 2 == 0 and epoch != 0: res_plot = np.array(grad_loss1) np.savetxt(File + '_loss1_aug.txt', res_plot, fmt='%0.8f') plt.plot([i for i in range(len(grad_loss1))], grad_loss1) plt.savefig(File + '_loss1_aug.png') res_plot = np.array(grad_loss4) np.savetxt(File + '_loss4_MMD.txt', res_plot, fmt='%0.8f') # plt.plot([i for i in range(len(grad_loss4))], grad_loss4) plt.savefig(File + '_loss4_MMD.png') res_plot = np.array(grad_png) np.savetxt(File + '_loss_ACC.txt', res_plot, fmt='%0.8f') # plt.plot([i for i in range(len(grad_png))], grad_png) plt.savefig(File + '_loss_ACC.png') # save encoder and decoder if epoch % 10 == 0 and epoch != 0: torch.save(net1, File + 'encoder_' + str(epoch) + '.pth') torch.save(net2, File + 'decoder_' + str(epoch) + '.pth') # plt.show()batch100_lr0.001_aug5_MMD_0.001_5_version1.txt # write log f = open(File + 'log.txt', 'a') print(" ") print(epoch_str + time_str) print(Loss) print('Clustering ACC = %.4f,Clustering NMI = %.4f' % (Acc, NMI)) print("---------------") f.write(epoch_str + time_str + '\n') f.write(Loss + '\n') f.write("Acc : " + str(Acc) + ',' + ' NMI :' + str(NMI) + '\n') f.close()
def train_nolabel(net1, net2, train_data, epoch, optimizer_en, optimizer_de, criterion): if torch.cuda.is_available(): net1 = torch.nn.DataParallel(net1, device_ids=device_ids) net2 = torch.nn.DataParallel(net2, device_ids=device_ids) net1 = net1.cuda() net2 = net2.cuda() prev_time = datetime.now() train_loss = 0 train_loss1 = 0 train_loss2 = 0 train_loss3 = 0 train_loss4 = 0 train_loss5 = 0 net1 = net1.train() net2 = net2.train() map_dict = read_pkl(PKL) tensor_empty1 = torch.Tensor([]).cuda() for label_index in range(classnum): tensor_empty1 = torch.cat( (tensor_empty1, map_dict[label_index].float().cuda()), 0) for im, label in tqdm(train_data, desc="Processing train data: "): im11 = torch.Tensor([]) im12 = torch.Tensor([]) im13 = torch.Tensor([]) im14 = torch.Tensor([]) for i in range(im.size(0)): im11 = torch.cat( (im11, normalize(loader(Transformations1( (unloader(im[i]))))).unsqueeze(0)), 0) im12 = torch.cat( (im12, normalize(loader(Transformations2( (unloader(im[i]))))).unsqueeze(0)), 0) im13 = torch.cat( (im13, normalize(loader(Transformations3( (unloader(im[i]))))).unsqueeze(0)), 0) im14 = torch.cat( (im14, normalize(loader(Transformations4( (unloader(im[i]))))).unsqueeze(0)), 0) im = im.cuda() im11 = im11.cuda() im12 = im12.cuda() im13 = im13.cuda() im14 = im14.cuda() output_classifier11 = net1(im11) output_classifier12 = net1(im12) output_classifier13 = net1(im13) output_classifier14 = net1(im14) output_classifier = net1(im) output_deconvt = net2(output_classifier) loss4 = 5*(criterion(output_classifier13, output_classifier14) \ +criterion(output_classifier12, output_classifier13) \ +criterion(output_classifier12, output_classifier11) \ +criterion(output_classifier14, output_classifier)) sigma = generator_noise(output_classifier.size(0), output_classifier.size(1)) new_out = output_classifier + torch.from_numpy( sigma * 0.05 * (output_classifier.size(1)**0.5)).float().cuda() output_deconv = net2(new_out) loss2 = criterion(output_deconvt, im) loss3 = 0.05 * sobel_1vs1_1D(im, output_deconv, criterion) z_fake = torch.Tensor([]).cuda() z_real = torch.cat((output_classifier, output_classifier14), 0) z_real = torch.cat((z_real, output_classifier11), 0) z_real = torch.cat((z_real, output_classifier13), 0) z_real = torch.cat((z_real, output_classifier12), 0) batchsize = output_classifier.size(0) for b in range(5 * (batchsize // classnum)): z_fake = torch.cat((z_fake, tensor_empty1), 0) z_fake = torch.cat((z_fake, tensor_empty1[0:batchsize % classnum])) z_fake = torch.cat((z_fake, tensor_empty1[0:batchsize % classnum])) z_fake = torch.cat((z_fake, tensor_empty1[0:batchsize % classnum])) z_fake = torch.cat((z_fake, tensor_empty1[0:batchsize % classnum])) z_fake = torch.cat((z_fake, tensor_empty1[0:batchsize % classnum])) loss1 = 5 * mmd_rbf(z_real, z_fake) loss = loss1 + loss2 + loss3 + loss4 # backward optimizer_en.zero_grad() optimizer_de.zero_grad() loss.backward() optimizer_en.step() optimizer_de.step() train_loss += loss.item() train_loss1 += loss1.item() train_loss2 += loss2.item() train_loss3 += loss3.item() train_loss4 += loss4.item() curr_time = datetime.now() h, remainder = divmod((curr_time - prev_time).seconds, 3600) m, s = divmod(remainder, 60) time_str = " Time %02d:%02d:%02d" % (h, m, s) epoch_str = ("Epoch %d. " % (epoch)) Loss = ( "Train Loss1: %f, Train Loss2: %f,Train Loss3: %f,Train Loss4: %f,Train Loss5: %f," % (train_loss1 / len(train_data), train_loss2 / len(train_data), train_loss3 / len(train_data), train_loss4 / len(train_data), train_loss5 / len(train_data))) prev_time = curr_time if not os.path.isdir('./model/MNIST/60/DATA/'): os.makedirs('./model/MNIST/60/DATA/') if epoch % 10 == 0 and epoch != 0: torch.save(net1, './model/MNIST/60/DATA/encoder_' + str(epoch) + '.pth') torch.save(net2, './model/MNIST/60/DATA/decoder_' + str(epoch) + '.pth') f = open('./model/MNIST/60/DATA/en_de.txt', 'a+') print(" ") print(epoch_str + time_str) print(Loss + "---------------") f.write(epoch_str + time_str + '\n') f.write(Loss + '\n') f.close()
def train(): # 将源域与目标域数据放入迭代器 # src_iter = iter(src_loader) # tgt_iter = iter(tgt_train_loader) # 【思路】 src_train通过如下模型训练处src_pred 与 真实的 src_label(这个X值下的groundtruth,即要训练模型拿src_train做X, # src_label(视觉方法测出)做Y的)拿经过迁移后得到模型根据X输入推出的src_pred(比较接近于目标域的情况)与真实的值的loss作为一个loss。 # 源域和目标域X距离尽可能小 ---- loss1 # 源域经过迁移过的模型出的pred要和真实的label距离尽可能小 ---- loss2 # tgt的域得出的pred是更符合真实label情况的,所以要往tgt方向迁移但是不需要tgt的pred # 输入一个x返回output # tf_x = tf.placeholder(tf.float32, [None, 6], name="input_placeholder_x") # input x # tf_y = tf.placeholder(tf.float32, [None, 1], name="ouput_placeholder_y") # input y # neural network layers # 直接从temp_output得到其他层输出的tensor # l1 = tf.layers.dense(tf_x, 512, tf.nn.relu, name="l1") # hidden layer # l2 = tf.layers.dense(l1, 512, tf.nn.relu, name="l2") # hidden layer l3 = tf.layers.dense(temp_output, 512, tf.nn.relu, name="l3_new") # hidden layer l4 = tf.layers.dense(l3, 512, tf.nn.relu, name="l4_new") # hidden layer l5 = tf.layers.dense(l4, 512, tf.nn.relu, name="l5_new") # hidden layer l6 = tf.layers.dense(l5, 512, tf.nn.relu, name="l6_new") # hidden layer l7 = tf.layers.dense(l6, 512, tf.nn.relu, name="l7_new") # hidden layer l8 = tf.layers.dense(l7, 1, name="l8_new") # output = tf.layers.dense(l7, 1, name="outputtttttttt_node") # output layer Uui = tf.add(l8, l8) output = tf.subtract(Uui, l8, name="output_node") src_loss = tf.losses.mean_squared_error(tf_y, output) # compute cost Saver = tf.train.Saver() # control training and others sess = tf.compat.v1.Session() sess.run(tf.compat.v1.global_variables_initializer()) # initialize var in graph # tf.reset_default_graph() # 将在外部run运行出的经过网络厚的tgt传入,然后和src经过网络厚的值source和target二者做mmd距离 tf_mmd_loss = tf.placeholder(tf.float32, None, name="mmd_loss_placeholder") tf_lambd = tf.placeholder(tf.float32, None, name="lambd_placeholder") # loss = src_loss + tf_lambd * tf_mmd_loss loss = src_loss + tf_lambd * tf_mmd_loss # tf.reset_default_graph() # outputNodeName = 'output_node' for step in range(5000000): # train and net output lrt = lr / math.pow((1 + 10 * (step - 1) / (5000000)), 0.5) optimizer = tf.train.AdadeltaOptimizer(lrt) train_op = optimizer.minimize(loss) # 经过Use模型得到source与target if step == 0: target = sess.run(output, {tf_x: tgt_x_train, tf_y: tgt_y_train, tf_mmd_loss: 100, tf_lambd: 0}) _, loss_loss, source = sess.run([train_op, loss, output], {tf_x: src_x_train, tf_y: src_y_train, tf_mmd_loss: 100, tf_lambd: 0}) else: target = sess.run(output, {tf_x: tgt_x_train, tf_y: tgt_y_train, tf_mmd_loss: mmd_loss, tf_lambd: lambd}) _, loss_out, source = sess.run([train_op, loss, output], {tf_x: src_x_train, tf_y: src_y_train, tf_mmd_loss: mmd_loss, tf_lambd: lambd}) # loss mmd_loss = mmd.mmd_rbf(source, target) # print("mmd_loss:",mmd_loss) lambd = 2 / (1 + math.exp(-10 * (step) / 5000000)) - 1 # print("lambd:",lambd) # constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def, ["Input/x", "Input/y", "Output/predictions"]) if (step > 0): # print('loss is: ' + str(loss_out)) if (step % 5== 0): print("step: ", step, " ", "learning rate: ", lrt) pred1 = sess.run(output, {tf_x: tgt_x_test}) # pred1 = sess.run(output, feed_dict={tf_x: X_test3}) # (-1, 3) print('loss is: ' + str(loss_out)) # print('prediction is:' + str(pred)) k = 0 k1 = 0 for i in range(len(tgt_y_test)): jjji = pred1[i] yyyi = tgt_y_test[i] if (abs(yyyi[0] - jjji[0]) < 1): # print(Reverse(yyyi[0]),Reverse(jjji[0])) k += 1 if (abs(yyyi[0] - jjji[0]) < 5): # print(Reverse(yyyi[0]),Reverse(jjji[0])) k1 += 1 # break # if abs(yyyi-jjji) < 1: print("pred_loss: ", sess.run(tf.losses.mean_squared_error(pred1, tgt_y_test))) print("acc:", k1 / len(tgt_y_test)) print("------------------------------") if (step % 2000 == 0): lujing = '/media/zzg/29b7a8df-b813-4851-a749-f9db6d488d0d/zzg/Documents/model.ckpt' Saver.save(sess, lujing) print("successSave!")
# plt.savefig('1') plt.show() # DTW验证 # s1 = data[100,:,:].flatten() # s2 = data[50,:,:].flatten() # s3 = A[1,:,:].flatten() # # 原始算法 # distance12, paths12, max_sub12 = DTW.TimeSeriesSimilarityImprove(s1, s2) # distance13, paths13, max_sub13 = DTW.TimeSeriesSimilarityImprove(s1, s3) # # print("更新前s1和s2距离:" + str(distance12)) # print("更新前s1和s3距离:" + str(distance13)) # # # 衰减系数 # weight12 = DTW.calculate_attenuate_weight(len(s1), len(s2), max_sub12) # weight13 = DTW.calculate_attenuate_weight(len(s1), len(s3), max_sub13) # # # 更新距离 # print("更新后s1和s2距离:" + str(distance12 * weight12)) # print("更新后s1和s3距离:" + str(distance13 * weight13)) # MMD验证 s1 = torch.from_numpy(data[100, :, :]) s2 = torch.from_numpy(data[50, :, :]) s3 = torch.from_numpy(A[10, :, :]) dict1 = mmd.mmd_rbf(s1, s2) dict2 = mmd.mmd_rbf(s1, s3) print("s1和s2MMD距离:" + str(dict1)) print("s1和s3MMD距离:" + str(dict2))