def __init__(self, hdfs_host: str = None, device: str = 'cpu'): self.device = device self.task = HateSpeech(self.TRAIN_DATA_PATH, (9, 1)) self.raw = HateSpeech(self.RAW_DATA_PATH) print('num pretrain dataset = ', len(self.raw.datasets[0])) print('num train dataset = ', len(self.task.datasets[0])) print('num validation dataset = ', len(self.task.datasets[1])) print('can use gpu num = ', torch.cuda.device_count()) config.n_enc_vocab = self.task.max_vocab_indexes['syllable_contents'] config.n_dec_vocab = self.task.max_vocab_indexes['syllable_contents'] self.save_pretrain = "save_gpt_pretrain.pth"
def infer(raw_data, **kwargs): model.eval() examples = HateSpeech(raw_data).examples tensors = [ torch.tensor(ex.syllable_contents, device='cuda').reshape([-1, 1]) for ex in examples ] results = [model(ex).sigmoid().tolist()[0][0] for ex in tensors] return results
def __init__(self, hdfs_host: str = None, device: str = 'cpu'): self.device = device self.task = HateSpeech(self.TRAIN_DATA_PATH, (9, 1)) self.raw = HateSpeech(self.RAW_DATA_PATH) config.n_enc_vocab = self.task.max_vocab_indexes['syllable_contents'] config.n_dec_vocab = self.task.max_vocab_indexes['syllable_contents'] ## train_data를 비율로 나눔 self.model = Classification(config) print('can use gpu num = ', torch.cuda.device_count()) if torch.cuda.device_count() > 1: self.model = nn.DataParallel(self.model) self.model.to(self.device) self.loss_fn = nn.BCEWithLogitsLoss( pos_weight=torch.tensor(config.weights[0] / config.weights[1])) self.batch_size = config.batch_size self.__test_iter = None bind_model(self.model)
def __init__(self, model, model_i, n_models, hdfs_host: str = None, device: str = 'cpu'): self.device = device self.task = HateSpeech(self.TRAIN_DATA_PATH, model_i, n_models) self.model = model self.model.to(self.device) self.model_i = model_i self.loss_fn = nn.BCELoss() self.batch_size = BATCH_SIZE self.__test_iter = None bind_model(self.model)
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)
def infer(raw_data, **kwargs): for model in models: model.eval() examples = HateSpeech(raw_data).examples tensors = [torch.tensor(ex.syllable_contents, device='cuda').reshape([-1, 1]) for ex in examples] results = [] for ex in tensors: total = 0 for model in models: # vote total += torch.where(model(ex) > 0.5, torch.tensor(1.0, device='cuda'), torch.tensor(0.0, device='cuda')) total = total / len(models) # len must be an odd number results.append(total.tolist()) return results
def infer(raw_data, **kwargs): model.eval() examples = HateSpeech(raw_data).examples examples = [ex.syllable_contents for ex in examples] loader = torch.utils.data.DataLoader(examples, batch_size=1) results = [] for data in loader: dec_inputs = torch.tensor(data).long().cuda() dec_inputs = dec_inputs.reshape(-1, len(dec_inputs)) index1 = dec_inputs == 1 index0 = dec_inputs == 0 dec_inputs[index1] = 0 dec_inputs[index0] = 1 pred = model(dec_inputs)[1].max(1)[1].tolist() results += pred return results
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)
def infer(raw_data, **kwargs): model.eval() examples = HateSpeech(raw_data).examples tensors = [ torch.tensor(ex.syllable_contents, device='cuda') for ex in examples ] results = [] for tensor in tensors: tensor = tensor.reshape(-1, len(tensor)) index1 = tensor == 1 index0 = tensor == 0 tensor[index1] = 0 tensor[index0] = 1 pred = model( tensor, torch.tensor([[3]] * len(tensor)).cuda())[0].sigmoid()[0][0].tolist() results.append(pred) return results
if __name__ == '__main__': # Constants HIDDEN_DIM = 128 DROPOUT_RATE = 0.3 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()