Views
No views yet
sentence-transformers (recommended), or
transformers if you want to work with the Hugging Face pipeline:pip install -U sentence-transformers transformers torchSentenceTransformer. This interface
handles tokenization, padding, batching, pooling, device placement, and
conversion to NumPy arrays.1from sentence_transformers import SentenceTransformer
2
3model_id = "CremaX/PoCo"
4model = SentenceTransformer(model_id)
5
6polymer_smiles = [
7 "[*]CC[*]",
8 "[*]CC(c1ccccc1)[*]",
9]
10
11embeddings = model.encode(
12 polymer_smiles,
13 batch_size=64,
14 convert_to_numpy=True,
15 show_progress_bar=True,
16)
17
18print(embeddings.shape)
19# (2, 512)1embedding = model.encode("[*]CC[*]", convert_to_numpy=True)
2
3print(embedding.shape)
4# (512,)embeddings = model.encode(polymer_smiles, normalize_embeddings=True)1from sklearn.ensemble import RandomForestRegressor
2from sentence_transformers import SentenceTransformer
3
4model = SentenceTransformer("CremaX/PoCo")
5
6X_train = model.encode(train_smiles, convert_to_numpy=True)
7X_test = model.encode(test_smiles, convert_to_numpy=True)
8
9regressor = RandomForestRegressor(random_state=0)
10regressor.fit(X_train, y_train)
11predictions = regressor.predict(X_test)transformers. This is useful when
you need full control over tokenization, tensors, devices, or pooling.AutoModel returns token-level hidden states with shape
(batch_size, sequence_length, hidden_size). To get one 512-dimensional vector
per polymer, apply attention-mask-aware mean pooling over the token dimension.1import torch
2from transformers import AutoModel, AutoTokenizer
3
4model_id = "CremaX/PoCo"
5device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
6
7tokenizer = AutoTokenizer.from_pretrained(model_id)
8model = AutoModel.from_pretrained(model_id).to(device)
9model.eval()
10
11polymer_smiles = [
12 "[*]CC[*]",
13 "[*]CC(c1ccccc1)[*]",
14]
15
16encoded = tokenizer(
17 polymer_smiles,
18 padding=True,
19 truncation=True,
20 return_tensors="pt",
21)
22encoded = {key: value.to(device) for key, value in encoded.items()}
23
24with torch.no_grad():
25 outputs = model(**encoded)
26
27token_embeddings = outputs.last_hidden_state
28attention_mask = encoded["attention_mask"].unsqueeze(-1).float()
29
30# mean pooling
31embeddings = (token_embeddings * attention_mask).sum(dim=1)
32embeddings = embeddings / attention_mask.sum(dim=1).clamp(min=1e-9)
33embeddings = embeddings.cpu().numpy()
34
35print(embeddings.shape)
36# (2, 512)SentenceTransformer example above or
apply the mean pooling step shown in this section.[*] to mark repeat-unit endpoints, not bare *.psmiles library before passing them to the model.1@article{wang2026poco,
2 title = {Contrastive representation learning for polymer informatics},
3 author = {Wang, Lida and Long, Donghui},
4 journal = {ChemRxiv},
5 year = {2026},
6 doi = {10.26434/chemrxiv.15003645/v1}
7}