<jupyter_start><jupyter_text>Model<jupyter_code>rna = 'trna'
alphabet = ['A', 'T', 'G', 'C']
alphabet_size = len(alphabet)
metadata_folder = f'data/rna_temp/tab/{rna}.tab'
sequences_folder = 'data/rna_temp/seq/'
classes = ['psychrophile', 'mesophile', 'thermophile', 'hyperthermophile']
n_classes = len(classes)
n_entries_per_class = 153
output_path = os.path.join(os.getcwd(), 'saved_models', f'seed_{seed}', f'{rna}_classification.h5')

model = rnn_classification_model(alphabet_size=alphabet_size, n_classes=n_classes, n_lstm=2)
compile_classification_model(model, learning_rate=1e-4)

model.load_weights(output_path)

model.summary()<jupyter_output>Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
sequence (InputLayer)        [(None, None, 4)]         0         
_________________________________________________________________
masking (Masking)            (None, None, 4)           0         
_________________________________________________________________
lstm (LSTM)                  (None, None, 100)         42000     
_________________________________________________________________
lstm_1 (LSTM)                (None, 100)               80400     
_________________________________________________________________
dense (Dense)                (None, 100)               10100     
_________________________________________________________________
dropout (Dropout)            (None, 100)               0         
_____________________________________________________________[...]<jupyter_text>Data<jupyter_code>%%time
metadata = pd.read_csv(metadata_folder, delimiter='\t')
metadata['category'] = metadata['temp.cat']

sequences = load_rna_nucleotides_dataset(metadata, sequences_folder)
y_balanced, balanced_metadata = make_dataset_balanced(metadata, n_entries_per_class=n_entries_per_class)
balanced_sequences = load_rna_nucleotides_dataset(balanced_metadata, sequences_folder)
y = one_hot_encode_classes(y_balanced, classes)
x = sequence_embedding(balanced_sequences, alphabet)
x_train, y_train, x_test, y_test, train_idx, test_idx = split_train_test_set(
    x, y, test_ratio=0.2, return_indices=True)<jupyter_output><empty_output><jupyter_text>Evaluation<jupyter_code>loss, accuracy = model.evaluate(x_test, y_test, verbose=0)
print(f'Loss: {loss:.4f}')
print(f'Accuracy: {accuracy:.4f}')<jupyter_output>Loss: 1.2486
Accuracy: 0.4098<jupyter_text>Confusion matrix (test set)<jupyter_code>y_pred = model(x_test)

labels = [np.argmax(el) for el in y_test]
predictions = [np.argmax(el) for el in y_pred]
conf = tf.math.confusion_matrix(labels, predictions).numpy()
conf
df_cm = pd.DataFrame(
    conf, 
    index=[f'{c}: {i}' for i, c in enumerate(classes)],
    columns=[f'{i}' for i, c in enumerate(classes)],
)
ax = sns.heatmap(df_cm, cmap="Purples");
ax.set_xlabel('Predictions');
ax.set_ylabel('Labels');
conf_sum = np.sum(conf, axis=1)
for i in range(len(classes)):
    acc = 100 * conf[i, i] / conf_sum[i]
    print(f'Accuracy for class {classes[i]}: {acc:.1f}%')<jupyter_output>Accuracy for class psychrophile: 65.4%
Accuracy for class mesophile: 16.1%
Accuracy for class thermophile: 25.0%
Accuracy for class hyperthermophile: 65.5%<jupyter_text>Confusion matrix (train set)<jupyter_code>y_pred_t = model(x_train)

labels_t = [np.argmax(el) for el in y_train]
predictions_t = [np.argmax(el) for el in y_pred_t]
conf_t = tf.math.confusion_matrix(labels_t, predictions_t).numpy()
conf_t
df_cm_t = pd.DataFrame(
    conf_t, 
    index=[f'{c}: {i}' for i, c in enumerate(classes)],
    columns=[f'{i}' for i, c in enumerate(classes)],
)
ax = sns.heatmap(df_cm_t, cmap="Purples");
ax.set_xlabel('Predictions');
ax.set_ylabel('Labels');
conf_sum_t = np.sum(conf_t, axis=1)
for i in range(len(classes)):
    acc = 100 * conf_t[i, i] / conf_sum_t[i]
    print(f'Accuracy for class {classes[i]}: {acc:.1f}%')<jupyter_output>Accuracy for class psychrophile: 71.7%
Accuracy for class mesophile: 21.3%
Accuracy for class thermophile: 26.5%
Accuracy for class hyperthermophile: 68.5%<jupyter_text>VAE<jupyter_code>prior, encoder, decoder, vae = variational_autoencoder(n_inputs=100, encoding_size=2, n_hidden=300)
compile_vae(vae, learning_rate=1e-4)
vae.summary()
layer_name = 'dense'
intermediate_layer_model = tf.keras.Model(
    inputs=model.input,
    outputs=model.get_layer(layer_name).output,
)
train_logits = intermediate_layer_model(x_train).numpy()
test_logits = intermediate_layer_model(x_test).numpy()
print(train_logits.shape, test_logits.shape)
do_train_vae = False
vae_model_path = os.path.join(os.getcwd(), 'saved_models', f'seed_{seed}', f'{rna}_vae.h5')

if do_train_vae:
    vae.fit(
        train_logits, train_logits,
        validation_data=(test_logits, test_logits),
        batch_size=32,
        epochs=200,
    )
    vae.save(vae_model_path)
else:
    vae.load_weights(vae_model_path)
encoding_test_dist = encoder(test_logits)
encoding_test = encoding_test_dist.mean().numpy()
encoding_test.shape
temp_test = balanced_metadata.iloc[test_idx]['temp'].values
temp_test
def plot_encoding(encoding, labels, figsize=(12, 6), show_only=None):
    f, ax = plt.subplots(1, 1, figsize=figsize)
    
    palette = sns.color_palette()
    
    for i, code in enumerate(encoding):
        if show_only is not None and labels[i] not in show_only:
            continue
        ax.plot(code[0], code[1], 'o', color=palette[labels[i]])
    
    return f, ax
plot_encoding(encoding_test, labels, show_only=(0, 3));
def plot_encoding_with_colors(encoding, labels, color_vals, figsize=(12, 6), show_only=None):
    f, ax = plt.subplots(1, 1, figsize=figsize)
    
    palette = sns.color_palette()
    
    x_d, y_d = [], []
    colors = []
    
    for i, code in enumerate(encoding):
        if show_only is not None and labels[i] not in show_only:
            continue
        
        x_d.append(encoding[i,0])
        y_d.append(encoding[i,1])
        colors.append(color_vals[i])
    
    sc = ax.scatter(x_d, y_d, c=colors, cmap='YlOrRd')
    plt.colorbar(sc)
    
    return f, ax
plot_encoding_with_colors(encoding_test, labels, temp_test, show_only=(0, 3));<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1330574566289989, 'fraction_numerical': 0.019417475728155338, 'mean_word_length': 4.883895131086143, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '483920', 'license': 'MIT', 'n_tokens_mistral': 2306, 'n_tokens_neox': 2144, 'n_words': 410, 'path': 'notebook/Classification (tRNA nucleotides).ipynb', 'repo_name': 'srom/rna_learn', 'size': '6283'}