This is a ReactionT5 pre-trained to predict yields of reactions. You can use the demo
here .
Use the code below to get started with the model.
1 import torch
2 import torch . nn as nn
3 from transformers import AutoTokenizer , T5ForConditionalGeneration , AutoConfig , PreTrainedModel
4 import logging
5 logging . getLogger ( 'transformers' ) . setLevel ( logging . ERROR )
6
7 class ReactionT5Yield ( PreTrainedModel ) :
8 config_class = AutoConfig
9 def __init__ ( self , config ) :
10 super ( ) . __init__ ( config )
11 self . config = config
12 self . model = T5ForConditionalGeneration . from_pretrained ( self . config . _name_or_path )
13 self . model . resize_token_embeddings ( self . config . vocab_size )
14 self . fc1 = nn . Linear ( self . config . hidden_size , self . config . hidden_size // 2 )
15 self . fc2 = nn . Linear ( self . config . hidden_size , self . config . hidden_size // 2 )
16 self . fc3 = nn . Linear ( self . config . hidden_size // 2 * 2 , self . config . hidden_size )
17 self . fc4 = nn . Linear ( self . config . hidden_size , self . config . hidden_size )
18 self . fc5 = nn . Linear ( self . config . hidden_size , 1 )
19
20 self . _init_weights ( self . fc1 )
21 self . _init_weights ( self . fc2 )
22 self . _init_weights ( self . fc3 )
23 self . _init_weights ( self . fc4 )
24 self . _init_weights ( self . fc5 )
25
26 def _init_weights ( self , module ) :
27 if isinstance ( module , nn . Linear ) :
28 module . weight . data . normal_ ( mean = 0.0 , std = 0.01 )
29 if module . bias is not None :
30 module . bias . data . zero_ ( )
31 elif isinstance ( module , nn . Embedding ) :
32 module . weight . data . normal_ ( mean = 0.0 , std = 0.01 )
33 if module . padding_idx is not None :
34 module . weight . data [ module . padding_idx ] . zero_ ( )
35 elif isinstance ( module , nn . LayerNorm ) :
36 module . bias . data . zero_ ( )
37 module . weight . data . fill_ ( 1.0 )
38
39 def forward ( self , inputs ) :
40 encoder_outputs = self . model . encoder ( ** inputs )
41 encoder_hidden_states = encoder_outputs [ 0 ]
42 outputs = self . model . decoder ( input_ids = torch . full ( ( inputs [ 'input_ids' ] . size ( 0 ) , 1 ) ,
43 self . config . decoder_start_token_id ,
44 dtype = torch . long ) , encoder_hidden_states = encoder_hidden_states )
45 last_hidden_states = outputs [ 0 ]
46 output1 = self . fc1 ( last_hidden_states . view ( - 1 , self . config . hidden_size ) )
47 output2 = self . fc2 ( encoder_hidden_states [ : , 0 , : ] . view ( - 1 , self . config . hidden_size ) )
48 output = self . fc3 ( torch . hstack ( ( output1 , output2 ) ) )
49 output = self . fc4 ( output )
50 output = self . fc5 ( output )
51 return output * 100
52
53
54 model = ReactionT5Yield . from_pretrained ( 'sagawa/ReactionT5v2-yield' )
55 tokenizer = AutoTokenizer . from_pretrained ( 'sagawa/ReactionT5v2-yield' )
56 inp = tokenizer ( [ 'REACTANT:CC(C)n1ncnc1-c1cn2c(n1)-c1cnc(O)cc1OCC2.CCN(C(C)C)C(C)C.Cl.NC(=O)[C@@H]1C[C@H](F)CN1REAGENT: PRODUCT:O=C(NNC(=O)C(F)(F)F)C(F)(F)F' ] , return_tensors = 'pt' )
57 print ( model ( inp ) ) # tensor([[19.1666]], grad_fn=<MulBackward0>)
We used
Open Reaction Database (ORD) dataset for model training. In addition, we used palladium-catalyzed Buchwald-Hartwig
C-N cross-coupling reactions dataset 's test split to prevent data leakage.
The command used for training is the following. For more information about data preprocessing and training, please refer to the paper and GitHub repository.
1 python train . py \
2 - - train_data_path = '../data/preprocessed_ord_train.csv' \
3 - - valid_data_path = '../data/preprocessed_ord_valid.csv' \
4 - - test_data_path = '../data/preprocessed_ord_test.csv' \
5 - - CN_test_data_path = '../data/C_N_yield/MFF_Test1/test.csv' \
6 - - epochs = 100 \
7 - - batch_size = 32 \
8 - - output_dir = './'
@article{Sagawa2025,
title = {ReactionT5: a pre-trained transformer model for accurate chemical reaction prediction with limited data},
author = {Sagawa, Tatsuya and Kojima, Ryosuke},
journal = {Journal of Cheminformatics},
year = {2025},
volume = {17},
number = {1},
pages = {126},
doi = {10.1186/s13321-025-01075-4},
url = {https://doi.org/10.1186/s13321-025-01075-4}
}