<jupyter_start><jupyter_text>Modelo Recurrentes con Embeddings a nivel de caracter<jupyter_code>path
df = pd.read_csv(path+'data/acetylcholinesterase_02_bioactivity_data_preprocessed.csv')
df.head()<jupyter_output><empty_output><jupyter_text>Canonical_smiles To secuence<jupyter_code>from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences<jupyter_output><empty_output><jupyter_text>Utilizando Tokenizer y pad_sequences de keras a nivel de caracter, obtener la entrada tokenizada. Por ejemplo:**El smiles**:'C[C@@]12CC[C@H]3[C@]4(C)CCC[C@@]5(C)C(=O)OCC[N+](C)(C)CCCCCCCCC[N+](C)(C)CCOC(=O)[C@]6(C)CCC[C@]7(C)[C@@H]6CC[C@@]68C[C@@H](OC(=O)CCCCCCCCC(=O)O[C@@H]1C[C@@]3(CC[C@@H]45)C2)[C@@](C)(CC[C@@H]76)C8.[Br-].[Br-]'**Tendría que quedar algo asi (puede que no sea exacto):**array([ 2, 11,  2, 13, 13, 12,  5,  7,  2,  2, 11,  2, 13, 16, 12, 10, 11,        2, 13, 12, 15,  3,  2,  4,  2,  2,  2, 11,  2, 13, 13, 12, 23,  3,        2,  4,  2,  3,  9,  6,  4,  6,  2,  2, 11,  8, 21, 12,  3,  2,  4,        3,  2,  4,  2,  2,  2,  2,  2,  2,  2,  2,  2, 11,  8, 21, 12,  3,        2,  4,  3,  2,  4,  2,  2,  6,  2,  3,  9,  6,  4, 11,  2, 13, 12,       30,  3,  2,  4,  2,  2,  2, 11,  2, 13, 12, 35,  3,  2,  4, 11,  2,       13, 13, 16, 12, 30,  2,  2, 11,  2, 13, 13, 12, 30, 36,  2, 11,  2,       13, 13, 16, 12,  3,  6,  2,  3,  9,  6,  4,  2,  2,  2,  2,  2,  2,        2,  2,  2,  3,  9,  6,  4,  6, 11,  2, 13, 13, 16, 12,  5,  2, 11,        2, 13, 13, 12, 10,  3,  2,  2, 11,  2, 13, 13, 16, 12, 15, 23,  4,        2,  7,  4, 11,  2, 13, 13, 12,  3,  2,  4,  3,  2,  2, 11,  2, 13,       13, 16, 12, 35, 30,  4,  2, 36, 20, 11, 25, 26, 17, 12, 20, 11, 25,       26, 17, 12], dtype=int32)       **Otro ejemplo:**smiles: 'NC(=O)O'array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       0, 0, 8, 2, 3, 9, 6, 4, 6], dtype=int32)**Tener en cuenta:**- Ver cuanta es la máxima longitud para agregar el padding correspondiente- En el ejemplo se agrego padding a la izquierda<jupyter_code>max_len_idx = df['canonical_smiles'].apply(len).argmax()
min_len_idx = df['canonical_smiles'].apply(len).argmin()
print('max longitud: ',max_len_idx)
print('min longitud: ',min_len_idx)
long_max_smile  =len(df.iloc[max_len_idx].canonical_smiles)
long_max_smile
#cant de caracteres
# Uno todos las cadenas smile en una sola
text = ''
for cs in df['canonical_smiles']:
    text = text + cs
vocab_hist = dict(Counter(text))
cant_caracteres = len(vocab_hist) +1
X = df['canonical_smiles']
y = df['pIC50']
print('cant. de caracteres de smile: ', cant_caracteres)
print('long. maxima de smile: ', long_max_smile)
# Implementar tokenización y guardar en X_seq_pad el dataset 
tokenizer_inputs = Tokenizer(num_words=cant_caracteres, char_level=True)
tokenizer_inputs.fit_on_texts(df['canonical_smiles'])
df['X_seq'] = tokenizer_inputs.texts_to_sequences(df['canonical_smiles']) #input tokenizado

df['X_seq_pad'] =  pad_sequences(df.X_seq, maxlen=long_max_smile, padding='pre', value=0).tolist()
X_seq_pad = pad_sequences(df.X_seq, maxlen=long_max_smile, padding='pre', value=0).tolist()
y =df['pIC50'].values
#X_seq_pad = df['X_seq_pad'].values
df.head()
df.to_csv('data/acetylcholinesterase_02_bioactivity_data_preprocessed_tokenizada.csv', index=False)<jupyter_output><empty_output><jupyter_text>Split<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_seq_pad, y, test_size=0.2, random_state=42)
len(X_train), len(y_train), len(X_test)<jupyter_output><empty_output><jupyter_text>Network Model<jupyter_code>from tensorflow.keras import Sequential
from tensorflow.keras.layers import LSTM, Dense, Embedding, Bidirectional, Dropout, Activation, BatchNormalization
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.optimizers import Adam, RMSprop
from tensorflow.keras import backend as K
# Métrica

def R2(y_true, y_pred):
    SS_res =  K.sum(K.square( y_true-y_pred ))
    SS_tot = K.sum(K.square( y_true - K.mean(y_true) ) )
    return ( 1 - SS_res/(SS_tot + K.epsilon()) )
# PARAMETROS - HIPER-PARAMETROS
# vocab_size = # Completar largo del vocabulario
vocab_size = cant_caracteres
long_vec_embbeding = 64
max_sequence_len = max_len_idx # Completar
cant_neuronas_lstm = 100
cant_neuronas_hidden_layer = 50<jupyter_output><empty_output><jupyter_text>Armar modelo en keras de LSTMEl de abajo es un ejemplo propuesto pero puede armar otro similar (siempre con LSTM o GRU) Model: "sequential"_________________________________________________________________Layer (type)                 Output Shape              Param    =================================================================embedding (Embedding)        (None, 207, 64)           2496      _________________________________________________________________bidirectional (Bidirectional (None, 200)               132000    _________________________________________________________________dense (Dense)                (None, 50)                10050     _________________________________________________________________batch_normalization (BatchNo (None, 50)                200       _________________________________________________________________activation (Activation)      (None, 50)                0         _________________________________________________________________dense_1 (Dense)              (None, 1)                 51        =================================================================Total params: 144,797Trainable params: 144,697Non-trainable params: 100_________________________________________________________________<jupyter_code>model = Sequential()
# Implementar modelo completo
                    # cant caracteres, #long vect Emb, #max vocabulario   
model.add(Embedding(cant_caracteres, long_vec_embbeding, input_length=long_max_smile))
model.add(Bidirectional(LSTM(cant_neuronas_lstm)))
model.add(Dense(cant_neuronas_hidden_layer))
model.add(BatchNormalization())
model.add( Activation('relu'))
model.add(Dense(1,activation='sigmoid'))
model.summary()
model.compile(optimizer=RMSprop(learning_rate=0.001), loss='mse', metrics=[R2])
mcp = ModelCheckpoint('models/best_model_{epoch}', save_best_only=True, save_format="h5")
history = model.fit(X_train, y_train.tolist(), epochs=100, batch_size=128, validation_data=(X_test, y_test.tolist()))
y_pred = model.predict(X_test)
a = np.array(y_test)
r2 = 1 - ((y_test - y_pred.reshape(-1)) ** 2).sum() / ((y_test - y_test.mean()) ** 2).sum()
r2
# 0.4984533246797399
# model.save('models/embed_64_bi_lstm_100_dense_50_bn.h5', )<jupyter_output><empty_output><jupyter_text>Load model (solo si se grabo el modelo)<jupyter_code>from tensorflow.keras.models import load_model
# model_loaded = load_model('models/embed_64_bi_lstm_100_dense_50_bn.h5', custom_objects={'R2': R2})
y_pred = model_loaded.predict(X_test)
r2 = 1 - ((y_test.values - y_pred.reshape(-1)) ** 2).sum() / ((y_test - y_test.mean()) ** 2).sum()
r2<jupyter_output><empty_output><jupyter_text>¿Hasta que valores de r2 logró?   --> Val_r2 = -8.10  Evaluación<jupyter_code>import seaborn as sns
import matplotlib.pyplot as plt

sns.set(color_codes=True)
sns.set_style("white")

ax = sns.regplot(x=y_test, y=y_pred, scatter_kws={'alpha':0.4})
ax.set_xlabel('Experimental pIC50', fontsize='large', fontweight='bold')
ax.set_ylabel('Predicted pIC50', fontsize='large', fontweight='bold')
ax.set_xlim(0, 12)
ax.set_ylim(0, 12)
ax.figure.set_size_inches(5, 5)
plt.show<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16485529368665283, 'fraction_numerical': 0.08316033703748932, 'mean_word_length': 4.25, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '573298', 'license': 'Apache-2.0', 'n_tokens_mistral': 3802, 'n_tokens_neox': 3294, 'n_words': 822, 'path': '003_train_LSTM.ipynb', 'repo_name': 'GuilleBur/tp_final_bioinformatica', 'size': '8189'}