def __init__(self, feature_extraction=False, p=0.5, norm=True, num_classes=10): super().__init__() self.embed = NetForEmbedding(feature_extraction) self.embed2 = NetForEmbedding(feature_extraction=True) #Extracting feature on the synthetic-data branches self.embed3 = NetForEmbedding(feature_extraction=True) self.l2dis = nn.PairwiseDistance() self.l2dis_neg = nn.PairwiseDistance() self.norm = norm self.drop = nn.Dropout(p=p) self.drop2d = nn.Dropout2d(p=0.2) self.classifier = nn.Sequential( nn.Linear(2048,512), nn.BatchNorm1d(512), nn.ReLU(), nn.Dropout(p=0.5), nn.Linear(512,128), nn.BatchNorm1d(128), nn.ReLU(), nn.Dropout(p=0.5), nn.Linear(128, num_classes) )
def test(): L1 = nn.PairwiseDistance(1) L2 = nn.PairwiseDistance(2) device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') print('Load pretrained model') net1 = VGG_Model().to(device) checkpoint = torch.load('E:/PycharmProjects/facedecoder/vgg_face_dag.pth', map_location='cpu') net1.load_state_dict(checkpoint) for para in net1.parameters(): para.requires_grad = False net1.eval() image1 = load_image(r'E:\PycharmProjects\result\f2f\f2f3.jpg') image2 = load_image( r'D:\BaiduNetdiskDownload\s2fTrueFace\s2fTrueFace\3.jpg') image1.unsqueeze_(0) image2.unsqueeze_(0) print(image1.shape) feat_7, _ = net1(image1) feat_71, _ = net1(image2) print('111111111') print(L1(feat_7, feat_71)) print(L2(feat_7, feat_71))
def selectModel_PairNet(params, point_net_last_dim=None, pair_feature_dim=None, pt=None): if params['modelName'].split("_")[1] == "l1": dist = nn.PairwiseDistance(p=1, keepdim=True) if params['modelName'].split("_")[1] == "l2": dist = nn.PairwiseDistance(p=2, keepdim=True) if params['modelName'].split("_")[1] == "cosine": cos_sim = nn.CosineSimilarity(dim=1, eps=1e-08) dist = lambda x1, x2: 1.0 - cos_sim(x1, x2).unsqueeze_(1) if params['modelName'].split("_")[1] == "bilinearDIAG": assert point_net_last_dim is not None dist = DiagonalBilinear(point_net_last_dim) if params['modelName'].split("_")[1] == "bilinearFULL": assert point_net_last_dim is not None dist = nn.Bilinear(point_net_last_dim, point_net_last_dim, 1, bias=False) if params['modelName'].split("_")[1] == "bilinearDIAG+PF": assert point_net_last_dim is not None and pair_feature_dim is not None dist = DiagonalBilinearWithPairFeature(point_net_last_dim, pair_feature_dim) if params['modelName'].split("_")[1] == "bilinearDIAG+PTF": assert point_net_last_dim is not None and pair_feature_dim is not None and pt is not None dist = DiagonalBilinearWithPairTransformFeature( point_net_last_dim, pair_feature_dim, pt) if params['modelName'].split("_")[1] == "PF": assert pair_feature_dim is not None dist = OnlyPairFeature(pair_feature_dim) return dist
def train(net2train, p_switch=0): device = torch.device('cpu') _ = net2train.train() _ = net2train.to(device) optim = torch.optim.Adam(net2train.parameters(), lr=lr) loss_collect = [] for i in range(train_iter): idxs = np.random.choice(len(train_lines), bs, replace=False) batch = torch.from_numpy(train_lines[idxs, :]).to( torch.float).to(device) train_labels = train_cls[idxs] embed = net2train(batch) unique_cls = np.unique(train_labels) indices = np.arange(len(batch)) class_dict = {i: indices[train_labels == i] for i in unique_cls} sampled_triplets = [ list(it.product([x], [x], [y for y in unique_cls if x != y])) for x in unique_cls ] sampled_triplets = [x for y in sampled_triplets for x in y] sampled_triplets = [[ x for x in list(it.product(*[class_dict[j] for j in i])) if x[0] != x[1] ] for i in sampled_triplets] sampled_triplets = [x for y in sampled_triplets for x in y] anchors = [triplet[0] for triplet in sampled_triplets] positives = [triplet[1] for triplet in sampled_triplets] negatives = [triplet[2] for triplet in sampled_triplets] if p_switch > 0: negatives = [ p if np.random.choice(2, p=[1 - p_switch, p_switch]) else n for n, p in zip(negatives, positives) ] neg_dists = torch.mean( F.relu(neg_margin - nn.PairwiseDistance( p=2)(embed[anchors, :], embed[negatives, :]))) loss = neg_dists else: pos_dists = torch.mean( F.relu( nn.PairwiseDistance(p=2)(embed[anchors, :], embed[positives, :]))) neg_dists = torch.mean( F.relu(neg_margin - nn.PairwiseDistance( p=2)(embed[anchors, :], embed[negatives, :]))) loss = pos_dists + neg_dists optim.zero_grad() loss.backward() optim.step() loss_collect.append(loss.item()) return loss_collect
def __init__(self, lambda_reconstruction=1): super(BaurLoss3D).__init__() self.lambda_reconstruction = lambda_reconstruction self.lambda_gdl = 0 self.l1_loss = lambda x, y: nn.PairwiseDistance(p=1)(x.view( x.shape[0], -1), y.view(y.shape[0], -1)).sum() self.l2_loss = lambda x, y: nn.PairwiseDistance(p=2)(x.view( x.shape[0], -1), y.view(y.shape[0], -1)).sum()
def __init__(self, model, dimension, batchSize, learningRate, dataOrganization, merge, similarityModel, similarityGold): super().__init__() if model == 1: projection_builder = lambda: nn.Linear(768, dimension) self.decoder = nn.Linear(dimension, 768) elif model == 2: projection_builder = lambda: nn.Sequential( nn.Linear(768, 1024), nn.Tanh(), nn.Linear(1024, 768), nn.Tanh(), nn.Linear(768, dimension), # This is optional. The final results are the same though the convergence is faster with this. nn.Tanh(), ) else: raise Exception("Unknown model specified") if merge: self.projection1 = projection_builder() self.projection2 = self.projection1 else: self.projection1 = projection_builder() self.projection2 = projection_builder() self.batchSize = batchSize self.dataOrganization = dataOrganization self.learningRate = learningRate self.similarityGold = similarityGold self.optimizer = torch.optim.Adam(self.parameters(), lr=self.learningRate) if similarityGold == "l2": self.similarityGold = lambda d1, d2, relevancy: nn.PairwiseDistance( p=2)(d1, d2) elif similarityGold == "ip": self.similarityGold = lambda d1, d2, relevancy: torch.mul( d1, d2).sum(dim=1) else: raise Exception("Unknown similarity gold") if similarityModel == "l2": self.similarityModel = lambda d1, d2: nn.PairwiseDistance(p=2)(d1, d2) elif similarityModel == "ip": self.similarityModel = lambda d1, d2: torch.mul(d1, d2).sum(dim=1) else: raise Exception("Unknown similarity model") self.loss = nn.MSELoss() self.to(DEVICE)
def forward(self, batch, labels, **kwargs): anchors, positives, negatives = self.batchminer(batch, labels) loss = [] for anchor, positive_set, negative_set in zip(anchors, positives, negatives): anchor, positive_set, negative_set = batch[anchor, :].view(1,-1), batch[positive_set, :].view(1,len(positive_set),-1), batch[negative_set, :].view(1,len(negative_set),-1) pos_term = torch.logsumexp(nn.PairwiseDistance(p=2)(anchor[:,:,None], positive_set.permute(0,2,1)), dim=1) neg_term = torch.logsumexp(self.margin - nn.PairwiseDistance(p=2)(anchor[:,:,None], negative_set.permute(0,2,1)), dim=1) loss.append(F.relu(pos_term + neg_term)) loss = torch.mean(torch.stack(loss)) + self.l2_weight*torch.mean(torch.norm(batch, p=2, dim=1)) return loss
def forward(self, batch, labels, **kwargs): sampled_triplets = self.batchminer(batch, labels) anchors = [triplet[0] for triplet in sampled_triplets] positives = [triplet[1] for triplet in sampled_triplets] negatives = [triplet[2] for triplet in sampled_triplets] pos_dists = torch.mean(F.relu(nn.PairwiseDistance(p=2)(batch[anchors,:], batch[positives,:]) - self.pos_margin)) neg_dists = torch.mean(F.relu(self.neg_margin - nn.PairwiseDistance(p=2)(batch[anchors,:], batch[negatives,:]))) loss = pos_dists + neg_dists return loss
def test(): # dataloader = Dataset('/home/yxy/s2f/data','/home/yxy/s2f/biplects','test',False) test_data = Data_ganerator('E:/PycharmProjects/facedecoder/test.txt') test_loader = DataLoader(test_data, batch_size=1, shuffle=True, drop_last=True, num_workers=2) device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') net1 = VGG_Model() net2 = Face_Decoder() print('Load pretrained model') net1 = VGG_Model().to(device) checkpoint = torch.load('E:/PycharmProjects/facedecoder/vgg_face_dag.pth', map_location='cpu') net1.load_state_dict(checkpoint) for para in net1.parameters(): para.requires_grad = False net1.eval() net2 = Face_Decoder().to(device) checkpoint2 = torch.load( 'E:/PycharmProjects/facedecoder/weight_epoch_68.pth', map_location='cpu') net2.load_state_dict(checkpoint2) for para in net2.parameters(): para.requires_grad = False net2.eval() L1 = nn.PairwiseDistance(1) L2 = nn.PairwiseDistance(2) print('start test...') with torch.no_grad(): for step, (face) in enumerate(test_loader): # print(face.shape) face = face.to(device) feat_7, _ = net1(face) output2 = net2(feat_7) #output2 = (output2 + 1 print(output2.shape) print('22222222', output2) image = output2.cpu().clone() print(image.shape) image = image.squeeze(0) image = unloader(image) image.save('E:/PycharmProjects/facedecoder/demo/%d.jpg' % (step + 1))
def __init__(self, way=5, shot=5, quiry=15, if_cuda=True, nchannel=64): super(ProtoNetwork, self).__init__() self.if_cuda = if_cuda self.nchannel = nchannel self.conv1 = nn.Conv2d(3, self.nchannel, 3, stride=1, padding=1) self.batchNorm1 = nn.BatchNorm2d(self.nchannel) self.conv2 = nn.Conv2d(self.nchannel, self.nchannel, 3, stride=1, padding=1) self.batchNorm2 = nn.BatchNorm2d(self.nchannel) self.conv3 = nn.Conv2d(self.nchannel, self.nchannel, 3, stride=1, padding=1) self.batchNorm3 = nn.BatchNorm2d(self.nchannel) self.conv4 = nn.Conv2d(self.nchannel, self.nchannel, 3, stride=1, padding=1) self.batchNorm4 = nn.BatchNorm2d(self.nchannel) self.softmax = nn.Softmax() self.way = way self.shot = shot self.quiry = quiry self.pdist = nn.PairwiseDistance(p=2)
def calculate_loss(self, predicted_embeds, type_indexes, granularity_ids): types_by_gran = self.filter_types_by_granularity(type_indexes, granularity_ids) type_lut_ids = [idx for row in types_by_gran for idx in row] index_on_prediction = self.get_index_on_prediction(types_by_gran) if len(type_lut_ids) == 0: return torch.zeros(1, requires_grad=True).to(self.device), torch.zeros(1).to(self.device), \ torch.zeros(1).to(self.device), torch.zeros(1).to(self.device) true_type_embeds = self.type_lut(torch.LongTensor(type_lut_ids).to(self.device)) # len_type_lut_ids x type_dims expanded_predicted = predicted_embeds[index_on_prediction] distances_to_pos = self.distance_function(expanded_predicted, true_type_embeds) metric_distance = distances_to_pos**2 if self.hyperbolic else distances_to_pos cosine_similarity = self.cos_sim_function(expanded_predicted, true_type_embeds) cosine_distance = 1 - cosine_similarity distance_sum = self.args.metric_dist_factor * metric_distance + self.args.cosine_dist_factor * cosine_distance y = torch.ones(len(distance_sum)).to(self.device) loss = self.hinge_loss_function(distance_sum, y) # stats avg_angle = torch.acos(torch.clamp(cosine_similarity.detach(), min=-1, max=1)) * 180 / pi euclid_dist = nn.PairwiseDistance()(expanded_predicted.detach(), true_type_embeds.detach()) return loss, avg_angle, distances_to_pos.detach(), euclid_dist
def __init__(self, args, vocabs): self.device = 'cuda' if torch.cuda.is_available() else 'cpu' self.args = args type_vocab = vocabs[Constants.TYPE_VOCAB] self.coarse_ids = type_vocab.get_coarse_ids() self.fine_ids = type_vocab.get_fine_ids() self.ultrafine_ids = type_vocab.get_ultrafine_ids() self.ids = [self.coarse_ids, self.fine_ids, self.ultrafine_ids] super(Model, self).__init__() self.word_lut = nn.Embedding(vocabs[Constants.TOKEN_VOCAB].size_of_word2vecs(), args.emb_size, padding_idx=Constants.PAD) self.type_lut = nn.Embedding(vocabs[Constants.TYPE_VOCAB].size(), args.type_dims) self.mention_encoder = MentionEncoder(vocabs[Constants.CHAR_VOCAB], args) self.context_encoder = ContextEncoder(args) self.feature_len = args.context_rnn_size * 2 + args.emb_size + args.char_emb_size self.coarse_projector = Projector(args, self.feature_len) self.fine_projector = Projector(args, self.feature_len + args.type_dims) self.ultrafine_projector = Projector(args, self.feature_len + args.type_dims) self.hyperbolic = args.metric == "hyperbolic" self.distance_function = PoincareDistance.apply if self.hyperbolic else nn.PairwiseDistance() self.cos_sim_function = nn.CosineSimilarity() self.hinge_loss_function = nn.HingeEmbeddingLoss()
def accuracy(target, output): """Computes the precision@k for distances of the landmarks""" dist_function = nn.PairwiseDistance(p=1) distances = dist_function(output, target) return distances.mean() / 2
def __init__(self, vocab_size, embedding_dim, hidden_dim, batch_size, wordvec_matrix, bidirectional): super(LSTM_angel, self).__init__() self.hidden_dim = hidden_dim self.bidirectional = bidirectional self.batch_size = batch_size self.dist = nn.PairwiseDistance(2) self.word_embedding = nn.Embedding(vocab_size, embedding_dim) self.word_embedding.weight.data.copy_(wordvec_matrix) self.word_embedding.weight.requires_grad = False self.lstm1 = nn.LSTM(embedding_dim, hidden_dim // 2 if bidirectional else hidden_dim, batch_first=True, bidirectional=bidirectional) self.lstm2 = nn.LSTM(embedding_dim, hidden_dim // 2 if bidirectional else hidden_dim, batch_first=True, bidirectional=bidirectional) self.linear1 = nn.Linear(3, 200) self.dropout1 = nn.Dropout(p=0.1) # self.batchnorm1 = nn.BatchNorm1d(200) self.linear2 = nn.Linear(200, 200) self.dropout2 = nn.Dropout(p=0.1) # self.batchnorm2 = nn.BatchNorm1d(200) self.linear3 = nn.Linear(200, 200) self.dropout3 = nn.Dropout(p=0.1) # self.batchnorm3 = nn.BatchNorm1d(200) self.linear4 = nn.Linear(200, 200) self.dropout4 = nn.Dropout(p=0.1) # self.batchnorm4 = nn.BatchNorm1d(200) self.linear5 = nn.Linear(200, 2)
def __init__(self, input_size, out_size=(10, 10), lr=0.3, sigma=None): ''' :param input_size: :param out_size: :param lr: :param sigma: ''' super(SOM, self).__init__() self.input_size = input_size self.out_size = out_size self.lr = lr if sigma is None: self.sigma = max(out_size) / 2 else: self.sigma = float(sigma) #rnd weights self.weight = nn.Parameter(torch.randn(input_size, out_size[0] * out_size[1]), requires_grad=False) #get locationS self.locations = nn.Parameter(torch.Tensor(list(self.get_map_index())), requires_grad=False) self.pdist_fn = nn.PairwiseDistance(p=2)
def forward(self, features): for feat in features: fgFeat = feat[0] bgFeat = feat[1] id = torch.flatten( torch.eye(fgFeat.shape[2]) ) # flatten generates a view (preserves sparsity of eye) invId = torch.flatten(torch.eye(fgFeat.shape[2])) == 0.0 simplex_1 = id - .5 simplex_2 = invId - .5 sumDist = torch.tensor([1], dtype=torch.long) for fgBn, bgBn in zip(fgFeat, bgFeat): # replicate identity vectorwise and features channelwise rep1 = nn.functional.pad(fgBn, (fgBn.shape[1], 0), mode='replicate') rep2 = nn.functional.pad(simplex_1, (fgBn.shape[0], 0, 0, 0), mode='replicate') rep3 = nn.functional.pad(fgBn, (bgBn.shape[1], 0), mode='replicate') rep4 = nn.functional.pad(simplex_2, (bgBn.shape[0], 0, 0, 0), mode='replicate') pdist = nn.PairwiseDistance(p=2) sumDist += self.weights[0] * torch.sum( nn.functional.softmin(pdist(rep1, rep2), dim=1)) sumDist -= self.weights[0] * torch.sum( nn.functional.softmax(pdist(rep1, rep4), dim=1)) sumDist += self.weights[1] * torch.sum( nn.functional.softmin(pdist(rep3, rep4), dim=1)) sumDist -= self.weights[1] * torch.sum( nn.functional.softmax(pdist(rep3, rep2), dim=1)) return sumDist
def predict(self, out, meeting_id): pdist = nn.PairwiseDistance(p=2) dist = pdist(out, self.embedding) values, indices = torch.sort(dist) if torch.cuda.is_available() == True: indices = indices.cpu() indice_list = list(indices.numpy()) indice = indice_list.index(self.meeting_id2index[meeting_id]) # print(meeting_id) # print(dist) # print(values) # print(indices) # print(indice) idx_x = self.idx2id[meeting_id] min_dist_indice = indices[0].item() min_meeting_id = self.index2meeting_id[min_dist_indice] idx_y = self.idx2id[min_meeting_id] # line = '%d-%d &[ ' % (int(idx_x/5), int(idx_x%5)) # for i in range(5): # dist_indice = indices[i].item() # meeting_id = self.index2meeting_id[dist_indice] # idx = self.idx2id[meeting_id] # line = line + '%d-%d, ' % (int(idx/5), int(idx%5)) # line = line[0:-2] + ']\\\\' # print(line) return indice, idx_x, idx_y
def __init__(self, input_size, out_size=(10, 10), lr=0.3, sigma=None): ''' :param input_size: :param out_size: :param lr: :param sigma: ''' super(SOM, self).__init__() self.input_size = input_size self.out_size = out_size self.lr = lr if sigma is None: self.sigma = max(out_size) / 2 #半径 else: self.sigma = float(sigma) #d,x*y self.weight = nn.Parameter(torch.randn(input_size, out_size[0] * out_size[1]), requires_grad=False) #input_dim,map_size print(f'self.weight.size=={self.weight.size()}') self.locations = nn.Parameter(torch.Tensor(list(self.get_map_index())), requires_grad=False) self.pdist_fn = nn.PairwiseDistance( p=2) #https://blog.csdn.net/mingtian715/article/details/51163986
def _init_embed(self, input): if self.is_vrp or self.is_orienteering or self.is_pctsp: if self.is_vrp: features = ('demand', ) elif self.is_orienteering: features = ('prize', ) else: assert self.is_pctsp features = ('deterministic_prize', 'penalty') return torch.cat( (self.init_embed_depot(input['depot'])[:, None, :], self.init_embed( torch.cat( (input['loc'], *(input[feat][:, :, None] for feat in features)), -1))), 1) # TSP with GCNEncoder elif self.encoder_class is GCNEncoder: batch_size, num_nodes, node_dim = input.shape input_temp = input[:, :, None].expand(batch_size, num_nodes, num_nodes, node_dim).reshape( (-1, node_dim)) # Batched Edge Distance matrix computation: https://github.com/pytorch/pytorch/issues/9406#issuecomment-472269698 edges = nn.PairwiseDistance(p=2, keepdim=False)( input_temp, input_temp).reshape( (batch_size, num_nodes, num_nodes)) return self.init_embed(input), self.init_edge_embed( edges.unsqueeze(3)) # TSP with GraphAttentionEncoder/MLPEncoder return self.init_embed(input)
def __init__(self, args, n_user, n_entity, n_relation, link_path): super(KTRN, self).__init__() self._parse_args(args, n_user, n_entity, n_relation, link_path) self.user_embeddings = nn.Embedding(self.n_user, self.embedding_size) self.entity_embeddings = nn.Embedding(self.n_entity, self.embedding_size) self.relation_embeddings = nn.Embedding(self.n_relation, self.embedding_size) self.relationHyper = nn.Embedding(self.n_relation, self.embedding_size) self.distfn = nn.PairwiseDistance(2) # self.rnn = nn.RNN() self.rnns = [] for _ in range(self.n_iter): self.rnns.append( nn.LSTM(input_size=2 * self.embedding_size, batch_first=True, hidden_size=self.embedding_size, num_layers=1)) # self.out = nn.Linear(32, 1) self.out = nn.Linear(self.embedding_size, 1) self.agg_sum = nn.Linear(self.embedding_size, self.embedding_size) self.agg_con = nn.Linear(self.embedding_size * 2, self.embedding_size) for i in range(len(self.rnns)): self.rnns[i] = to_gpu(self.rnns[i]) self.user_embeddings = to_gpu(self.user_embeddings) self.entity_embeddings = to_gpu(self.entity_embeddings) self.relation_embeddings = to_gpu(self.relation_embeddings) self.relationHyper = to_gpu(self.relationHyper) self.out = to_gpu(self.out) self.agg_sum = to_gpu(self.agg_sum) self.agg_con = to_gpu(self.agg_con)
def get_closest_word(cbow, word, word_to_idx, unique_vocab): ''' Returns 5 closest neighbours( determined by cosine similarity) for the input word. Returns a list of 5 closest neighbours and their distansce from the word ''' word_distance = [] topn = 5 emb = cbow.embeddings pdist = nn.PairwiseDistance() pdist = nn.CosineSimilarity(dim=1, eps=1e-6) i = word_to_idx[word] # use cuda support if available and send the tensors to the correct device lookup_tensor_i = tensor_to_cuda(torch.tensor([i], dtype=torch.long)) v_i = emb(lookup_tensor_i).view(1, -1) index_to_word = {idx: w for (idx, w) in enumerate(unique_vocab)} for j in range(len(unique_vocab)): if j != i: # use cuda support if available and send the tensors to the correct device lookup_tensor_j = tensor_to_cuda( torch.tensor([j], dtype=torch.long)) v_j = emb(lookup_tensor_j) word_distance.append((index_to_word[j], float(abs(pdist(v_i, v_j))))) word_distance.sort(key=lambda x: x[1]) return word_distance[:topn]
def __init__(self, vocab_size, opt, pretrained_embeddings=None, is_train=True): super(AnswerSelection, self).__init__() self.opt = opt self.is_train = is_train self.encoder_a = self.encoder_b = self.encoder_c = LSTMEncoder( vocab_size, self.opt, is_train) if pretrained_embeddings is not None: self.encoder_a.embedding_table.weight.data.copy_( pretrained_embeddings) self.encoder_b.embedding_table.weight.data.copy_( pretrained_embeddings) self.encoder_c.embedding_table.weight.data.copy_( pretrained_embeddings) self.initialize_parameters() self.loss_function = nn.MarginRankingLoss(margin=0.5) self.optimizer_a = optim.Adam(self.encoder_a.parameters(), lr=self.opt.learning_rate, betas=(self.opt.beta_1, 0.999)) self.optimizer_b = optim.Adam(self.encoder_b.parameters(), lr=self.opt.learning_rate, betas=(self.opt.beta_1, 0.999)) self.optimizer_c = optim.Adam(self.encoder_c.parameters(), lr=self.opt.learning_rate, betas=(self.opt.beta_1, 0.999)) self.distance = nn.PairwiseDistance(1)
def __init__(self, vocab_size, embedding_dim, hidden_dim, batch_size,wordvec_matrix, bidirectional): super(wide_deep, self).__init__() self.hidden_dim = hidden_dim self.bidirectional = bidirectional self.batch_size = batch_size self.dist = nn.PairwiseDistance(2) self.word_embedding = nn.Embedding(vocab_size, embedding_dim) self.word_embedding.weight.data.copy_(wordvec_matrix) self.word_embedding.weight.requires_grad = False self.lstm1 = nn.LSTM(embedding_dim, hidden_dim//2 if bidirectional else hidden_dim, batch_first=True, bidirectional=bidirectional) self.lstm2 = nn.LSTM(embedding_dim, hidden_dim//2 if bidirectional else hidden_dim, batch_first=True, bidirectional=bidirectional) self.mp = nn.MaxPool1d(hidden_dim, stride=1) self.deep1 = nn.Linear(2 * hidden_dim, 300) self.dropout1 = nn.Dropout(p=0.1) self.deep2 = nn.Linear(300, 300) self.dropout2 = nn.Dropout(p=0.1) self.deep3 = nn.Linear(300, 200) self.dropout3 = nn.Dropout(p=0.1) self.deep4 = nn.Linear(200, 100) self.dropout4 = nn.Dropout(p=0.1) self.deep5 = nn.Linear(100, 16) self.dropout5 = nn.Dropout(p=0.1) self.merge_layer = nn.Linear(16+5, 2)
def __init__(self): super(contrastiveVAE, self).__init__() # qs self.fc1 = nn.Linear(X_dim, h1_dim) self.fc2 = nn.Linear(h1_dim, h2_dim) self.fc3 = nn.Linear(h2_dim, S_dim[0]) # qz self.fc4 = nn.Linear(X_dim, h1_dim) self.fc5 = nn.Linear(h1_dim, h2_dim) self.fc6m = nn.Linear(h2_dim, Z_dim) self.fc6v = nn.Linear(h2_dim, Z_dim) # pz self.fc7 = nn.Linear(Z_dim, h2_dim) self.fc8 = nn.Linear(h2_dim, h1_dim) self.fc9 = nn.Linear(h1_dim, X_dim) # for getting Contrastive loss self.distance = nn.PairwiseDistance(2) # utils self.relu = nn.ReLU() self.sigmoid = nn.Sigmoid() self.softmax = nn.Softmax() self.batchNorm1 = nn.BatchNorm1d(h1_dim) self.batchNorm2 = nn.BatchNorm1d(h2_dim) # params self.Z_dim = Z_dim
def __init__(self, vocab_size, device, word_matrix=None, embedding_dim=300, hidden_dim=300): super(Siamese_LSTM, self).__init__() self.device = device self.hidden_dim = hidden_dim self.word_embedding = nn.Embedding(vocab_size, embedding_dim) if word_matrix is not None: word_matrix = torch.tensor(word_matrix).to(self.device) self.word_embedding.weight.data.copy_(word_matrix) self.word_embedding.weight.requires_grad = False self.lstm = nn.LSTM(embedding_dim, hidden_dim // 2, batch_first=True, bidirectional=True) self.linear1 = nn.Linear(3, 100) self.linear2 = nn.Linear(100, 100) self.linear3 = nn.Linear(100, 1) self.dropout = nn.Dropout(p=0.1) self.dist = nn.PairwiseDistance(2)
def __init__(self, config): t_start = time.time() # parameters config['git_commit_version'] = str( subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).strip()) self.config = config self.experiment_id = self.config["train"]["experiment_id"] # self.submaps = submap_handler.SubmapHandler(self.config) t_sm = time.time() self.submaps = submap_handler.SubMapParser(config) print(f'Loaded Submaps ({time.time()-t_sm}s') self.max_nr_pts = self.config["train"]["max_nr_pts"] self.device = torch.device( "cuda:0" if torch.cuda.is_available() else "cpu") ### Load Encoder and Decoder #### t_model = time.time() self.encoder_model = None self.decoder_model = None self.getModel(config) print(f'Loaded Model ({time.time()-t_model}s)') ################################## ########## Loss Attributes ####### ################################## self.cham_loss = chamfer3D.dist_chamfer_3D.chamfer_3DDist() self.pairwise_dist = nn.PairwiseDistance() self.l2_loss = nn.MSELoss(reduction='mean') self.w_transf2map = self.config['train']['loss_weights']['transf2map'] self.w_map2transf = self.config['train']['loss_weights']['map2transf'] print(f'Init Trainer ({time.time()-t_start}s)')
def forward(self, x): # go through (1. Convolution Layer), and get activation by relu (2. ReLU) x = F.relu(self.conv1(x)) # go through (3. Batch Normalization Layer), and use (4. Max Pooling Layer) to filter x x = F.max_pool2d(self.BN1(x), (2, 2)) # go through (5. Convolution Layer), and get activation by relu (6. ReLU) x = F.relu(self.conv2(x)) # go through (7. Batch Normalization Layer), and use (8. Max Pooling Layer) to filter x x = F.max_pool2d(self.BN2(x), (2, 2)) # go through (9. Convolution Layer), and get activation by relu (10. ReLU) x = F.relu(self.conv3(x)) # go through (11. Batch Normalization Layer), and use (12. Max Pooling Layer) to filter x x = F.max_pool2d(self.BN3(x), (2, 2)) # go through (13. Convolution Layer), and get activation by relu (14. ReLU) x = F.relu(self.conv4(x)) # go through (15. Batch Normalization Layer) x = self.BN4(x) # go through (16. Flatten Layer) x = x.view(-1, self.num_flat_features(x)) # go through (17. fully connected layer), and et activation by relu (18. ReLU) x = F.relu(self.fc1(x)) x1, x2 = torch.chunk(x, 2, 0) distance = nn.PairwiseDistance(p=2) x = distance(x1, x2) return x
def __init__(self, emb_dim, device, BCE_mode, mode='all', dropout_p=0.3): super(Hidden_Layer, self).__init__() self.emb_dim = emb_dim self.mode = mode self.device = device self.BCE_mode = BCE_mode self.Linear1 = nn.Linear(self.emb_dim * 2, self.emb_dim).to(self.device) self.Linear2 = nn.Linear(self.emb_dim, 32).to(self.device) x_dim = 1 self.Linear3 = nn.Linear(32, x_dim).to(self.device) if self.mode == 'all': if self.BCE_mode: self.linear_output = nn.Linear(x_dim + 3, 1).to(self.device) else: self.linear_output = nn.Linear(x_dim + 3, 2).to(self.device) else: self.linear_output = nn.Linear(1, 2).to(self.device) self.linear_output.weight.data[1, :] = 1 self.linear_output.weight.data[0, :] = -1 self.cos = nn.CosineSimilarity(dim=1, eps=1e-6) self.pdist = nn.PairwiseDistance(p=2, keepdim=True) self.softmax = nn.Softmax(dim=1) self.elu = nn.ELU() assert (self.mode in ['all', 'cos', 'dot', 'pdist']), "Wrong mode type"
def triplet_loss(ref, pos, neg, min_dist_neg=1): """ Parameters ---------- ref: N,D pos: N,D neg: N,D min_dist_neg Returns ------- """ func_dist = nn.PairwiseDistance(p=2) ref = f.normalize(ref, p=2) # default dim=1, so works for both 2d or 1d tensor pos = f.normalize(pos, p=2) neg = f.normalize(neg, p=2) pos_distance = func_dist(ref, pos) neg_distance = func_dist(ref, neg) distance = pos_distance - neg_distance + min_dist_neg loss = torch.mean(torch.max(distance, torch.zeros_like(distance))) return loss
def __init__(self, embedding_dim=384, hidden_dim=128): super().__init__() self.text_embedder = Embedder(embedding_dim, hidden_dim) self.title_embedder = Embedder(embedding_dim, hidden_dim) self.distance = nn.PairwiseDistance(p=2) self.margin = 0.3