-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsample_sequences.py
More file actions
75 lines (62 loc) · 1.87 KB
/
Copy pathsample_sequences.py
File metadata and controls
75 lines (62 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import torch
from tqdm import tqdm
from transformers import AutoTokenizer
from SCISOR.shortening_scud import ShorteningSCUD
from SCISOR.trainer import get_text
path = "https://huggingface.co/SCISOR/SCISOR/resolve/main/SCISOR_U90_S.ckpt"
batch_size = 32
n_samples = 10
sample_lengths = 100
num_steps = 10
temperature = 1
K = 0
r = 1
save_file_path = "sampled_sequences.fasta"
def get_sequences_from_model(
sample_x, model, gen_trans_step, batch_size, temperature=1, K=0, r=1
):
last_text, _ = get_text(
sample_x,
None,
model,
gen_trans_step,
batch_size,
tokenizer,
temperature=temperature,
K=K,
r=r,
)
last_text = [
s.replace("<cls>", "").replace("<eos>", "").replace("<pad>", "")
for s in last_text
]
return last_text
device = "cuda" if torch.cuda.is_available() else "cpu"
model = ShorteningSCUD.load_from_checkpoint(path)
model.to(device)
model.eval()
model.p0 = torch.load("p0.pt")
rate = 1 / 1.1
model.alpha = lambda t: (1 - t) ** rate
model.beta = lambda t: rate / (1 - t)
tokenizer = AutoTokenizer.from_pretrained("facebook/esm2_t6_8M_UR50D")
sample_x = torch.full((n_samples, sample_lengths + 2), 4, device=device)
sample_x[:, 0] = model.tokenizer.cls_token_id
sample_x[:, -1] = model.tokenizer.eos_token_id
sampled_sequences = []
for i in tqdm(range(0, len(sample_x), batch_size)):
x = sample_x[i : i + batch_size]
sequences = get_sequences_from_model(
x,
model,
gen_trans_step=num_steps,
batch_size=x.shape[0],
temperature=temperature,
K=K,
r=r,
)
sampled_sequences.extend(sequences)
padding = len(str(len(sampled_sequences))) # Number of digits needed
with open(save_file_path, "w") as fasta_file:
for i, seq in enumerate(sampled_sequences):
fasta_file.write(f">{str(i).zfill(padding)}\n{seq}\n")