Views
No views yet
pip install torch transformers[torch] biopython datasets pandas numpy scipy seaborn matplotlib jupyter notebooktransformers library.1from transformers import GPT2LMHeadModel, AutoTokenizer, pipeline
2import torch
3
4# Load model and tokenizer
5model = GPT2LMHeadModel.from_pretrained("jinyuan22/promogen2-base")
6tokenizer = AutoTokenizer.from_pretrained("jinyuan22/promogen2-base")
7
8# Set device (CPU or GPU)
9device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10pipe = pipeline("text-generation", model=model, device=device, tokenizer=tokenizer)text-generation pipeline to generate sequences based on an input sequence and various parameters such as sampling temperature, repetition penalty, and top-p sampling. Customize the input sequence (txt), number of sequences, and sampling parameters.1# Define input text and generation parameters
2txt = "<|bos|>5"
3num_return_sequences = 5
4batch_size = 2
5max_new_tokens = 50
6repetition_penalty = 1.2
7top_p = 0.9
8temperature = 0.7
9do_sample = True
10
11# Generate sequences
12all_outputs = []
13for i in range(0, num_return_sequences, batch_size):
14 outputs = pipe(
15 txt,
16 num_return_sequences=batch_size,
17 max_new_tokens=max_new_tokens,
18 repetition_penalty=repetition_penalty,
19 top_p=top_p,
20 temperature=temperature,
21 do_sample=do_sample
22 )
23 all_outputs.extend(outputs)score) evaluates each generated sequence. It calculates the sequence's likelihood under the model, based on the provided tag (or none if no tag is used).1@torch.no_grad()
2def score(seq, tag="none"):
3 # Format input with specified tag
4 if tag == "none":
5 inputs = tokenizer(f"<|bos|>5{seq}3<|eos|>", return_tensors="pt")
6 else:
7 inputs = tokenizer(f"<|bos|>{tag}5{seq}3{tag}<|eos|>", return_tensors="pt")
8 inputs.to(device)
9 input_ids = inputs['input_ids'].to(device)
10 attention_mask = inputs['attention_mask'].to(device)
11 pred = model(input_ids=input_ids, attention_mask=attention_mask, labels=input_ids)
12 return pred['loss'].item()score function. Each sequence and its score are saved to an output file.1# Post-process generated sequences
2tag = "none"
3seqs = [output["generated_text"].replace("<|bos|>", "").replace("5", "").replace("3", "").replace(tag, "") for output in all_outputs]
4scores = [score(seq, tag) for seq in seqs]
5
6# Save sequences and scores
7with open("output.txt", "w") as f:
8 for i, (seq, score) in enumerate(zip(seqs, scores)):
9 f.write(f">{i}|score={score}\n{seq}\n")"none" if no specific tag is used)True for sampling-based generation