Esempio n. 1
0
def read_site_level_correlation(model, X, y_true, 
                                lambda_1=1, lambda_2=1, lambda_3=1,
                                mode='prod_pooling'):
    read_level_probability, site_level_probability, read_representation = model.get_read_site_probability(X)
    
    # Calculating site probability through pooling of read level probability and classifier

    if mode == 'prod_pooling':
        pooled_read_probability = 1 - torch.prod(1 - read_level_probability, axis=1)
    elif mode == 'mean_pooling':
        pooled_read_probability = torch.mean(read_level_probability, axis=1)
    elif mode == 'max_pooling':
        pooled_read_probability = torch.max(read_level_probability, axis=1).values

    site_level_loss = BCELoss()(site_level_probability.flatten(), y_true.float())
    read_level_loss = BCELoss()(pooled_read_probability, y_true.float())

    # Calculate correlation between site level probabiltiy and read level
    
    device = pooled_read_probability.device

    vx = site_level_probability - torch.mean(site_level_probability.flatten())
    vy = pooled_read_probability - torch.mean(pooled_read_probability)
    eps = torch.Tensor([1e-6]).to(device)
    pcc = torch.sum(vx * vy) / (torch.sqrt(torch.sum(vx ** 2) + eps) * torch.sqrt(torch.sum(vy ** 2) + eps))

    pcc_loss = 1 - pcc

    loss =  lambda_1 * site_level_loss + lambda_2 * read_level_loss + lambda_3 * pcc_loss
    return {'y_pred': site_level_probability, 'loss': loss, 'y_pred_read': pooled_read_probability,
            'site_loss': site_level_loss, 'read_loss': read_level_loss, 'pcc_loss': pcc_loss}
Esempio n. 2
0
    def __init__(self, hparams, fold):
        super().__init__()

        # Save hparams for later
        self.hparams = hparams
        self.fold = fold

        if self.hparams.dataset == "US8K":
            self.dataset_folder = config.path_to_UrbanSound8K
            self.nb_classes = 10
            self.best_scores = [0] * 5
        elif self.hparams.dataset == "ESC50":
            self.dataset_folder = config.path_to_ESC50
            self.nb_classes = 50
            self.best_scores = [0] * 5
        elif self.hparams.dataset == "SONYCUST":
            self.dataset_folder = config.path_to_SONYCUST
            self.nb_classes = 31
            self.best_scores = [0] * 10
        else:
            None

        #
        # Settings for the SED models
        model_param = {"classes_num": self.nb_classes}

        self.model = Cnn10(**model_param)
        if self.hparams.dataset != "SONYCUST":
            self.loss = BCELoss(reduction="none")
        else:
            self.loss_c = BCELoss(reduction="none")
            self.loss_f = Masked_loss(BCELoss(reduction="none"))
        self.mixup_augmenter = Mixup(self.hparams.alpha, self.hparams.seed)
    def __init__(self, hparams):
        super().__init__()
        # Save hparams for later
        self.hparams = hparams
        self.path_to_talnet = config.audioset
        self.num_classes_dict = {'coarse': 8, 'fine': 23, 'both': 31}
        #hack
        self.data_prepared = False
        self.prepare_data()
        num_meta = len(self.dataset[0]['metadata'])

        # Model parameters
        model_param = dict(
            (k, self.hparams.__dict__[k])
            for k in ("dropout", "pooling", "n_conv_layers", "kernel_size",
                      "n_pool_layers", "embedding_size", "batch_norm"))
        self.model = TALNetV3(
            self.hparams,
            num_mels=64,
            num_meta=num_meta,
            num_classes=self.num_classes_dict[self.hparams.output_mode])

        # Load every pretrained layers matching our layers
        pretrained_dict = torch.load(self.path_to_talnet,
                                     map_location='cpu')['model']
        model_dict = self.model.state_dict()
        pretrained_dict = {
            k: v
            for k, v in pretrained_dict.items()
            if k in model_dict and v.size() == model_dict[k].size()
        }
        model_dict.update(pretrained_dict)
        self.model.load_state_dict(model_dict)
        self.loss_c = BCELoss(reduction='none')
        self.loss_f = Masked_loss(BCELoss(reduction='none'))
Esempio n. 4
0
 def _define_loss(self):
     loss_functions = {
         "Generator": BCELoss(),
         "Adversary": BCELoss(),
         "L1": L1Loss()
     }
     return loss_functions
Esempio n. 5
0
    def __init__(self, hparams):
        super().__init__()
        # Save hparams for later
        self.hparams = hparams

        self.num_classes_dict = {'coarse': 8, 'fine': 23, 'both': 31}
        # To avoid calling prepare_data 2 times
        self.data_prepared = False
        self.prepare_data()
        num_meta = len(self.dataset[0]['metadata'])

        # Model parameters
        model_param = {
            'n_conv_layers': hparams.n_conv_layers,
            'kernel_size': (3, 3),
            'n_pool_layers': hparams.n_pool_layers,
            'embedding_size': hparams.embedding_size,
            'norm': 'GN',
            'conv_pool_strat': 'max',
            'conv_activation': hparams.conv_activation,
            'pooling': hparams.pooling,
            'dropout': hparams.dropout,
            'n_head': hparams.n_head,
            'd_kv': hparams.d_kv,
            'dropout_transfo': hparams.dropout_transfo,
            'num_meta': num_meta,
            'meta_emb': hparams.meta_embedding_size,
            'num_mels': hparams.num_mels,
            'num_classes': self.num_classes_dict[self.hparams.output_mode]
        }
        self.model = TALNetV2_meta(**model_param)
        self.loss_c = BCELoss(reduction='none')
        self.loss_f = Masked_loss(BCELoss(reduction='none'))
Esempio n. 6
0
def weighted_binary_cross_entropy_loss(model, X, y_true):
    y_pred = model(X)
    _, counts = torch.unique(y_true, return_counts=True)
    pos_weight, neg_weight = counts
    sample_weights = torch.where(y_true == 0, neg_weight, pos_weight)
    loss = BCELoss(reduce=False)(y_pred, y_true.float()) * sample_weights
    loss = loss.mean()
    return create_train_results(y_pred, loss)
Esempio n. 7
0
 def _define_loss(self):
     loss_functions = {
         "Generator": BCELoss(),
         "Adversary": BCELoss(),
         "Discrete": CrossEntropyLoss(),
         "Continuous": NormalNegativeLogLikelihood()
     }
     return loss_functions
Esempio n. 8
0
 def _define_loss(self):
     if self.adv_type == "Discriminator":
         loss_functions = {"Generator": BCELoss(), "Adversary": BCELoss()}
     elif self.adv_type == "Critic":
         loss_functions = {"Generator": WassersteinLoss(), "Adversary": WassersteinLoss()}
     else:
         raise NotImplementedError("'adv_type' must be one of Discriminator or Critic.")
     return loss_functions
Esempio n. 9
0
def main():
    args = parse_training_args("SRGAN")
    epochs = args.epochs
    load_path = args.load
    init_path = args.init
    out_path = args.out
    cuda = args.cuda
    device = torch.device(
        'cuda' if torch.cuda.is_available() and cuda else 'cpu')

    g_net = Generator().to(device)
    g_criterion = PerceptualLoss(feature_extractor=TruncatedVgg(),
                                 content_criterion=MSELoss(),
                                 adversarial_criterion=BCELoss()).to(device)
    g_optimizer = Adam(params=filter(lambda p: p.requires_grad,
                                     g_net.parameters()),
                       lr=1e-4)
    g_scheduler = ReduceLROnPlateau(optimizer=g_optimizer,
                                    factor=0.5,
                                    patience=3,
                                    verbose=True)

    d_net = Discriminator().to(device)
    d_criterion = DiscriminatorLoss(criterion=BCELoss()).to(device)
    d_optimizer = Adam(params=filter(lambda p: p.requires_grad,
                                     d_net.parameters()),
                       lr=1e-4)
    d_scheduler = ReduceLROnPlateau(optimizer=d_optimizer,
                                    factor=0.5,
                                    patience=3,
                                    verbose=True)

    converter = Converter()
    dataset = ImageNetDataset(json_path='data/train.json', converter=converter)
    data_loader = DataLoader(dataset=dataset,
                             batch_size=8,
                             num_workers=4,
                             pin_memory=True,
                             shuffle=True)

    trainer = GANTrainer(g_net=g_net,
                         g_criterion=g_criterion,
                         g_optimizer=g_optimizer,
                         g_scheduler=g_scheduler,
                         d_net=d_net,
                         d_criterion=d_criterion,
                         d_optimizer=d_optimizer,
                         d_scheduler=d_scheduler,
                         data_loader=data_loader,
                         device=device)

    if init_path:
        trainer.load_pretrained_generator(init_path)

    if load_path:
        trainer.load(load_path)

    trainer.train(max_epochs=epochs, save_path=out_path)
Esempio n. 10
0
    def __init__(self, anchors, num_classes, img_dim):
        super(YOLOLayer, self).__init__()
        self.anchors = anchors
        self.num_anchors = len(anchors)
        self.num_classes = num_classes
        self.bbox_attrs = 5 + num_classes
        self.img_dim = img_dim
        self.ignore_thres = 0.5
        self.lambda_coord = 1

        self.mse_loss = MSELoss()
        self.bce_loss = BCELoss()
Esempio n. 11
0
def _get_loss(F, model, batch, forward_barch_fun, **kws):
    device = torch.device(F.device)
    y = batch['label'].to(device)
    y_ = forward_barch_fun(F, model, batch, **kws)
    loss = BCELoss()(y_, y)
    sc = {}
    sc['loss'] = loss.item()
    sc['auc'] = np.mean([
        calc_auc(y_.cpu().detach().numpy()[:, i],
                 y.cpu().detach().numpy()[:, i]) for i in range(y.shape[1])
    ])
    sc['score'] = scoring(sc['loss'])
    return loss, sc
Esempio n. 12
0
    def loss(self, input_x, output_x, lat_mean: torch.Tensor,
             lat_logvar: torch.Tensor) -> (Variable, Variable, Variable):

        real_cred_id, \
        real_debt_id, \
        real_log_balance, \
        real_perc_withdrawed, \
        real_trans_types, \
        real_trans_ops, \
        real_trans_descs, \
        real_bank_code_debtor = self.cut_emb_data(input_x)

        gen_cred_id, \
        gen_debt_id, \
        gen_log_balance, \
        gen_perc_withdrawed, \
        gen_trans_types, \
        gen_trans_ops, \
        gen_trans_descs, \
        gen_bank_code_debtor = output_x

        loss_cred_id = MSELoss()(real_cred_id, gen_cred_id)
        loss_debt_id = MSELoss()(real_debt_id, gen_debt_id)
        loss_log_balance = MSELoss()(real_log_balance, gen_log_balance)
        loss_perc_withdrawed = MSELoss()(real_perc_withdrawed,
                                         gen_perc_withdrawed)
        loss_trans_types = BCELoss()(gen_trans_types,
                                     real_trans_types.detach())
        loss_trans_ops = BCELoss()(gen_trans_ops, real_trans_ops.detach())
        loss_trans_descs = BCELoss()(gen_trans_descs,
                                     real_trans_descs.detach())
        loss_bank_code_debtor = BCELoss()(gen_bank_code_debtor,
                                          real_bank_code_debtor.detach())

        recon_loss = 0*loss_cred_id + \
                     0*loss_debt_id + \
                     loss_log_balance + \
                     loss_perc_withdrawed + \
                     loss_trans_types + \
                     loss_trans_ops + \
                     loss_trans_descs + \
                     loss_bank_code_debtor

        kld_loss = torch.mean(-0.5 * torch.sum(
            1 + lat_logvar - lat_mean.pow(2) - lat_logvar.exp(), dim=1),
                              dim=0)

        return recon_loss + kld_loss * 5e-3, recon_loss, kld_loss
    def __init__(self, hparams, fold):
        super().__init__()

        # Save hparams for later
        self.hparams = hparams
        self.fold = fold

        if self.hparams.dataset == "US8K":
            self.dataset_folder = config.path_to_UrbanSound8K
            self.nb_classes = 10
            self.input_size = (162, 64)
            self.best_scores = [0] * 5
        elif self.hparams.dataset == "ESC50":
            self.dataset_folder = config.path_to_ESC50
            self.nb_classes = 50
            self.input_size = (200, 64)
            self.best_scores = [0] * 5
        elif self.hparams.dataset == "SONYCUST":
            self.dataset_folder = config.path_to_SONYCUST
            self.nb_classes = 31
            self.input_size = (400, 64)
            self.best_scores = [0] * 10
        else:
            None

        model_param = {
            "num_mels": hparams.num_mels,
            "num_classes": self.nb_classes,
        }
        self.model = TALNetV3NoMeta(hparams.__dict__, **model_param)

        # Load every pretrained layers matching our layers
        pretrained_dict = torch.load(config.audioset,
                                     map_location="cpu")["model"]
        model_dict = self.model.state_dict()
        pretrained_dict = {
            k: v
            for k, v in pretrained_dict.items()
            if k in model_dict and v.size() == model_dict[k].size()
        }
        model_dict.update(pretrained_dict)
        self.model.load_state_dict(model_dict)

        if self.hparams.dataset != "SONYCUST":
            self.loss = BCELoss(reduction="none")
        else:
            self.loss_c = BCELoss(reduction="none")
            self.loss_f = Masked_loss(BCELoss(reduction="none"))
Esempio n. 14
0
 def deep_learning_setup(self):
     """
     Sets up the optimizer and loss criterion
     """
     if self.optimizer_type == 'Adam':
         self.optimizer = Adam(filter(lambda p: p.requires_grad,
                                      self.detector.parameters()),
                               lr=self.lr)
     elif self.optimizer_type == 'RMSprop':
         self.optimizer = RMSprop(filter(lambda p: p.requires_grad,
                                         self.detector.parameters()),
                                  lr=self.lr)
     elif self.optimizer_type == 'SGD':
         self.optimizer = SGD(filter(lambda p: p.requires_grad,
                                     self.detector.parameters()),
                              lr=self.lr)
     else:
         raise NotImplementedError("No such optimizer implemented!!!")
     if self.loss_type == 'BCE':
         self.criterion = BCELoss().to(device)
     elif self.loss_type == 'CrossEntropy':
         self.criterion = CrossEntropyLoss().to(device)
     elif self.loss_type == 'MSE':
         self.criterion = MSELoss().to(device)
     else:
         raise NotImplementedError("No such loss function implemented!!!")
Esempio n. 15
0
    def __init__(self, model, lr, weight_decay):

        super(trainDeepPix, self).__init__()

        self.model = model
        self.lossC = BCELoss()
        self.lossS = BCELoss()

        if CUDA:
            self.model = self.model.cuda()
            self.lossC = self.lossC.cuda()
            self.lossS = self.lossS.cuda()

        self.optimizer = Adam(self.model.parameters(),
                              lr=lr,
                              weight_decay=weight_decay)
def ordinalLoss(y_pred, y_true, n, padded_value_indicator=PADDED_Y_VALUE):
    """
    Ordinal loss.
    :param y_pred: predictions from the model, shape [batch_size, slate_length, n]
    :param y_true: ground truth labels, shape [batch_size, slate_length]
    :param n: number of ordinal values, int
    :param padded_value_indicator: an indicator of the y_true index containing a padded item, e.g. -1
    :return: loss value, a torch.Tensor
    """
    # device = get_torch_device()

    y_pred = y_pred.clone()
    y_true = with_ordinals(y_true.clone(), n)

    mask = y_true == padded_value_indicator
    valid_mask = y_true != padded_value_indicator

    ls = BCELoss(reduction='none')(y_pred, y_true)
    ls[mask] = 0.0

    document_loss = torch.sum(ls, dim=2)
    # sum_valid = torch.sum(valid_mask, dim=2).type(torch.float32) > torch.tensor(0.0, dtype=torch.float32, device=device)
    sum_valid = torch.sum(valid_mask, dim=2).type(
        torch.float32) > torch.tensor(0.0, dtype=torch.float32)

    loss_output = torch.sum(document_loss) / torch.sum(sum_valid)

    return loss_output
Esempio n. 17
0
    def forward_sub(self, sequence_output, attention_mask, sub_labels,
                    batch_size):

        logits = self.classifier(sequence_output)
        outputs = (logits,
                   )  # add hidden states and attention if they are here
        #  logger.warning(f"logits.shape: {logits.shape}")
        loss_fct = BCELoss(reduction='none')
        # loss_fct = BCEWithLogitsLoss(reduction='none')
        loss_sig = nn.Sigmoid()
        # Only keep active parts of the loss
        #  active_logits = logits.view(-1, self.num_labels)
        active_logits = logits.view(-1, 2)
        active_logits = loss_sig(active_logits)
        active_logits = active_logits**2
        #  logger.warning(f"active_logits.shape: {active_logits.shape}")
        if sub_labels is not None:
            #  active_labels = labels.view(-1, self.num_labels).float()
            active_labels = sub_labels.view(-1, 2).float()
            #  logger.warning(
            #      f"active_logits.shape: {active_logits.shape}, active_labels.shape: {active_labels.shape}"
            #  )
            loss = loss_fct(active_logits, active_labels)
            # loss = loss.view(-1,sequence_output.size()[1],2)
            loss = loss.view(batch_size, -1, 2)
            loss = torch.mean(loss, 2)
            loss = torch.sum(attention_mask * loss) / torch.sum(attention_mask)
            outputs = (loss, ) + outputs
        else:
            outputs = (torch.tensor(0.0), active_logits)
            #  outputs = (torch.tensor(0.0), logits)

        return outputs
Esempio n. 18
0
def site_level_entropy_cluster(model, X, y_true, 
                               lambda_1=1, lambda_2=1,
                                mode='prod_pooling'):
    read_level_probability, site_level_probability, read_representation = model.get_read_site_probability(X)
    
    # Calculating site probability through pooling of read level probability and classifier

    site_level_loss = BCELoss()(site_level_probability, y_true.float())

    # Calculate loss based on the compactness of reads with similar probability score
    
    with torch.no_grad():
        prob = read_level_probability.detach().cpu().numpy().reshape(-1, 1)
        kmeans = KMeans(2).fit(prob)
        labels = kmeans.labels_
    
    read_representation = read_representation.reshape(-1, read_representation.shape[-1])
    probability_mask = (labels == 1)   
    pos_reps = read_representation[probability_mask]
    neg_reps = read_representation[~probability_mask]
    device = read_representation.device
    eps = torch.Tensor([1e-08]).to(device)
    rep_loss = 0
    for reps in [pos_reps, neg_reps]:
        if len(reps) > 1:
            reps = reps.div(torch.max(reps.pow(2).sum(1, keepdim=True).pow(0.5), eps))
            rep_similarities = reps @ reps.t()
            norm_constants = rep_similarities.numel() - rep_similarities.shape[0]
            avg_similarities = (torch.sum(rep_similarities) - torch.sum(torch.diagonal(rep_similarities))) / norm_constants # zeroing the diagonal
            sim_loss = -torch.log(avg_similarities)
            rep_loss += sim_loss

    loss =  lambda_1 * site_level_loss + lambda_2 * rep_loss
    return {'y_pred': site_level_probability, 'loss': loss, 
            'site_loss': site_level_loss, 'sim_loss': sim_loss}
Esempio n. 19
0
    def forward(self, input_ids, pos_tag_ids, senti_word_ids, polarity_ids, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None,
                labels=None):
        outputs = self.roberta(input_ids,
                               attention_mask=attention_mask,
                               token_type_ids=token_type_ids,
                               position_ids=position_ids,
                               head_mask=head_mask,
                               pos_ids=pos_tag_ids,
                               senti_word_ids=senti_word_ids,
                               polarity_ids=polarity_ids
                               )
        sequence_output = outputs[0]
        logits = self.classifier(sequence_output)
        logits = self.sigmoid_layer(logits)

        outputs = (logits,) + outputs[2:]
        if labels is not None:
            if self.num_labels == 1:
                #  We are doing regression
                loss_fct = MSELoss()
                loss = loss_fct(logits.view(-1), labels.view(-1))
            else:
                loss_fct = BCELoss()
                loss = loss_fct(logits.view(-1), labels.view(-1))
            outputs = (loss,) + outputs

        return outputs  # (loss), logits, (hidden_states), (attentions)
Esempio n. 20
0
 def __init__(self,
              se_loss=False,
              se_weight=0.2,
              nclass=-1,
              aux=False,
              aux_weight=0.4,
              weight=None,
              size_average=True,
              ignore_index=-1,
              reduction='mean'):
     super(SegmentationLosses_oc, self).__init__(weight,
                                                 ignore_index=ignore_index,
                                                 reduction=reduction)
     self.ignore_index = ignore_index
     self.se_loss = se_loss
     self.aux = aux
     self.nclass = nclass
     self.se_weight = se_weight
     self.aux_weight = aux_weight
     self.bceloss = BCELoss(weight, reduction=reduction)
     self.bce = BCEWithLogitsLoss(weight=None,
                                  size_average=None,
                                  reduce=None,
                                  reduction='mean',
                                  pos_weight=None)
Esempio n. 21
0
def bce(y_pred, y_true, indices=None, padded_value_indicator=PADDED_Y_VALUE):
    """
    Binary Cross-Entropy loss.
    :param y_pred: predictions from the model, shape [batch_size, slate_length]
    :param y_true: ground truth labels, shape [batch_size, slate_length]
    :param padded_value_indicator: an indicator of the y_true index containing a padded item, e.g. -1
    :return: loss value, a torch.Tensor
    """
    device = get_torch_device()

    y_pred = y_pred.clone()
    y_true = y_true.clone()

    mask = y_true == padded_value_indicator
    valid_mask = y_true != padded_value_indicator

    ls = BCELoss(reduction='none')(y_pred, y_true)
    ls[mask] = 0.0

    document_loss = torch.sum(ls, dim=-1)
    sum_valid = torch.sum(valid_mask, dim=-1).type(
        torch.float32) > torch.tensor(0.0, dtype=torch.float32, device=device)

    loss_output = torch.sum(document_loss) / torch.sum(sum_valid)

    return loss_output
Esempio n. 22
0
    def __init__(self):
        super().__init__()
        self.criterion = BCELoss()

        self.old_classes = []
        self.old_model = None
        self.old_logits = None
Esempio n. 23
0
 def __init__(self,
              se_loss=False,
              se_weight=0.2,
              nclass=-1,
              aux=False,
              aux_weight=0.4,
              weight=None,
              size_average=True,
              ignore_index=-1,
              reduction='mean'):
     super(SegmentationLosses_contour_BoundaryRelax,
           self).__init__(weight,
                          ignore_index=ignore_index,
                          reduction=reduction)
     self.se_loss = se_loss
     self.aux = aux
     self.nclass = nclass
     self.se_weight = se_weight
     self.aux_weight = aux_weight
     self.bceloss = BCELoss(weight, reduction=reduction)
     self.gamma = 2.0
     self.alpha = 1.0
     self.label_relax = RelaxedBoundaryLossToTensor(ignore_id=ignore_index,
                                                    num_classes=nclass)
     self.label_relax_loss = ImgWtLossSoftNLL(classes=nclass,
                                              ignore_index=ignore_index,
                                              weights=None,
                                              upper_bound=1.0,
                                              norm=False)
Esempio n. 24
0
    def __init__(self, config, special_slot_label_number, slot_embedding_type='with_BIO', matching_similarity_type='xy1', sentence_encoder_shared=True, slot_embedding_dependent_query_encoding='none', matching_similarity_y='ctx', matching_similarity_function='dot', task_adaptive_projection='none', pretrained_tf_model=None, device=None):
        '''
        slot_embedding_type: with_BIO, with_BI, without_BIO
        matching_similarity_type: xy, x1y, xy1, x1y1, rxy
        slot_embedding_dependent_query_encoding: none, ctx, desc
        matching_similarity_y: ctx, desc, ctx_desc
        task_adaptive_projection: none, LSM, adaptive_finetune, LEN
        '''
        super().__init__()

        self.config = config
        self.device = device
        self.special_slot_label_number = special_slot_label_number
        self.slot_embedding_type = slot_embedding_type
        self.matching_similarity_type = matching_similarity_type
        self.matching_similarity_y = matching_similarity_y

        #sentence_encoder_shared = True #default 'True'; False
        bilinear = False #default 'False'; True, False
        crf_trainable_balance_weight = False # default 'False'; False
        if self.matching_similarity_type == 'rxy':
            crf_trainable_balance_weight = True
        
        #slot_embedding_dependent_query_encoding = True # True, False
        self.slot_embedding_dependent_query_encoding = slot_embedding_dependent_query_encoding

        # token embedding layer (shared between query and support sets)
        self.query_sentence_encoder = SequenceEncoder_with_pure_bert(config, pretrained_tf_model, device=self.device)
        if sentence_encoder_shared:
            self.support_sentence_encoder = self.query_sentence_encoder
        else:
            self.support_sentence_encoder = SequenceEncoder_with_pure_bert(config, pretrained_tf_model, device=self.device)
        encoder_output_dim = self.query_sentence_encoder.get_output_dim()
        self.special_slot_label_embeddings = nn.Embedding(special_slot_label_number, encoder_output_dim)

        self.slot_tagger_projection_x = Projection(config, encoder_output_dim, device=self.device)
        self.slot_tagger_projection_y = self.slot_tagger_projection_x
        self.slot_tagger = MatchingClassifier(config, matching_similarity_function=matching_similarity_function)
        projected_tag_embedding_dim = self.slot_tagger_projection_y.get_output_dim()
        if config.task_st == "slot_tagger":
            self.slot_tag_loss_fct = CrossEntropyLoss(ignore_index=config.output_tag_pad_id, size_average=False)
        elif config.task_st == "slot_tagger_with_adaptive_crf":
            self.crf_transition_layer = TransitionLayer(projected_tag_embedding_dim)
            self.crf = CRFLoss(trainable_balance_weight=crf_trainable_balance_weight, device=self.device)
            self.slot_tag_loss_fct = self.crf.neg_log_likelihood_loss
        elif config.task_st == "slot_tagger_with_abstract_crf":
            self.crf_transition_layer = AbstractTransitionLayer(device=self.device)
            self.crf = CRFLoss(trainable_balance_weight=crf_trainable_balance_weight, device=self.device)
            self.slot_tag_loss_fct = self.crf.neg_log_likelihood_loss
        else:
            exit()
        
        self.intent_multi_class = (config.task_sc_type == "multi_cls_BCE")
        self.intent_classifier = MatchingClassifier(config, matching_similarity_function=matching_similarity_function)
        if self.intent_multi_class:
            self.intent_loss_fct = BCELoss(size_average=False)
        else:
            self.intent_loss_fct = CrossEntropyLoss(size_average=False)

        self.init_weights()
Esempio n. 25
0
def main():
    params = vars(
        train_parse_args(hyperparameters_default=HYPERPARAMETERS,
                         paths_default=PATHS))

    if not params['disable_cuda'] and torch.cuda.is_available():
        params['device'] = torch.device('cuda:0')
    else:
        params['device'] = torch.device('cpu')

    print(torch.cuda.is_available(), params['device'])

    loaders = get_data_loaders(imgs_dir=params['imgs_dir'],
                               labels_filename=params['labels_filename'],
                               batch_size=params['batch_size'],
                               n_imgs=params['n_imgs'])

    model = ResNet()
    #model = SimpleClassifier()
    model.cuda()
    optimizer = Adam(model.parameters(), lr=3e-4)
    criterion = BCELoss()
    trainer = Trainer(params, model, optimizer, criterion)

    trainer.run(loaders)
Esempio n. 26
0
 def __init__(self, args: argparse.Namespace):
     super().__init__()
     self.args = args
     self.model = ConvolutionClassifier()
     self.loss_fn = BCELoss()
     self.predict = [[] for i in range(919)]
     self.label = [[] for i in range(919)]
Esempio n. 27
0
 def __init__(self, vocab_size_1=16, vocab_size_2=4, word_dim=300,
              hidden_dim=256, batch_size=64, num_layers=2, use_dropout=True,
              dropout=0.5, use_cuda=False):
     super(AttentionSeq2Seq, self).__init__()
     self.hidden_dim = hidden_dim
     self.num_layers = num_layers
     self.use_cuda = use_cuda
     self.W1 = nn.Linear(hidden_dim*2, hidden_dim*2)
     self.W2 = nn.Linear(hidden_dim*2, hidden_dim*2)
     self.W3 = nn.Linear(hidden_dim*2, vocab_size_2)
     self.W4 = nn.Linear(hidden_dim*2, vocab_size_2)
     self.v = nn.Linear(hidden_dim*2, 1)
     if use_dropout:
         self.encoder = nn.LSTM(word_dim, hidden_dim, dropout=dropout,
                                num_layers=self.num_layers, batch_first=True,
                                bidirectional=True)
         self.decoder = nn.LSTM(word_dim, hidden_dim, dropout=dropout,
                                num_layers=self.num_layers, batch_first=True,
                                bidirectional=True)
     else:
         self.encoder = nn.LSTM(word_dim, hidden_dim,
                                num_layers=self.num_layers, batch_first=True,
                                bidirectional=True)
         self.decoder = nn.LSTM(word_dim, hidden_dim,
                                num_layers=self.num_layers, batch_first=True,
                                bidirectional=True)
     self.encembedding = nn.Embedding(vocab_size_1, word_dim)
     self.decembedding = nn.Embedding(vocab_size_2, word_dim)
     self.batch_size = batch_size
     self.loss = BCELoss()
Esempio n. 28
0
    def __init__(self, args, word_embeddings: TextFieldEmbedder,
                 vocab: Vocabulary) -> None:
        super().__init__(vocab)

        # parameters
        self.args = args
        self.word_embeddings = word_embeddings

        # layers
        self.event_embedding = EventEmbedding(self.args, self.word_embeddings)
        self.lstm = DynamicLSTM(self.args.embedding_size * 2,
                                self.args.hidden_size,
                                num_layers=1,
                                batch_first=True)
        self.attention = NoQueryAttention(self.args.hidden_size +
                                          self.args.embedding_size * 2,
                                          score_function='bi_linear')
        self.score = Score(self.args.hidden_size,
                           self.args.embedding_size,
                           threshold=self.args.threshold)

        # metrics
        self.accuracy = BooleanAccuracy()
        self.f1_score = F1Measure(positive_label=1)
        self.loss_function = BCELoss()
def eval_genome(g, c):

    cppn = create_cppn(
        g, c,
        ['k', 'C_in', 'C_out', 'z'],
        ['w', 'b'])

    cnn = AudioCNN(cppn)

    criterion = BCELoss()
    optimizer = torch.optim.SGD(cnn.parameters(), lr=0.001)
    y_ = getattr(c, "y")

    loss = None

    for i in range(20):

        out = cnn(c.data.view(c.data.shape[0], 1, -1))
        optimizer.zero_grad()
        out = torch.sigmoid(cnn.l1(out.view(-1, 72)))
        loss = criterion(out, y_)
        loss.backward()
        optimizer.step()

    return 1 / (1 + loss.detach().item())
Esempio n. 30
0
    def __init__(self, dev, dis):
        from torch.nn import BCELoss

        super().__init__(dev, dis)

        # define the criterion object
        self.criterion = BCELoss()