<jupyter_start><jupyter_text>Define Bert tokenizer<jupyter_code>tokenizer = BertTokenizer.from_pretrained("bert-base-chinese")
sentence = "今天北京天气出现小雨,山区还出现了降雪,气温下降,体感十分寒冷。"
print("Tokenize")
print(tokenizer.tokenize(sentence))
print("Tokens to ids")
ids = tokenizer.convert_tokens_to_ids(sentence)
print(ids)
print("Ids to tokens")
print(tokenizer.convert_ids_to_tokens(ids))
batch_size = 1
# make sure your model is on GPU
device = torch.device("cuda")

model = TransformerDecoderLM()
model = model.to(device)<jupyter_output><empty_output><jupyter_text>Load weights into the model<jupyter_code>old_state_dict = torch.load("model_state_epoch_62.th", map_location=lambda storage, loc: storage)
new_state_dict = model.state_dict()

for item in new_state_dict.keys():
    new_state_dict[item] = old_state_dict['module.'+item]
    
model.load_state_dict(new_state_dict)<jupyter_output><empty_output><jupyter_text>Conditioanl or Unconditional Decoding<jupyter_code># ask more about news
prompt = tokenizer.tokenize("房价上涨的原因主要有哪些?")
prompt = tokenizer.convert_tokens_to_ids(prompt)
#prompt = []
top_k = 20
temperature = 1.0
length = 0

start_predictions = torch.LongTensor([[101] + prompt]* batch_size).to(device)
mask = torch.ones(batch_size, start_predictions.shape[1]).to(device)

with torch.no_grad():
    # cache saves in past
    logits, past = model(start_predictions, mask, past=None, past_length=0)
    logits = logits[:, -1, :] / temperature
    logits = top_k_logits(logits, k=top_k)

    sentence = []

    probs = F.softmax(logits, dim=-1)
    prob, prev_pred = torch.topk(probs, k=1, dim=-1)
    sentence.append(prev_pred)
    length += 1

    # decoding loop
    for i in range(500):
        mask = F.pad(mask, (0, 1), "constant", 1.0)
        logits, past = model(prev_pred, mask, past=past, past_length=length)
        logits = logits.squeeze(1) / temperature
        logits = top_k_logits(logits, k=top_k)
        probs = F.softmax(logits, dim=-1)
        prev_pred = torch.multinomial(probs, num_samples=1)
        sentence.append(prev_pred)
        length += 1

    sentence = torch.cat(sentence, dim=-1)

"".join(tokenizer.convert_ids_to_tokens(sentence[0].tolist()))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12929475587703435, 'fraction_numerical': 0.014014466546112115, 'mean_word_length': 5.164345403899722, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '590700', 'license': 'MIT', 'n_tokens_mistral': 852, 'n_tokens_neox': 833, 'n_words': 144, 'path': 'tutorials/Decode.ipynb', 'repo_name': 'qywu/Chinese-GPT', 'size': '2212'}