File size: 1,275 Bytes
1928da3 4a70cd7 1928da3 790a052 1928da3 |
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 |
---
license: apache-2.0
library_name: transformers
pipeline_tag: feature-extraction
tags:
- chemistry
- transformers
---
# selfies-ted
selfies-ted is an transformer based encoder decoder model for molecular representations using SELFIES.
![selfies-ted](selfies-ted.png)
## Usage
### Import
```
from transformers import AutoTokenizer, AutoModel
import selfies as sf
import torch
```
### Load the model and tokenizer
```
tokenizer = AutoTokenizer.from_pretrained("ibm/materials.selfies-ted")
model = AutoModel.from_pretrained("ibm/materials.selfies-ted")
```
### Encode SMILES strings to selfies
```
smiles = "c1ccccc1"
selfies = sf.encoder(smiles)
selfies = selfies.replace("][", "] [")
```
### Get embedding
```
token = tokenizer(selfies, return_tensors='pt', max_length=128, truncation=True, padding='max_length')
input_ids = token['input_ids']
attention_mask = token['attention_mask']
outputs = model.encoder(input_ids=input_ids, attention_mask=attention_mask)
model_output = outputs.last_hidden_state
input_mask_expanded = attention_mask.unsqueeze(-1).expand(model_output.size()).float()
sum_embeddings = torch.sum(model_output * input_mask_expanded, 1)
sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9)
model_output = sum_embeddings / sum_mask
```
|