Views
No views yet
i only uses the inputs from 1 to i but not the future tokens.1from transformers import GPT2Tokenizer, GPT2LMHeadModel
2import torch
3
4tokenizer = GPT2Tokenizer.from_pretrained("lchaloupsky/czech-gpt2-oscar")
5model = GPT2LMHeadModel.from_pretrained("lchaloupsky/czech-gpt2-oscar")
6
7# Get sequence length max of 1024
8tokenizer.model_max_length=1024
9# For older versions of the 'transformers' library use this
10# tokenizer.max_len=1024
11
12model.eval() # disable dropout (or leave in train mode to finetune)1# input sequence
2text = "Univerzita je základem"
3inputs = tokenizer(text, return_tensors="pt")
4
5# model output
6outputs = model(**inputs, labels=inputs["input_ids"])
7loss, logits = outputs[:2]
8predicted_index = torch.argmax(logits[0, -1, :]).item()
9predicted_text = tokenizer.decode([predicted_index])
10
11# results
12print('input text:', text)
13print('predicted text:', predicted_text)1# input sequence
2text = "Univerzita je základem"
3inputs = tokenizer(text, return_tensors="pt") # tokenizer.encode(text, return_tensors="pt") directly for input_ids
4
5# model output using Top-k sampling text generation method
6sample_outputs = model.generate(inputs.input_ids,
7 pad_token_id=50256,
8 do_sample=True,
9 max_length=50, # put the token number you want
10 top_k=40,
11 num_return_sequences=1)
12
13# generated sequence
14for i, sample_output in enumerate(sample_outputs):
15 print("{}\n\n{}".format(i+1, tokenizer.decode(sample_output.tolist()))) # tokenizer.decode(sample_output, skip_special_tokens=True)1from transformers import GPT2Tokenizer, TFGPT2LMHeadModel
2import tensorflow as tf
3
4tokenizer = GPT2Tokenizer.from_pretrained("lchaloupsky/czech-gpt2-oscar")
5model = TFGPT2LMHeadModel.from_pretrained("lchaloupsky/czech-gpt2-oscar")
6
7# Get sequence length max of 1024
8tokenizer.model_max_length=1024
9# For older versions of the 'transformers' library use this
10# tokenizer.max_len=1024
11
12model.eval() # disable dropout (or leave in train mode to finetune)1# input sequence
2text = "Univerzita je základem"
3input_ids = tokenizer.encode(text, return_tensors="tf")
4
5# model output using Top-k sampling text generation method
6outputs = model.generate(input_ids, eos_token_id=50256, pad_token_id=50256,
7 do_sample=True,
8 max_length=40,
9 top_k=40)
10print(tokenizer.decode(outputs[0])) # tokenizer.decode(outputs[0], skip_special_tokens=True)Because large-scale language models like GPT-2 do not distinguish fact from fiction, we don’t support use-cases that require the generated text to be true. Additionally, language models like GPT-2 reflect the biases inherent to the systems they were trained on, so we do not recommend that they be deployed into systems that interact with humans > unless the deployers first carry out a study of biases relevant to the intended use-case. We found no statistically significant difference in gender, race, and religious bias probes between 774M and 1.5B, implying all versions of GPT-2 should be approached with similar levels of caution around use cases that are sensitive to biases around human attributes.
@article{chaloupsky2022automatic,
title={Automatic generation of medical reports from chest X-rays in Czech},
author={Chaloupsk{\`y}, Luk{\'a}{\v{s}}},
year={2022},
publisher={Charles University, Faculty of Mathematics and Physics}
}