text_plain_output_1.png
from pytorch_pretrained_bert import BertConfig

from pytorch_pretrained_bert import BertTokenizer, BertForSequenceClassification, BertAdam

from tqdm import tqdm

import numpy as np

import torch

device = torch.device('cuda')

def convert_lines(example, max_seq_length, tokenizer):
    max_seq_length -= 2
    all_tokens = []
    longer = 0
    for text in tqdm(example):
        tokens_a = tokenizer.tokenize(text)
        if len(tokens_a) > max_seq_length:
            tokens_a = tokens_a[:max_seq_length]
            longer += 1
        one_token = tokenizer.convert_tokens_to_ids(['[CLS]'] + tokens_a + ['[SEP]']) + [0] * (max_seq_length - len(tokens_a))
        all_tokens.append(one_token)
    return np.array(all_tokens)

MAX_SEQUENCE_LENGTH = 200
SEED = 42
BATCH_SIZE = 32
BERT_MODEL_PATH = '../input/bert-pretrained-models/uncased_l-12_h-768_a-12/uncased_L-12_H-768_A-12/'
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.backends.cudnn.deterministic = True

bert_config = BertConfig('../input/bertinference/bert_config.json')
tokenizer = BertTokenizer.from_pretrained(BERT_MODEL_PATH, cache_dir=None, do_lower_case=True)
BERT_SMALL_PATH = '../input/bert-pretrained-models/uncased_l-12_h-768_a-12/uncased_L-12_H-768_A-12/'
BERT_LARGE_PATH = '../input/bert-pretrained-models/uncased_l-24_h-1024_a-16/uncased_L-24_H-1024_A-16/'

model = BertForSequenceClassification(bert_config, num_labels=1)
model.load_state_dict(torch.load('../input/bertinference/pytorch_bert_6.bin'))
model.to(device)
for param in model.parameters():
    param.requires_grad = False
model.eval()