Discrepancy in Returned Values for the Same SMILES Input #3

Open
opened 2025-10-14 16:10:45 -06:00 by navan · 0 comments
Owner

Originally created by @poinco-gogo on 6/30/2023

Dear developers,

Thank you for sharing the source code. As mentioned in the title, upon executing the provided code, I observed that different values are returned for the same SMILES input. Would it be reasonable to consider this behavior as expected?

the code:

import torch
from build_vocab import WordVocab
from pretrain_trfm import TrfmSeq2seq
from utils import split
from transformers import T5EncoderModel, T5Tokenizer
import re
import gc
import numpy as np
import pandas as pd
import pickle
import math


def smiles_to_vec(Smiles):
    pad_index = 0
    unk_index = 1
    eos_index = 2
    sos_index = 3
    mask_index = 4
    vocab = WordVocab.load_vocab('vocab.pkl')
    def get_inputs(sm):
        seq_len = 220
        sm = sm.split()
        if len(sm)>218:
            print('SMILES is too long ({:d})'.format(len(sm)))
            sm = sm[:109]+sm[-109:]
        ids = [vocab.stoi.get(token, unk_index) for token in sm]
        ids = [sos_index] + ids + [eos_index]
        seg = [1]*len(ids)
        padding = [pad_index]*(seq_len - len(ids))
        ids.extend(padding), seg.extend(padding)
        return ids, seg
    def get_array(smiles):
        x_id, x_seg = [], []
        for sm in smiles:
            a,b = get_inputs(sm)
            x_id.append(a)
            x_seg.append(b)
        return torch.tensor(x_id), torch.tensor(x_seg)
    trfm = TrfmSeq2seq(len(vocab), 256, len(vocab), 4)
    trfm.load_state_dict(torch.load('trfm_12_23000.pkl'))
    trfm.eval()
    x_split = [split(sm) for sm in Smiles]
    xid, xseg = get_array(x_split)
    print(f"xid: {xid}") # <-- ここまで同じ値
    X = trfm.encode(torch.t(xid))
    print(f"X: {X}") # <-- 違う値になってる
    return X


Smiles = ['CC']*2
smiles_vec = smiles_to_vec(Smiles)

the output is:

xid: tensor([[3, 6, 6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0],
        [3, 6, 6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0]])
X: [[-0.08155493 -0.3144304   0.34284085 ...  1.5726329   0.47515997
   0.67139834]
 [-0.07930924 -0.3067594   0.3409212  ...  1.577628    0.4822625
   0.6750506 ]]

Here is the computational environment I used:
python 3.6.5
numpy 1.19.5
torch 1.10.1+cu113
nvidia V100

I apologize if I have overlooked anything as I am not extensively familiar with transformer architecture.

Sincerely,

*Originally created by @poinco-gogo on 6/30/2023* Dear developers, Thank you for sharing the source code. As mentioned in the title, upon executing the provided code, I observed that different values are returned for the same SMILES input. Would it be reasonable to consider this behavior as expected? the code: ``` import torch from build_vocab import WordVocab from pretrain_trfm import TrfmSeq2seq from utils import split from transformers import T5EncoderModel, T5Tokenizer import re import gc import numpy as np import pandas as pd import pickle import math def smiles_to_vec(Smiles): pad_index = 0 unk_index = 1 eos_index = 2 sos_index = 3 mask_index = 4 vocab = WordVocab.load_vocab('vocab.pkl') def get_inputs(sm): seq_len = 220 sm = sm.split() if len(sm)>218: print('SMILES is too long ({:d})'.format(len(sm))) sm = sm[:109]+sm[-109:] ids = [vocab.stoi.get(token, unk_index) for token in sm] ids = [sos_index] + ids + [eos_index] seg = [1]*len(ids) padding = [pad_index]*(seq_len - len(ids)) ids.extend(padding), seg.extend(padding) return ids, seg def get_array(smiles): x_id, x_seg = [], [] for sm in smiles: a,b = get_inputs(sm) x_id.append(a) x_seg.append(b) return torch.tensor(x_id), torch.tensor(x_seg) trfm = TrfmSeq2seq(len(vocab), 256, len(vocab), 4) trfm.load_state_dict(torch.load('trfm_12_23000.pkl')) trfm.eval() x_split = [split(sm) for sm in Smiles] xid, xseg = get_array(x_split) print(f"xid: {xid}") # <-- ここまで同じ値 X = trfm.enc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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) X: [[-0.08155493 -0.3144304 0.34284085 ... 1.5726329 0.47515997 0.67139834] [-0.07930924 -0.3067594 0.3409212 ... 1.577628 0.4822625 0.6750506 ]] ``` Here is the computational environment I used: python 3.6.5 numpy 1.19.5 torch 1.10.1+cu113 nvidia V100 I apologize if I have overlooked anything as I am not extensively familiar with transformer architecture. Sincerely,
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github/smiles-transformer#3
No description provided.