File size: 6,511 Bytes
8ffcfd0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | import torch.nn as nn
from torch import optim
class endA(nn.Module):
def __init__(self, in_features, out_features):
super(endA, self).__init__()
self.encoder = nn.Sequential(
nn.Linear(in_features, out_features),
nn.BatchNorm1d(out_features),
nn.ReLU(True),
nn.Dropout(0.1)
) # 编码
#self.decoder[0].weight.data = self.encoder[0].weight.data.transpose(0, 1)
def forward(self, x):
h = self.encoder(x)
return h
class dedA(nn.Module):
def __init__(self, out_features, in_features):
super(dedA, self).__init__()
self.decoder = nn.Sequential(
nn.Linear(out_features, in_features),
nn.ReLU(True)
) # 编码
# self.decoder[0].weight.data = self.encoder[0].weight.data.transpose(0, 1)
def forward(self, x):
h = self.decoder(x)
return h
class SdA(nn.Module):
def __init__(self, config):
super(SdA, self).__init__()
layers1 = []
layers2 = []
layersall1=[]
layersall2 = []
in_features1 = config.input_features1
for out_features in config.enhidden_features:
layer1 = endA(in_features1, out_features)
in_features1 = out_features
layers1.append(layer1)
self.layers1 = nn.Sequential(*layers1) # 就是封装了成了一个
in_features=config.enhidden_features[-1]
for out_features in config.dehidden_features1:
layer2 = dedA(in_features, out_features)
in_features = out_features
layers2.append(layer2)
self.layers2=nn.Sequential(*layers2)
layersall1.append(self.layers1)
layersall1.append(self.layers2)
self.layerll1=nn.Sequential(*layersall1)
if config.is_train:
self.ce_criterion = nn.CrossEntropyLoss()
self.da_optimizers = []
for layer1 in self.layers1[:-1]:
# optimizer = optim.SGD(layer1.parameters(), lr=config.lr,
# momentum=config.momentum, weight_decay=config.weight_decay) # 优化器可以改一下
optimizer = optim.Adam(
layer1.parameters(), lr=0.001, betas=(0.9, 0.99), eps=1e-8, weight_decay=0)
self.da_optimizers.append(optimizer)
layers3 = []
layers4 = []
in_features2 = config.input_features2
for out_features in config.enhidden_features:
layer3 = endA(in_features2, out_features)
in_features2 = out_features
layers3.append(layer3)
self.layers3 = nn.Sequential(*layers3) # 就是封装了成了一个
in_features=config.enhidden_features[-1]
for out_features in config.dehidden_features2:
layer4 = dedA(in_features, out_features)
in_features = out_features
layers4.append(layer4)
self.layers4=nn.Sequential(*layers4)
layersall2.append(self.layers3)
layersall2.append(self.layers4)
self.layerll2 = nn.Sequential(*layersall2)
# for layer in self.layers3:
# print(layer)
if config.is_train:
self.ce_criterion = nn.CrossEntropyLoss()
self.da_optimizers = []
for layer1 in self.layers3[:-1]:
# optimizer = optim.SGD(layer1.parameters(), lr=config.lr,
# momentum=config.momentum, weight_decay=config.weight_decay) # 优化器可以改一下
optimizer=optim.Adam(layer1.parameters(),lr=0.001,betas=(0.9,0.99),eps=1e-8,weight_decay=0)
self.da_optimizers.append(optimizer)
# 每一层的优化器
def forward(self, x1, x2):
h1, h2 = x1, x2
for layer1 in self.layers1:
h1 = layer1(h1)
h3 = h1
for layer2 in self.layers2:
h3 = layer2(h3)
for layer3 in self.layers3:
h2 = layer3(h2)
h4=h2
for layer4 in self.layers4:
h4 = layer4(h4)
return h1, h2, h3, h4 # 不是很理解构
def regularization_loss(self):
l2_lambda = 0.001
l2_norm = sum(p.pow(2).sum() for p in self.parameters())
return l2_lambda * l2_norm
class Anchormodel(nn.Module):
def __init__(self,dim,outfeature):
super(Anchormodel, self).__init__()
self.encoder0 = nn.Sequential(
nn.Linear(dim, 1024),
nn.BatchNorm1d(1024),
nn.ReLU(True),
nn.Dropout(0.2),
nn.Linear(1024, 1024),
nn.BatchNorm1d(1024),
nn.ReLU(True),
nn.Dropout(0.2),
nn.Linear(1024, 1024),
nn.BatchNorm1d(1024),
nn.ReLU(True),
nn.Dropout(0.2),
nn.Linear(1024, outfeature),
nn.BatchNorm1d(outfeature),
nn.ReLU(True)
)
self.encoder1 = nn.Sequential(
nn.Linear(dim, 1024),
nn.BatchNorm1d(1024),
nn.ReLU(True),
nn.Dropout(0.2),
nn.Linear(1024, 1024),
nn.BatchNorm1d(1024),
nn.ReLU(True),
nn.Dropout(0.2),
nn.Linear(1024, 1024),
nn.BatchNorm1d(1024),
nn.ReLU(True),
nn.Dropout(0.2),
nn.Linear(1024, outfeature),
nn.BatchNorm1d(outfeature),
nn.ReLU(True)
)
# self.decoder0 = nn.Sequential(nn.Linear(outfeature, 1024), nn.ReLU(), nn.Dropout(0.2), nn.Linear(1024, 1024), nn.ReLU(),
# nn.Dropout(0.2), nn.Linear(1024, 1024), nn.ReLU(), nn.Dropout(0.2),
# nn.Linear(1024, dim))
# self.decoder1 = nn.Sequential(nn.Linear(outfeature, 1024), nn.ReLU(), nn.Dropout(0.2), nn.Linear(1024, 1024), nn.ReLU(),
# nn.Dropout(0.2), nn.Linear(1024, 1024), nn.ReLU(), nn.Dropout(0.2),
# nn.Linear(1024, dim))
def forward(self, x0, x1):
h0 = self.encoder0(x0.view(x0.size()[0], -1))
h1 = self.encoder1(x1.view(x1.size()[0], -1))
# z0 = self.decoder0(h0)
# z1 = self.decoder1(h1)
return h0, h1
|