Пример #1
0
 def __init__(self, hdfs_host: str = None, device: str = 'cpu'):
     self.device = device
     self.task = HateSpeech(self.TRAIN_DATA_PATH, (9, 1))
     self.model = BaseLine(256, 3, 0.2,
                           self.task.max_vocab_indexes['syllable_contents'],
                           384)
     self.model.to(self.device)
     self.loss_fn = nn.BCELoss()
     self.batch_size = 128
     self.__test_iter = None
     bind_model(self.model)
Пример #2
0
 def __init__(self, hdfs_host: str = None):
     self.device = config.device
     self.task = HateSpeech(self.TRAIN_DATA_PATH, [9, 1])
     self.model = BaseLine(config.hidden_dim, config.filter_size,
                           config.dropout,
                           self.task.max_vocab_indexes['syllable_contents'],
                           config.embedding_dim,
                           config.padding)  ## 저장모델도 같게 해줘~
     ## Baseline : self.model = BaseLine(256, 3, 0.2, self.task.max_vocab_indexes['syllable_contents'], 384)
     #         print('can use gpu num = ',torch.cuda.device_count())
     if torch.cuda.device_count() > 1:
         self.model = nn.DataParallel(self.model)
     self.model.to(config.device)
     self.loss_fn = nn.BCEWithLogitsLoss(
         torch.tensor(config.weights[0] / config.weights[1]))
     self.batch_size = config.batch_size
     self.__test_iter = None
     bind_model(self.model)
Пример #3
0
    EMBEDDING_SIZE = 128
    BATCH_SIZE = 512
    BI_RNN_LAYERS = 1
    UNI_RNN_LAYERS = 1
    LEARNING_RATE = 0.001
    ENSEMBLE_SIZE = 5

    parser = ArgumentParser()
    parser.add_argument('--mode', default='train')
    parser.add_argument('--pause', default=0)
    args = parser.parse_args()
    task = HateSpeech()
    vocab_size = task.max_vocab_indexes['syllable_contents']
    models = []
    for i in range(ENSEMBLE_SIZE):
        model = BaseLine(HIDDEN_DIM, DROPOUT_RATE, vocab_size, EMBEDDING_SIZE, BI_RNN_LAYERS, UNI_RNN_LAYERS)
        model.to('cuda')
        models.append(model)

    if args.pause:
        bind_models(models)
        nsml.paused(scope=locals())
    if args.mode == 'train':
        scores = []
        for i, model in enumerate(models):
            trainer = Trainer(model, i, ENSEMBLE_SIZE, device='cuda')
            best_state_dict, best_f1_score = trainer.train()
            model.load_state_dict(best_state_dict)
            scores.append(best_f1_score)
            print('best f1 score:', best_f1_score)
Пример #4
0
class Trainer(object):
    TRAIN_DATA_PATH = '{}/train/train_data'.format(DATASET_PATH)

    def __init__(self, hdfs_host: str = None):
        self.device = config.device
        self.task = HateSpeech(self.TRAIN_DATA_PATH, [9, 1])
        self.model = BaseLine(config.hidden_dim, config.filter_size,
                              config.dropout,
                              self.task.max_vocab_indexes['syllable_contents'],
                              config.embedding_dim,
                              config.padding)  ## 저장모델도 같게 해줘~
        ## Baseline : self.model = BaseLine(256, 3, 0.2, self.task.max_vocab_indexes['syllable_contents'], 384)
        #         print('can use gpu num = ',torch.cuda.device_count())
        if torch.cuda.device_count() > 1:
            self.model = nn.DataParallel(self.model)
        self.model.to(config.device)
        self.loss_fn = nn.BCEWithLogitsLoss(
            torch.tensor(config.weights[0] / config.weights[1]))
        self.batch_size = config.batch_size
        self.__test_iter = None
        bind_model(self.model)

    @property
    def test_iter(self) -> Iterator:
        if self.__test_iter:
            self.__test_iter.init_epoch()
            return self.__test_iter
        else:
            self.__test_iter = Iterator(
                self.task.datasets[-1],
                batch_size=self.batch_size,
                repeat=False,
                sort_key=lambda x: len(x.syllable_contents),
                train=False,
                device=self.device)
            return self.__test_iter

    def train(self):
        max_epoch = 50
        optimizer = optim.Adam(self.model.parameters(),
                               lr=config.learning_rate)
        total_len = len(self.task.datasets[0])
        ds_iter = Iterator(self.task.datasets[0],
                           batch_size=self.batch_size,
                           repeat=False,
                           sort_key=lambda x: len(x.syllable_contents),
                           train=True,
                           device=self.device)
        min_iters = 10
        max_acc = 0
        max_acc_epoch = 0
        for epoch in range(max_epoch):
            loss_sum, acc_sum, len_batch_sum = 0., 0., 0.
            ds_iter.init_epoch()
            tr_total = math.ceil(total_len / self.batch_size)

            self.model.train()
            for batch in ds_iter:
                self.model.zero_grad()
                data = batch.syllable_contents.cuda()
                target = batch.eval_reply.reshape(len(batch.eval_reply),
                                                  1).cuda()
                pred = self.model(data).cuda()
                loss = self.loss_fn(pred, target)

                loss.backward()
                optimizer.step()

                acc = torch.sum(pred.sigmoid().round() == target,
                                dtype=torch.float32)

                len_batch = len(batch)
                len_batch_sum += len_batch
                acc_sum += acc.tolist()
                loss_sum += loss.tolist() * len_batch

            pred_lst, loss_avg, acc_sum, te_total = self.eval(
                self.test_iter, len(self.task.datasets[1]))
            acc_current = acc_sum / te_total
            if acc_current > max_acc:
                max_acc = acc_current
                max_acc_epoch = epoch
            nsml.save(epoch)
        print(f'max accuracy = {max_acc} when epoch {max_acc_epoch}')

    def eval(self, iter: Iterator,
             total: int) -> (List[float], float, List[float], int):
        pred_lst = list()
        target_lst = list()
        loss_sum = 0.
        acc_sum = 0.

        self.model.eval()
        for batch in iter:
            data = batch.syllable_contents.cuda()
            target = batch.eval_reply.reshape(len(batch.eval_reply), 1).cuda()

            pred = self.model(data).cuda()

            accs = torch.sum(pred.sigmoid().round() == target,
                             dtype=torch.float32)
            losses = self.loss_fn(pred, target)

            pred_lst += pred.sigmoid().round().squeeze().tolist()
            acc_sum += accs.tolist()
            target_lst += batch.eval_reply.tolist()
            loss_sum += losses.tolist() * len(batch)
        return pred_lst, loss_sum / total, acc_sum, total

    def save_model(self, model, appendix=None):
        file_name = 'model'
        if appendix:
            file_name += '_{}'.format(appendix)
        torch.save({
            'model': model,
            'task': type(self.task).__name__
        }, file_name)
Пример #5
0
if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('--mode', default='train')
    parser.add_argument('--pause', default=0)
    args = parser.parse_args()

    torch.manual_seed(2020)
    torch.cuda.manual_seed_all(2020)
    np.random.seed(2020)
    random.seed(2020)

    if args.pause:
        task = HateSpeech()
        model = BaseLine(config.hidden_dim, config.filter_size, config.dropout,
                         task.max_vocab_indexes['syllable_contents'],
                         config.embedding_dim, config.padding)
        model.to("cuda")
        bind_model(model)
        nsml.paused(scope=locals())
    if args.mode == 'train':
        print(config)
        trainer = Trainer()
        trainer.train()
        print('-' * 50)
        ##############################################
        config.embedding_dim = 128
        print(config)
        trainer = Trainer()
        trainer.train()
        print('-' * 50)
Пример #6
0
def SimulatedAnnealing(modelParam):
    ###print purpose
    output = ""
    cntForQ=0
    cntForExcl=0
    cntForPlus=0
    cntForDot=0

    printCounter =  0

    eMaxVal = 0
    curr_sol=modelParam()
    best_sol =modelParam()
    best_sol.copy(curr_sol)
    #print("Before ... ", curr_sol.getCurrentBaseline())

    #print("isBaseLineSet:", BaseLine.is_baseline_set)

    if BaseLine.is_baseline_set == False:
        #curr_sol.updateBaseline(BaseLine.getInitialBaseline(modelParam))
        initialBaseline = BaseLine.getInitialBaseline(modelParam)
        #print("Output of initial baseline ", initialBaseline)
        BaseLine.baseline_min = initialBaseline[0]
        BaseLine.baseline_max = initialBaseline[1]
        newBaseLine = [BaseLine.baseline_min, BaseLine.baseline_max]
        curr_sol.updateBaseline(newBaseLine)        
    else:
        #print("HO GAYA TRUE")
        newBaseLine = [BaseLine.baseline_min, BaseLine.baseline_max]
        curr_sol.updateBaseline(newBaseLine)
        #print("newBaseLine:", newBaseLine)
    #print("BaseLine Min:", BaseLine.baseline_min)
    #print("BaseLine Max:", BaseLine.baseline_max)
    #print( "After ... ",    curr_sol.getCurrentBaseline())
    #exit
    ## kMaxVal is always fixed to 1000 !
    kMaxVal=1000
    #counter = 0
    counter = 1000
    ## to keep track of eras
    eraDict = {}
    eraCount  = 0
    eraTracker = 50
    crr_era, prev_era = [], [0 for _ in range(curr_sol.numOfDec)]
    terminateCount = 0
    terminator = 10
    eraList = []
    a12Factor = 0.56
    eraDictCount  = 0

    while (counter > 0) and (curr_sol.sumOfObjs() > eMaxVal):

        printCounter = printCounter + 1

        neighbor_sol=generateNeighbor(curr_sol, randint(0, curr_sol.numOfDec-1), modelParam)


        if neighbor_sol.sumOfObjs() < best_sol.sumOfObjs():
            best_sol.copy(neighbor_sol)
            curr_sol.copy(neighbor_sol)
            cntForExcl = cntForExcl + 1
            output = output + "!"
        elif neighbor_sol.sumOfObjs() < curr_sol.sumOfObjs():
            curr_sol.copy(neighbor_sol)
            cntForPlus = cntForPlus + 1
            output = output + "+"
        elif fetchProbOfAcceptance(neighbor_sol.sumOfObjs(), curr_sol.sumOfObjs(), float(counter)/float(kMaxVal)) > random():
            curr_sol.copy(neighbor_sol)
            cntForQ = cntForQ + 1
            output = output + "?"
        else:
           output = output + "."
           cntForDot = cntForDot  + 1
        if printCounter % eraTracker == 0:
           #print("\ncounter={}, best energy seen so far={}, '?'={}, '!'={}, '+'={}, '.'={}, output={}".format(printCounter, best_sol.sumOfObjs(), cntForQ, cntForExcl, cntForPlus, cntForDot, output))
           cntForQ = 0
           cntForExcl = 0
           cntForPlus = 0
           cntForDot = 0
           output = ""
           ## era purpose
        if BaseLine.baseline_min > best_sol.sumOfObjs():
           BaseLine.baseline_min = best_sol.sumOfObjs()    
        if BaseLine.baseline_max < best_sol.sumOfObjs():
           BaseLine.baseline_max = best_sol.sumOfObjs()   
        newBaseLine = [BaseLine.baseline_min, BaseLine.baseline_max]
        curr_sol.updateBaseline(newBaseLine)                   
        if eraCount ==19:
          ## comparing prev and current
          crr_era = sorted(eraList, reverse=True)
          #print("Current era: ", crr_era)

          eraDict[eraDictCount] = crr_era
          #print ("Contents of the dictionary ", eraDict[eraDictCount])          
          eraDictCount = eraDictCount + 1
          #print ("era dict count ", eraDictCount)
          
          a12Output =  utilities.a12(crr_era, prev_era)

          if a12Output <= a12Factor:
             terminateCount = terminateCount + 1
          eraList = []
          eraCount = 0
          prev_era = crr_era
          #print("era count ={}, era dict= {}, a12={}, terminator={}".format(eraCount, prev_era, crr_era, a12Output, terminateCount))
        else:
          eraList.append(best_sol.getobj())
          eraCount +=  1
          #print("era count ... ", eraCount)
        if terminateCount >= terminator:
             break
#        if curr_sol.sumOfObjs() < curr_sol.getCurrentBaseline()[0]:
#          curr_sol.updateBaseline( [curr_sol.sumOfObjs(), curr_sol.getCurrentBaseline()[1]])
#          # print("+++++^^BASELINE UPDATED")
#        if curr_sol.sumOfObjs() > curr_sol.getCurrentBaseline()[1]:
#          curr_sol.updateBaseline([curr_sol.getCurrentBaseline()[0], curr_sol.sumOfObjs()])
          #print("+++++^^BASELINE UPDATED MILA MAX")
          #exit()

#        #DOUBT
#        if curr_sol.sumOfObjs() > 12.7814799596:
#            print("GREATER THAN 13")
#            #exit()
        #print("current energy ", curr_sol.sumOfObjs())
        counter = counter - 1
        #counter= counter + 1

    #Update Global Baseline :
    #print(curr_sol.getCurrentBaseline());

    BaseLine.baseline_min = curr_sol.getCurrentBaseline()[0]
    BaseLine.baseline_max = curr_sol.getCurrentBaseline()[1]
    BaseLine.is_baseline_set = True


    #print("BaseLine Min:", BaseLine.baseline_min)
    #print("BaseLine Max:", BaseLine.baseline_max)
    #print("Era Dict Count :", eraDictCount)
    #print("Era Dictionary First:", eraDict[1])
    #print("-------------------------------------------------------------------------------------")
    #print("Era Dictionary Last:", eraDict[eraDictCount])
    #print("-------------------------------------------------------------------------------------")

    _printSolution(best_sol.decisionVec, best_sol.sumOfObjs(), best_sol.getobj(), printCounter)
    _printEndMsg(SimulatedAnnealing.__name__)
    # print("------------------------------------------------------------------------------------------------")
    # print("Era dictionary First:=", eraDict[1])
    # print("Era dictionary Last:=", eraDict[eraDictCount])
    # print("------------------------------------------------------------------------------------------------")
    #print("era dict ", len(eraDict[1]))
    #exit()    
    return eraDict
Пример #7
0
def MaxWalkSat(model, maxTries=100, maxChanges=10, threshold=0, p=0.5, step=10):
    printCounter = 0
    ####
    #print purpose
    output = ""
    printCounter = 0
    cnt_Exc = 0
    cnt_Que = 0
    cnt_Dot = 0
    cnt_Pls = 0
    ## to keep track of eras
    eraDict = {}
    eraCount = 0
    eraTracker = 50
    temporary_sol= model()   # Just for getting the object
    crr_era, prev_era = [], [0 for _ in xrange(temporary_sol.numOfDec)]
    terminateCount = 0
    terminator = 10
    eraList = []
    a12Factor = 0.56
    eraDictCount = 0

    #print("isBaseLineSet:", BaseLine.is_baseline_set)

    for cntTry in range(maxTries):

        curr_solve_for_model = model()

        if BaseLine.is_baseline_set == False:
          #curr_sol.updateBaseline(BaseLine.getInitialBaseline(modelParam))
          initialBaseline = BaseLine.getInitialBaseline(model)
          #print("Output of initial baseline ", initialBaseline)
          BaseLine.baseline_min = initialBaseline[0]
          BaseLine.baseline_max = initialBaseline[1]
          newBaseLine = [BaseLine.baseline_min, BaseLine.baseline_max]
          curr_solve_for_model.updateBaseline(newBaseLine)        
        else:
          #print("HO GAYA TRUE")
          newBaseLine = [BaseLine.baseline_min, BaseLine.baseline_max]
          curr_solve_for_model.updateBaseline(newBaseLine)
          # print("newBaseLine:", newBaseLine)

        if cntTry==0:
            sbest=model()
            sbest.copy(curr_solve_for_model)
        for cng in range(maxChanges):
            printCounter = printCounter + 1

            if curr_solve_for_model.sumOfObjs() < threshold:
                print("\n Got lucky !. \n Found the solution early for {} by MaxWlakSat at step {} \n".format(model.__name__, printCounter))
                sbest.copy(curr_solve_for_model)
                _printSolution(curr_solve_for_model.decisionVec, curr_solve_for_model.sumOfObjs(), curr_solve_for_model.getobj(), printCounter)
                _printEndMsg(MaxWalkSat.__name__)
                return sbest.decisionVec

            decToMutate=randint(0, curr_solve_for_model.numOfDec-1)
            ## keeping tack of values before mutating annd labeling them as old
            score_old = curr_solve_for_model.sumOfObjs()
            oldModel = model()
            oldModel.copy(curr_solve_for_model)

            if p < random():
                curr_solve_for_model=generateNeighbor(curr_solve_for_model, decToMutate, model)
            else:
                #curr_solve_for_model=_mutateSelectivelyWithModifiedRange(model, curr_solve_for_model, decToMutate, step)
                curr_solve_for_model=_mutateSelectively(model, curr_solve_for_model, decToMutate, step)
            ## lets heck teh ebhavior !
                
            if curr_solve_for_model.sumOfObjs() < sbest.sumOfObjs():
                sbest.copy(curr_solve_for_model)
                    
                #'?' means muatted solution  is better than best soultuion
                # btw, we are only interested in the best and we keep that
                output = output + "?"
                cnt_Que = cnt_Que + 1
            elif curr_solve_for_model.sumOfObjs() < score_old:
                #'!' means muatted solution  is better than old soultuion

                output = output + "!"
                cnt_Exc = cnt_Exc + 1
            elif curr_solve_for_model.sumOfObjs() >= score_old:
                ## '+' means old solution is better than mutated solution
                output = output + "+"
                cnt_Pls = cnt_Pls + 1
            else:
                output = output + "."
                cnt_Dot = cnt_Dot + 1
            # if printCounter % 40 == 0:
            if printCounter % eraTracker == 0:
                #print("itrations so far={} '?'={}, '!'={}, '+'={}, '.'={}, output={} ".format(printCounter, cnt_Que, cnt_Exc, cnt_Pls, cnt_Dot,output))
                #print(output + '\n')
                output = ""

                cnt_Que = 0
                cnt_Exc = 0
                cnt_Pls = 0
                cnt_Dot = 0

            ## era purpose
            if BaseLine.baseline_min > sbest.sumOfObjs():
                  BaseLine.baseline_min = sbest.sumOfObjs()    
            if BaseLine.baseline_max < sbest.sumOfObjs():
                  BaseLine.baseline_max = sbest.sumOfObjs() 
            newBaseLine = [BaseLine.baseline_min, BaseLine.baseline_max]
            curr_solve_for_model.updateBaseline(newBaseLine)                          
            if eraCount ==19:
                ## comparing prev and current
                crr_era = sorted(eraList, reverse=True)
                #print("Current era:", curr_era)

                eraDict[eraDictCount] = crr_era
                eraDictCount = eraDictCount + 1
                a12Output = utilities.a12(crr_era, prev_era)

                if a12Output <= a12Factor:
                    terminateCount = terminateCount + 1
                eraList = []
                eraCount = 0
                prev_era = crr_era
                #print("era count ={}, era dict= {}, a12={}, terminator={}".format(eraCount, prev_era, crr_era, a12Output, terminateCount))
            else:
                eraList.append(sbest.getobj())
                eraCount += 1

            if terminateCount >= terminator:
                break            

            
#            if curr_solve_for_model.sumOfObjs() < curr_solve_for_model.getCurrentBaseline()[0]:
#                curr_solve_for_model.updateBaseline( [curr_solve_for_model.sumOfObjs(), curr_solve_for_model.getCurrentBaseline()[1]])
#                # print("+++++^^BASELINE UPDATED")
#            if curr_solve_for_model.sumOfObjs() > curr_solve_for_model.getCurrentBaseline()[1]:
#                curr_solve_for_model.updateBaseline([curr_solve_for_model.getCurrentBaseline()[0], curr_solve_for_model.sumOfObjs()])
                #print("+++++^^BASELINE UPDATED MILA MAX")
                #exit()

        #Update Global Baseline :
        #print(curr_solve_for_model.getCurrentBaseline());

        BaseLine.baseline_min = curr_solve_for_model.getCurrentBaseline()[0]
        BaseLine.baseline_max = curr_solve_for_model.getCurrentBaseline()[1]
        BaseLine.is_baseline_set = True

    #print("BaseLine Min:", BaseLine.baseline_min)
    #print("BaseLine Max:", BaseLine.baseline_max)
    #print("Era Dict Count :", eraDictCount)
    #print("Era Dictionary First:", eraDict[1])
    #print("-------------------------------------------------------------------------------------")
    #print("Era Dictionary Last:", eraDict[eraDictCount])
    #print("-------------------------------------------------------------------------------------")

    _printSolution(sbest.decisionVec, sbest.sumOfObjs() , sbest.getobj(), printCounter)
    _printEndMsg(MaxWalkSat.__name__)

    return  eraDict
Пример #8
0
class Trainer(object):
    TRAIN_DATA_PATH = '{}/train/train_data'.format(DATASET_PATH)

    def __init__(self, hdfs_host: str = None, device: str = 'cpu'):
        self.device = device
        self.task = HateSpeech(self.TRAIN_DATA_PATH, (9, 1))
        self.model = BaseLine(256, 3, 0.2,
                              self.task.max_vocab_indexes['syllable_contents'],
                              384)
        self.model.to(self.device)
        self.loss_fn = nn.BCELoss()
        self.batch_size = 128
        self.__test_iter = None
        bind_model(self.model)

    @property
    def test_iter(self) -> Iterator:
        if self.__test_iter:
            self.__test_iter.init_epoch()
            return self.__test_iter
        else:
            self.__test_iter = Iterator(
                self.task.datasets[1],
                batch_size=self.batch_size,
                repeat=False,
                sort_key=lambda x: len(x.syllable_contents),
                train=False,
                device=self.device)
            return self.__test_iter

    def train(self):
        max_epoch = 32
        optimizer = optim.Adam(self.model.parameters())
        total_len = len(self.task.datasets[0])
        ds_iter = Iterator(self.task.datasets[0],
                           batch_size=self.batch_size,
                           repeat=False,
                           sort_key=lambda x: len(x.syllable_contents),
                           train=True,
                           device=self.device)
        min_iters = 10
        for epoch in range(max_epoch):
            loss_sum, acc_sum, len_batch_sum = 0., 0., 0.
            ds_iter.init_epoch()
            tr_total = math.ceil(total_len / self.batch_size)
            tq_iter = tqdm(
                enumerate(ds_iter),
                total=tr_total,
                miniters=min_iters,
                unit_scale=self.batch_size,
                bar_format=
                '{n_fmt}/{total_fmt} [{elapsed}<{remaining} {rate_fmt}] {desc}'
            )

            self.model.train()
            for i, batch in tq_iter:
                self.model.zero_grad()
                pred = self.model(batch.syllable_contents)
                acc = torch.sum((torch.reshape(pred, [-1]) >
                                 0.5) == (batch.eval_reply > 0.5),
                                dtype=torch.float32)
                loss = self.loss_fn(pred, batch.eval_reply)
                loss.backward()
                optimizer.step()

                len_batch = len(batch)
                len_batch_sum += len_batch
                acc_sum += acc.tolist()
                loss_sum += loss.tolist() * len_batch
                if i % min_iters == 0:
                    tq_iter.set_description(
                        '{:2} loss: {:.5}, acc: {:.5}'.format(
                            epoch, loss_sum / len_batch_sum,
                            acc_sum / len_batch_sum), True)
                if i == 3000:
                    break

            tq_iter.set_description(
                '{:2} loss: {:.5}, acc: {:.5}'.format(epoch,
                                                      loss_sum / total_len,
                                                      acc_sum / total_len),
                True)

            print(
                json.dumps({
                    'type': 'train',
                    'dataset': 'hate_speech',
                    'epoch': epoch,
                    'loss': loss_sum / total_len,
                    'acc': acc_sum / total_len
                }))

            pred_lst, loss_avg, acc_lst, te_total = self.eval(
                self.test_iter, len(self.task.datasets[1]))
            print(
                json.dumps({
                    'type': 'test',
                    'dataset': 'hate_speech',
                    'epoch': epoch,
                    'loss': loss_avg,
                    'acc': sum(acc_lst) / te_total
                }))
            nsml.save(epoch)
            self.save_model(self.model, 'e{}'.format(epoch))

    def eval(self, iter: Iterator,
             total: int) -> (List[float], float, List[float], int):
        tq_iter = tqdm(enumerate(iter),
                       total=math.ceil(total / self.batch_size),
                       unit_scale=self.batch_size,
                       bar_format='{r_bar}')
        pred_lst = list()
        loss_sum = 0.
        acc_lst = list()

        self.model.eval()
        for i, batch in tq_iter:
            preds = self.model(batch.syllable_contents)
            accs = torch.eq(preds > 0.5,
                            batch.eval_reply > 0.5).to(torch.float)
            losses = self.loss_fn(preds, batch.eval_reply)
            pred_lst += preds.tolist()
            acc_lst += accs.tolist()
            loss_sum += losses.tolist() * len(batch)
        return pred_lst, loss_sum / total, acc_lst, total

    def save_model(self, model, appendix=None):
        file_name = 'model'
        if appendix:
            file_name += '_{}'.format(appendix)
        torch.save({
            'model': model,
            'task': type(self.task).__name__
        }, file_name)
Пример #9
0
            losses = self.loss_fn(preds, batch.eval_reply)
            pred_lst += preds.tolist()
            acc_lst += accs.tolist()
            loss_sum += losses.tolist() * len(batch)
        return pred_lst, loss_sum / total, acc_lst, total

    def save_model(self, model, appendix=None):
        file_name = 'model'
        if appendix:
            file_name += '_{}'.format(appendix)
        torch.save({
            'model': model,
            'task': type(self.task).__name__
        }, file_name)


if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('--mode', default='train')
    parser.add_argument('--pause', default=0)
    args = parser.parse_args()
    if args.pause:
        task = HateSpeech()
        model = BaseLine(256, 3, 0.2,
                         task.max_vocab_indexes['syllable_contents'], 384)
        model.to("cuda")
        bind_model(model)
        nsml.paused(scope=locals())
    if args.mode == 'train':
        trainer = Trainer(device='cuda')
        trainer.train()
Пример #10
0
def de(model, max = 1000, f = 0.75, cf = 0.3, epsilon = 0.01):
  from model import BaseLine 
  global eraDict, eraCount , eraTracker, crr_era, prev_era, terminateCount, terminator, eraList, a12Factor, eraDictCount

  eraDict = {}
  eraCount = 0
  eraTracker = 50
  crr_era, prev_era = [], []
  terminateCount = 0
  terminator = 10
  eraList = []
  a12Factor = 0.56
  eraDictCount = 0


  #baseline_min,baseline_max = get_min_max(model)
  if BaseLine.is_baseline_set == False:
    #print "flag value: ", BaseLine.is_baseline_set  
    #print "Baseline value ... max, min {}, {}".format(BaseLine.baseline_max, BaseLine.baseline_min)
    
    baseline_min,baseline_max = BaseLine.getInitialBaseline(model) 
    #print "Baseline value ... max, min {}, {}".format(BaseLine.baseline_max, BaseLine.baseline_min) 
    #exit()    
    #print "BASELINE: MIN=", baseline_min, " MAX=", baseline_max
    BaseLine.baseline_min = baseline_min
    BaseLine.baseline_max = baseline_max
    #print "baseline_min,baseline_max ", baseline_min," ",baseline_max
    BaseLine.is_baseline_set = True 
  curr_candidate_sol = model()
  prev_era = [0 for _ in range(curr_candidate_sol.numOfDec)]
  # print "FROM DE-->", curr_candidate_sol
  np = curr_candidate_sol.numOfDec * 10
  frontier = [candidate(curr_candidate_sol) for _ in xrange(np)]

  # for x in frontier:
  #   print "id:", x.id, " have:", x.have, " score:", x.score

  # print "length of frontier:", len(frontier)

  # Pending : should you use else if here?

  for each_thing in frontier:
    if(each_thing.score < 0):
      BaseLine.baseline_min = 0
      #print "--------"
    if(each_thing.score < BaseLine.baseline_min):
      BaseLine.baseline_min = each_thing.score
      #print "--------------"
    if(each_thing.score > BaseLine.baseline_max):
      BaseLine.baseline_max = each_thing.score
      #print "---------"



  #Normalize the scores of each thing now

  # for each_thing in frontier:
  #   prev_each_thing_score = each_thing.score
  #   each_thing.score = float(each_thing.score - BaseLine.baseline_min)/(BaseLine.baseline_max - BaseLine.baseline_min)

  #total = total score of all the candidates found so far
  for k in xrange(max):
    total,n = update(f,cf,frontier,curr_candidate_sol,BaseLine.baseline_min,BaseLine.baseline_max)

    # if eraCount >=20:
    #   print eraCount
    #   ## comparing prev and current
    #   crr_era = sorted(eraList, reverse=True)
    #   #print("Current era: ", crr_era)
    #   eraDictCount = eraDictCount + 1
    #   eraDict[eraDictCount] = crr_era
    #   a12Output =  utilities.a12(crr_era, prev_era)

    #   if a12Output <= a12Factor:
    #      terminateCount = terminateCount + 1

    #   eraList = []
    #   eraCount = 0
    #   prev_era = crr_era

          #print("era count ={}, era dict= {}, a12={}, terminator={}".format(eraCount, prev_era, crr_era, a12Output, terminateCount))
        # else:
        #   eraList.append(best_sol.getobj())
        #   eraCount +=  1

    # print "BASELINE: MIN=", BaseLine.baseline_min," MAX=", BaseLine.baseline_max
    # if total/n > (1 - epsilon):
    #   print "break: value of k=", k, " total=",total, "n=",n
    #   break
  # for x in frontier:
  #   print "print --x:",x.id," ",x.have, x.score

  #Now baseline everything again

  for each_thing in frontier:
    each_thing.score = (each_thing.score - BaseLine.baseline_min) / ( BaseLine.baseline_max - BaseLine.baseline_min + 0.001)

  score_have_dict = { obj.score:obj.have for obj in frontier}
  print "==================="
  #for key in sorted(score_have_dict.keys(),reverse = True):
  #   print "%s: %s" % (key, score_have_dict[key])

  #print "BASELINE: MIN=", BaseLine.baseline_min," MAX=", BaseLine.baseline_max
  sorted_keys = sorted(score_have_dict.keys(),reverse = True)
  print "Energy .... %s: %s" % (sorted_keys[0], score_have_dict[sorted_keys[0]])
  #print("era dict ", len(eraDict[1]))
  #exit()
  return eraDict