Logo
Section 1 / 12

相似性最大化简介

了解基于相似性最大化(similarity maximization)的自监督学习。

导入

相似性最大化简介

了解基于相似性最大化(similarity maximization)的自监督学习。

...
学习不变表示的平凡解
1import torch 2import torch.nn as nn 3import matplotlib.pyplot as plt 4 5model = nn.Sequential(nn.Linear(5,3), # 模型定义 6 nn.ReLU(), 7 nn.Linear(3, 2), 8 nn.ReLU()) 9 10x = torch.FloatTensor([[1, 0, 0, 1, 1], # 人工输入数据 11 [0, 0, 1, 1, 1], 12 [1, 1, 1, 1, 1], 13 [1, 0, 1, 0, 1], 14 [1, 0, 0, 0, 1], 15 [0, 0, 1, 0, 0]]) 16 17t1_x = x + 0.3*torch.randn(6, 5) # 增强版本 1 18t2_x = x + 0.3*torch.randn(6, 5) # 增强版本 2 19 20optimizer = torch.optim.Adam(model.parameters()) # 优化器 21criterion = nn.CosineSimilarity(dim=-1) # 余弦相似度目标函数 22 23for epoch in range(10): 24 optimizer.zero_grad() 25 f1, f2 = model(t1_x), model(t2_x) # 提取特征 26 loss = -criterion(f1, f2).mean() # 最大化余弦相似度 27 loss.backward() 28 optimizer.step() # 更新模型参数 29 30 with torch.no_grad(): 31 f = model(x) 32 mean = f.mean(0) 33 var = f.var(0) 34 print("第 {} 轮特征均值\n".format(epoch), mean.numpy()) 35 print("第 {} 轮特征方差\n".format(epoch), var.numpy()) 36 print("=====================")
...
基于相似性最大化自监督学习的分类体系

预训练任务

对比学习:SimCLR 算法