From 7eb1cef658d083d664234f2fed5073e9ee6672b1 Mon Sep 17 00:00:00 2001 From: evaklimentova Date: Wed, 2 Oct 2024 12:01:36 +0200 Subject: [PATCH 01/20] Sync with main --- code/machine_learning/encode/README.md | 12 +- .../encode/binding_2D_matrix_encoder.py | 125 +++-- .../train/CNN_miRBind_2022/training.ipynb | 442 ------------------ .../train/CNN_miRBind_2022/training.py | 204 ++++++++ 4 files changed, 308 insertions(+), 475 deletions(-) delete mode 100644 code/machine_learning/train/CNN_miRBind_2022/training.ipynb create mode 100644 code/machine_learning/train/CNN_miRBind_2022/training.py diff --git a/code/machine_learning/encode/README.md b/code/machine_learning/encode/README.md index 79fe152..8179719 100644 --- a/code/machine_learning/encode/README.md +++ b/code/machine_learning/encode/README.md @@ -1 +1,11 @@ -# Encoding the dataset into inner representation \ No newline at end of file +# Encoding the dataset into inner representation + +### [Binding 2D matrix encoder](binding_2d_matrix_encoder.py) + The encoder is based on the "miRBind: A deep learning method for miRNA binding classification." (2022) https://doi.org/10.3390/genes13122323 + with original python implementation here: https://github.com/ML-Bioinfo-CEITEC/miRBind + +Encodes miRNA and gene sequences into 2D-binding matrix. +2D-binding matrix has shape (gene_max_len=50, miRNA_max_len=20, 1) and contains 1 for Watson-Crick interactions and 0 otherwise. + +Outputs npy file with encoded matrices and npy file with corresponding labels. + diff --git a/code/machine_learning/encode/binding_2D_matrix_encoder.py b/code/machine_learning/encode/binding_2D_matrix_encoder.py index 70b6360..480e524 100644 --- a/code/machine_learning/encode/binding_2D_matrix_encoder.py +++ b/code/machine_learning/encode/binding_2D_matrix_encoder.py @@ -1,37 +1,98 @@ -class miRBindEncoder(): +import pandas as pd +import numpy as np +import argparse +import time + + +def binding_encoding(df, alphabet, tensor_dim=(50, 20, 1)): """ - Based on Klimentová, Eva, et al. "miRBind: A deep learning method for miRNA binding classification." Genes 13.12 (2022): 2323. https://doi.org/10.3390/genes13122323. - Python implementation: https://github.com/ML-Bioinfo-CEITEC/miRBind + Transform input sequence pairs to a binding matrix with corresponding labels. - Encodes miRNA and gene sequences into 2D-binding matrix. - 2D-binding matrix has shape (gene_max_len, miRNA_max_len, 1) and contains 1 for Watson-Crick interactions and 0 otherwise. - Returns array with shape (num_of_samples, gene_max_len, miRNA_max_len, 1). + Parameters: + - df: Pandas DataFrame with columns "noncodingRNA", "gene", "label" + - alphabet: dictionary with letter tuples as keys and 1s when they bind + - tensor_dim: 2D binding matrix shape + + Output: + 2D binding matrix, labels as np array + """ + labels = df["label"].to_numpy() + + # Initialize dot matrix with zeros + ohe_matrix_2d = np.zeros((len(df), *tensor_dim), dtype="float32") + + df = df.reset_index(drop=True) + + # Compile matrix with Watson-Crick interactions + for index, row in df.iterrows(): + for bind_index, bind_nt in enumerate(row['gene'].upper()): + for ncrna_index, ncrna_nt in enumerate(row['noncodingRNA'].upper()): + if ncrna_index >= tensor_dim[1]: + break + base_pairs = bind_nt + ncrna_nt + ohe_matrix_2d[index, bind_index, ncrna_index, 0] = alphabet.get(base_pairs, 0) + + return ohe_matrix_2d, labels + +def encode_large_tsv_to_numpy(tsv_file_path, data_output_path, labels_output_path, chunk_size=10000): + """ + Encode a large TSV file into a NumPy matrix using chunk processing. + + Parameters: + - tsv_file_path: Path to the TSV file with dataset. + - data_output_path: Path to the output data .npy file. + - labels_output_path: Path to the output labels .npy file. + - chunk_size: Number of rows to process at a time. """ + # Alphabet for Watson-Crick interactions + alphabet = {"AT": 1., "TA": 1., "GC": 1., "CG": 1.} + tensor_dim = (50, 20, 1) - def __call__(self, df, miRNA_col="noncodingRNA", gene_col="gene", tensor_dim=(50, 20, 1)): - return self.binding_encoding(df, miRNA_col, gene_col, tensor_dim) + # Get total number of rows in the dataset + num_rows = sum(len(df) for df in pd.read_csv(tsv_file_path, sep='\t', usecols=[0], chunksize=chunk_size)) - def binding_encoding(self, df, miRNA_col, gene_col, tensor_dim): - """ - fun encodes miRNAs and mRNAs in df into binding matrices - :param df: dataframe containing gene_col and miRNA_col columns - :param tensor_dim: output shape of the matrix. If sequences are longer than tensor_dim, they will be truncated. - :return: 2D binding matrix with shape (N, *tensor_dim) - """ - - # alphabet for watson-crick interactions. - alphabet = {"AT": 1., "TA": 1., "GC": 1., "CG": 1., "AU": 1., "UA": 1.} - # create empty main 2d matrix array - N = df.shape[0] # number of samples in df - shape_matrix_2d = (N, *tensor_dim) # 2d matrix shape - # initialize dot matrix with zeros - ohe_matrix_2d = np.zeros(shape_matrix_2d, dtype="float32") - - # compile matrix with watson-crick interactions. - for index, row in df.iterrows(): - for bind_index, bind_nt in enumerate(row[gene_col][:tensor_dim[0]].upper()): - for mirna_index, mirna_nt in enumerate(row[miRNA_col][:tensor_dim[1]].upper()): - base_pairs = bind_nt + mirna_nt - ohe_matrix_2d[index, bind_index, mirna_index, 0] = alphabet.get(base_pairs, 0) - - return ohe_matrix_2d \ No newline at end of file + # Determine the shape of the output arrays + labels_shape = (num_rows,) + data_shape = (num_rows, *tensor_dim) + + # Create memory-mapped files + ohe_matrix_2d = np.memmap(data_output_path, dtype='float32', mode='w+', shape=data_shape) + labels = np.memmap(labels_output_path, dtype='float32', mode='w+', shape=labels_shape) + + row_offset = 0 + + # Process each chunk + for chunk in pd.read_csv(tsv_file_path, sep='\t', chunksize=chunk_size): + encoded_data, encoded_labels = binding_encoding(chunk, alphabet, tensor_dim) + + # Write the chunk's data and labels to the memory-mapped files + ohe_matrix_2d[row_offset:row_offset + len(chunk)] = encoded_data + labels[row_offset:row_offset + len(chunk)] = encoded_labels + row_offset += len(chunk) + + # Flush changes to disk + ohe_matrix_2d.flush() + labels.flush() + +def main(): + """ + Based on "miRBind: A deep learning method for miRNA binding classification." Genes 13.12 (2022): 2323. https://doi.org/10.3390/genes13122323. + Original implementation: https://github.com/ML-Bioinfo-CEITEC/miRBind + + Encodes miRNA and gene sequences into 2D-binding matrix. + 2D-binding matrix has shape (gene_max_len=50, miRNA_max_len=20, 1) and contains 1 for Watson-Crick interactions and 0 otherwise. + """ + + parser = argparse.ArgumentParser(description="Encode dataset to miRNA x target binding matrix. Outputs numpy file with matrices and and numpy file with corresponding labels. Expected columns of the dataset are 'noncodingRNA', 'gene' and 'label'") + parser.add_argument('--i_file', type=str, required=True, help="Input dataset file name") + parser.add_argument('--o_prefix', type=str, required=True, help="Output file name prefix") + args = parser.parse_args() + + start = time.time() + encode_large_tsv_to_numpy(args.i_file, args.o_prefix + '_dataset.npy', args.o_prefix + '_labels.npy') + end = time.time() + + print("Elapsed time: ", end - start, " s.") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/code/machine_learning/train/CNN_miRBind_2022/training.ipynb b/code/machine_learning/train/CNN_miRBind_2022/training.ipynb deleted file mode 100644 index cf24ebf..0000000 --- a/code/machine_learning/train/CNN_miRBind_2022/training.ipynb +++ /dev/null @@ -1,442 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "Training.ipynb", - "provenance": [], - "collapsed_sections": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } - }, - "cells": [ - { - "cell_type": "code", - "metadata": { - "id": "T6BIHgU38o2f" - }, - "source": [ - "import pandas as pd\n", - "import numpy as np\n", - "import tensorflow as tf\n", - "from tensorflow import keras as K\n", - "import matplotlib.pyplot as plt\n", - "from matplotlib.pyplot import figure\n", - "from tensorflow.keras.layers import (\n", - " BatchNormalization, LeakyReLU,\n", - " Input, Dense, Conv2D,\n", - " MaxPooling2D, Flatten, Dropout)\n", - "from tensorflow.keras.optimizers import Adam" - ], - "execution_count": 1, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "EfIQCQj2r3fV", - "outputId": "26867200-18e8-4b4e-d34d-129c7203f694" - }, - "source": [ - "!wget https://raw.githubusercontent.com/ML-Bioinfo-CEITEC/miRBind/main/Datasets/train_set_1_10_CLASH2013_paper.tsv" - ], - "execution_count": 2, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "--2022-04-24 17:22:50-- https://raw.githubusercontent.com/ML-Bioinfo-CEITEC/miRBind/main/Datasets/train_set_1_10_CLASH2013_paper.tsv\n", - "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...\n", - "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.\n", - "HTTP request sent, awaiting response... 200 OK\n", - "Length: 12518906 (12M) [text/plain]\n", - "Saving to: ‘train_set_1_10_CLASH2013_paper.tsv’\n", - "\n", - "train_set_1_10_CLAS 100%[===================>] 11.94M --.-KB/s in 0.09s \n", - "\n", - "2022-04-24 17:22:51 (130 MB/s) - ‘train_set_1_10_CLASH2013_paper.tsv’ saved [12518906/12518906]\n", - "\n" - ] - } - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "ALmjW7vd9u60" - }, - "source": [ - "def binding_encoding(df, tensor_dim=(50,20,1)):\n", - " \"\"\"\n", - " fun transform input database to numpy array.\n", - " \n", - " parameters:\n", - " df = Pandas df with col names \"noncodingRNA\", \"gene\", \"label\"\n", - " tensor_dim = 2d matrix shape\n", - " \n", - " output:\n", - " 2d dot matrix, labels as np array\n", - " \"\"\"\n", - " df.reset_index(inplace=True, drop=True)\n", - "\n", - " # alphabet for watson-crick interactions.\n", - " alphabet = {\"AT\": 1., \"TA\": 1., \"GC\": 1., \"CG\": 1.} \n", - "\n", - " # labels to one hot encoding\n", - " labels = df[\"label\"].to_numpy()\n", - "\n", - " # create empty main 2d matrix array\n", - " N = df.shape[0] # number of samples in df\n", - " shape_matrix_2d = (N, *tensor_dim) # 2d matrix shape \n", - " # initialize dot matrix with zeros\n", - " ohe_matrix_2d = np.zeros(shape_matrix_2d, dtype=\"float32\")\n", - "\n", - " # compile matrix with watson-crick interactions.\n", - " for index, row in df.iterrows(): \n", - " for bind_index, bind_nt in enumerate(row.gene.upper()):\n", - " \n", - " for ncrna_index, ncrna_nt in enumerate(row.noncodingRNA.upper()):\n", - " if ncrna_index >= tensor_dim[1]:\n", - " break\n", - " base_pairs = bind_nt + ncrna_nt\n", - " ohe_matrix_2d[index, bind_index, ncrna_index, 0] = alphabet.get(base_pairs, 0)\n", - " \n", - "\n", - " return ohe_matrix_2d, labels" - ], - "execution_count": 3, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "CpoytQwIElkg" - }, - "source": [ - "def make_architecture():\n", - " \"\"\"\n", - " build model architecture\n", - "\n", - " return a model object\n", - " \"\"\"\n", - " cnn_num = 6\n", - " kernel_size = 5\n", - " pool_size = 2\n", - " dropout_rate = 0.3\n", - " dense_num = 2\n", - "\n", - " x = Input(shape=(50,20,1),\n", - " dtype='float32', name='main_input'\n", - " )\n", - " main_input = x\n", - "\n", - " for cnn_i in range(cnn_num):\n", - " x = Conv2D(\n", - " filters=32 * (cnn_i + 1),\n", - " kernel_size=(kernel_size, kernel_size),\n", - " padding=\"same\",\n", - " data_format=\"channels_last\",\n", - " name=\"conv_\" + str(cnn_i + 1))(x)\n", - " x = LeakyReLU()(x)\n", - " x = BatchNormalization()(x)\n", - " x = MaxPooling2D(pool_size=(pool_size, pool_size), padding='same', name='Max_' + str(cnn_i + 1))(x)\n", - " x = Dropout(rate=dropout_rate)(x)\n", - "\n", - " x = Flatten(name='2d_matrix')(x)\n", - "\n", - " for dense_i in range(dense_num):\n", - " neurons = 32 * (cnn_num - dense_i)\n", - " x = Dense(neurons)(x)\n", - " x = LeakyReLU()(x)\n", - " x = BatchNormalization()(x)\n", - " x = Dropout(rate=dropout_rate)(x)\n", - "\n", - " main_output = Dense(1, activation='sigmoid', name='main_output')(x)\n", - "\n", - " model = K.Model(inputs=[main_input], outputs=[main_output], name='arch_00')\n", - " \n", - " return model" - ], - "execution_count": 4, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "oZ591qC0Femi" - }, - "source": [ - "def compile_model():\n", - " K.backend.clear_session()\n", - " model = make_architecture()\n", - " \n", - " opt = Adam(\n", - " learning_rate=0.00152,\n", - " beta_1=0.9,\n", - " beta_2=0.999,\n", - " epsilon=1e-07,\n", - " amsgrad=False,\n", - " name=\"Adam\")\n", - "\n", - " model.compile(\n", - " optimizer=opt,\n", - " loss='binary_crossentropy',\n", - " metrics=['accuracy']\n", - " )\n", - " return model" - ], - "execution_count": 5, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "wTB6K0lxyzcx" - }, - "source": [ - "def plot_history(history):\n", - " \"\"\"\n", - " plot history of the training of the model,\n", - " accuracy and loss of the training and validation set\n", - " \"\"\"\n", - " \n", - " acc = history.history['accuracy']\n", - " val_acc = history.history['val_accuracy']\n", - " loss = history.history['loss']\n", - " val_loss = history.history['val_loss']\n", - "\n", - " epochs = range(1, len(acc) + 1)\n", - "\n", - " plt.figure(figsize=(8, 6), dpi=80)\n", - "\n", - " plt.plot(epochs, acc, 'bo', label='Training acc')\n", - " plt.plot(epochs, val_acc, 'b', label='Validation acc')\n", - " plt.title('Accuracy')\n", - " plt.legend()\n", - " plt.figure()\n", - "\n", - " plt.plot(epochs, loss, 'bo', label='Training loss')\n", - " plt.plot(epochs, val_loss, 'b', label='Validation loss')\n", - " plt.title('Loss')\n", - " plt.legend()\n", - " plt.show()" - ], - "execution_count": 6, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "3Bh2-XEPxmZf", - "outputId": "3dffc4d8-d135-46fd-dcf4-94edb75db0cb" - }, - "source": [ - "train_df = pd.read_csv('train_set_1_10_CLASH2013_paper.tsv', sep='\\t', names=['noncodingRNA', 'gene', 'label'], header=0)\n", - "# set random state for reproducibility\n", - "RANDOM_STATE = 42\n", - "np.random.seed(RANDOM_STATE)\n", - "train_df = train_df.sample(frac=1, random_state=RANDOM_STATE)\n", - "print(train_df.head())\n", - "ohe_data = binding_encoding(train_df)\n", - "train_ohe, labels = ohe_data\n", - "print(\"Number of training samples: \", train_df.shape[0])" - ], - "execution_count": 8, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - " noncodingRNA \\\n", - "45236 ACTGCATTATGAGCACTTAA \n", - "168824 TATTGCACTTGTCCCGGCCT \n", - "2591 AAAAGCTGGGTTGAGAGGGC \n", - "76746 TCTCACACAGAAATCGCACC \n", - "63277 TGAGGTAGTAGTTTGTGCTG \n", - "\n", - " gene label \n", - "45236 GAGAAGAAATCTGGCTGGTTTGAGGGTTTCCTTTAGTTCACCCTCA... 0 \n", - "168824 GTAAATGTCTGTTTTTCATAATTGCTCTTTATATTGTGTGTTATCT... 0 \n", - "2591 GTACCCAGTAAAAACCAGAATGACCCATTGCCAGGACGCATCAAAG... 1 \n", - "76746 ACGTCGGCGCCATGCTCCAGGTACAGAGCCACATGTTGCTCCAGGC... 0 \n", - "63277 ACCAATGCCAGAGGAGCAACAGCGGCAACCTTTGGCACTGCATCCA... 0 \n", - "Number of training samples: 169312\n" - ] - } - ] - }, - { - "cell_type": "code", - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "Lcc6OuabyVsK", - "outputId": "bb4e3fae-f08d-4d37-acfe-ff41c66e83eb" - }, - "source": [ - "model = compile_model()\n", - "model.summary()" - ], - "execution_count": 9, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Model: \"arch_00\"\n", - "_________________________________________________________________\n", - " Layer (type) Output Shape Param # \n", - "=================================================================\n", - " main_input (InputLayer) [(None, 50, 20, 1)] 0 \n", - " \n", - " conv_1 (Conv2D) (None, 50, 20, 32) 832 \n", - " \n", - " leaky_re_lu (LeakyReLU) (None, 50, 20, 32) 0 \n", - " \n", - " batch_normalization (BatchN (None, 50, 20, 32) 128 \n", - " ormalization) \n", - " \n", - " Max_1 (MaxPooling2D) (None, 25, 10, 32) 0 \n", - " \n", - " dropout (Dropout) (None, 25, 10, 32) 0 \n", - " \n", - " conv_2 (Conv2D) (None, 25, 10, 64) 51264 \n", - " \n", - " leaky_re_lu_1 (LeakyReLU) (None, 25, 10, 64) 0 \n", - " \n", - " batch_normalization_1 (Batc (None, 25, 10, 64) 256 \n", - " hNormalization) \n", - " \n", - " Max_2 (MaxPooling2D) (None, 13, 5, 64) 0 \n", - " \n", - " dropout_1 (Dropout) (None, 13, 5, 64) 0 \n", - " \n", - " conv_3 (Conv2D) (None, 13, 5, 96) 153696 \n", - " \n", - " leaky_re_lu_2 (LeakyReLU) (None, 13, 5, 96) 0 \n", - " \n", - " batch_normalization_2 (Batc (None, 13, 5, 96) 384 \n", - " hNormalization) \n", - " \n", - " Max_3 (MaxPooling2D) (None, 7, 3, 96) 0 \n", - " \n", - " dropout_2 (Dropout) (None, 7, 3, 96) 0 \n", - " \n", - " conv_4 (Conv2D) (None, 7, 3, 128) 307328 \n", - " \n", - " leaky_re_lu_3 (LeakyReLU) (None, 7, 3, 128) 0 \n", - " \n", - " batch_normalization_3 (Batc (None, 7, 3, 128) 512 \n", - " hNormalization) \n", - " \n", - " Max_4 (MaxPooling2D) (None, 4, 2, 128) 0 \n", - " \n", - " dropout_3 (Dropout) (None, 4, 2, 128) 0 \n", - " \n", - " conv_5 (Conv2D) (None, 4, 2, 160) 512160 \n", - " \n", - " leaky_re_lu_4 (LeakyReLU) (None, 4, 2, 160) 0 \n", - " \n", - " batch_normalization_4 (Batc (None, 4, 2, 160) 640 \n", - " hNormalization) \n", - " \n", - " Max_5 (MaxPooling2D) (None, 2, 1, 160) 0 \n", - " \n", - " dropout_4 (Dropout) (None, 2, 1, 160) 0 \n", - " \n", - " conv_6 (Conv2D) (None, 2, 1, 192) 768192 \n", - " \n", - " leaky_re_lu_5 (LeakyReLU) (None, 2, 1, 192) 0 \n", - " \n", - " batch_normalization_5 (Batc (None, 2, 1, 192) 768 \n", - " hNormalization) \n", - " \n", - " Max_6 (MaxPooling2D) (None, 1, 1, 192) 0 \n", - " \n", - " dropout_5 (Dropout) (None, 1, 1, 192) 0 \n", - " \n", - " 2d_matrix (Flatten) (None, 192) 0 \n", - " \n", - " dense (Dense) (None, 192) 37056 \n", - " \n", - " leaky_re_lu_6 (LeakyReLU) (None, 192) 0 \n", - " \n", - " batch_normalization_6 (Batc (None, 192) 768 \n", - " hNormalization) \n", - " \n", - " dropout_6 (Dropout) (None, 192) 0 \n", - " \n", - " dense_1 (Dense) (None, 160) 30880 \n", - " \n", - " leaky_re_lu_7 (LeakyReLU) (None, 160) 0 \n", - " \n", - " batch_normalization_7 (Batc (None, 160) 640 \n", - " hNormalization) \n", - " \n", - " dropout_7 (Dropout) (None, 160) 0 \n", - " \n", - " main_output (Dense) (None, 1) 161 \n", - " \n", - "=================================================================\n", - "Total params: 1,865,665\n", - "Trainable params: 1,863,617\n", - "Non-trainable params: 2,048\n", - "_________________________________________________________________\n" - ] - } - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "vIEbdJxqydNm" - }, - "source": [ - "model_history = model.fit(\n", - " train_ohe, labels,\n", - " validation_split=0.05, epochs=10,\n", - " batch_size=32,\n", - " class_weight={0 : 1, 1 : 10}\n", - " )" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "e4uUTu-k0Y2S" - }, - "source": [ - "plot_history(model_history)" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "WDSXwnZmymfK" - }, - "source": [ - "model.save(\"model.h5\")" - ], - "execution_count": null, - "outputs": [] - } - ] -} \ No newline at end of file diff --git a/code/machine_learning/train/CNN_miRBind_2022/training.py b/code/machine_learning/train/CNN_miRBind_2022/training.py new file mode 100644 index 0000000..48b4124 --- /dev/null +++ b/code/machine_learning/train/CNN_miRBind_2022/training.py @@ -0,0 +1,204 @@ +#!/usr/bin/env python +# coding: utf-8 + +import numpy as np +import argparse +import time +import tensorflow as tf +from tensorflow import keras as K +import matplotlib.pyplot as plt +from tensorflow.keras.layers import ( + BatchNormalization, LeakyReLU, + Input, Dense, Conv2D, + MaxPooling2D, Flatten, Dropout) +from tensorflow.keras.optimizers import Adam +from tensorflow.keras.utils import Sequence + + +def make_architecture(): + """ + Build model architecture + The parameters are based on the CNN model presented in miRBind paper (2022) https://doi.org/10.3390/genes13122323 + """ + cnn_num = 6 + kernel_size = 5 + pool_size = 2 + dropout_rate = 0.3 + dense_num = 2 + + x = Input(shape=(50,20,1), + dtype='float32', name='main_input' + ) + main_input = x + + for cnn_i in range(cnn_num): + x = Conv2D( + filters=32 * (cnn_i + 1), + kernel_size=(kernel_size, kernel_size), + padding="same", + data_format="channels_last", + name="conv_" + str(cnn_i + 1))(x) + x = LeakyReLU()(x) + x = BatchNormalization()(x) + x = MaxPooling2D(pool_size=(pool_size, pool_size), padding='same', name='Max_' + str(cnn_i + 1))(x) + x = Dropout(rate=dropout_rate)(x) + + x = Flatten(name='2d_matrix')(x) + + for dense_i in range(dense_num): + neurons = 32 * (cnn_num - dense_i) + x = Dense(neurons)(x) + x = LeakyReLU()(x) + x = BatchNormalization()(x) + x = Dropout(rate=dropout_rate)(x) + + main_output = Dense(1, activation='sigmoid', name='main_output')(x) + + model = K.Model(inputs=[main_input], outputs=[main_output], name='arch_00') + + return model + + +def compile_model(): + K.backend.clear_session() + model = make_architecture() + + opt = Adam( + learning_rate=0.00152, + beta_1=0.9, + beta_2=0.999, + epsilon=1e-07, + amsgrad=False, + name="Adam") + + model.compile( + optimizer=opt, + loss='binary_crossentropy', + metrics=['accuracy'] + ) + return model + + +def plot_history(history, ratio): + """ + Plot history of the model training, + accuracy and loss of the training and validation set + """ + + acc = history.history['accuracy'] + val_acc = history.history['val_accuracy'] + loss = history.history['loss'] + val_loss = history.history['val_loss'] + + epochs = range(1, len(acc) + 1) + + plt.figure(figsize=(8, 6), dpi=80) + + plt.plot(epochs, acc, 'bo', label='Training acc') + plt.plot(epochs, val_acc, 'b', label='Validation acc') + plt.title('Accuracy') + plt.legend() + plt.savefig(f"training_acc_1_{ratio}.jpg") + + plt.figure() + + plt.plot(epochs, loss, 'bo', label='Training loss') + plt.plot(epochs, val_loss, 'b', label='Validation loss') + plt.title('Loss') + plt.legend() + plt.savefig(f"training_loss_1_{ratio}.jpg") + + +class DataGenerator(Sequence): + def __init__(self, data_path, labels_path, dataset_size, batch_size, ratio, validation_split=0.1, is_validation=False, shuffle=True): + # preload the encoded numpy data + # the dataset size is needed to properly load the numpy files + self.size = dataset_size + + self.data = np.memmap(data_path, dtype='float32', mode='r', shape=(self.size, 50, 20, 1)) + self.labels = np.memmap(labels_path, dtype='float32', mode='r', shape=(self.size,)) + self.batch_size = batch_size + self.shuffle = shuffle + + # Determine number of train and validation samples + self.validation_split = validation_split + self.num_samples = len(self.data) + self.num_validation_samples = int(self.num_samples * validation_split) + self.num_train_samples = self.num_samples - self.num_validation_samples + + # Determine indices for validation and training + indices = np.arange(self.num_samples) + if shuffle: + np.random.shuffle(indices) + + if is_validation: + self.indices = indices[self.num_train_samples:] + else: + self.indices = indices[:self.num_train_samples] + + # Shuffle the data initially + self.on_epoch_end() + + def __len__(self): + # Denotes the number of batches per epoch + return int(np.ceil(len(self.indices) / float(self.batch_size))) + + def __getitem__(self, idx): + # Generate one batch of data + batch_indices = self.indices[idx * self.batch_size:(idx + 1) * self.batch_size] + batch_data = self.data[batch_indices] + batch_labels = self.labels[batch_indices] + return batch_data, batch_labels + + def on_epoch_end(self): + # Updates indices after each epoch for shuffling + if self.shuffle: + np.random.shuffle(self.indices) + + +def train_model(data, labels, dataset_size, ratio, model_file): + + # set random state for reproducibility + np.random.seed(42) + tf.random.set_seed(42) + K.utils.set_random_seed(42) + # TODO still not fully reproducible? why? + + train_data_gen = DataGenerator(data, labels, dataset_size, batch_size=32, ratio=ratio, validation_split=0.1, is_validation=False) + val_data_gen = DataGenerator(data, labels, dataset_size, batch_size=32, ratio=ratio, validation_split=0.1, is_validation=True) + + model = compile_model() + model_history = model.fit( + train_data_gen, + validation_data=val_data_gen, + epochs=10, + class_weight={0: 1, 1: ratio} + ) + + plot_history(model_history, ratio) + + model.save(model_file) + + +def main(): + parser = argparse.ArgumentParser(description="Train CNN model on encoded miRNA x target binding matrix dataset") + parser.add_argument('--ratio', type=int, required=True, help="Ratio of pos:neg in the training dataset") + parser.add_argument('--data', type=str, required=True, help="File with the encoded dataset") + parser.add_argument('--labels', type=str, required=True, help="File with the dataset labels") + parser.add_argument('--dataset_size', type=int, required=True, help="Number of samples in the dataset. Needed to properly load the nump files.") + parser.add_argument('--model', type=str, required=False, help="Filename to save the trained model") + args = parser.parse_args() + + if args.model is None: + args.model = f"model_Manakov22_{args.ratio}.keras" + + start = time.time() + train_model(args.data, args.labels, args.dataset_size, args.ratio, args.model) + end = time.time() + + print("Elapsed time: ", end - start, " s.") + +if __name__ == "__main__": + main() + + From e92e058dc62c8620cb24605ddfd58bdc6080ff43 Mon Sep 17 00:00:00 2001 From: evaklimentova Date: Tue, 15 Oct 2024 15:54:14 +0200 Subject: [PATCH 02/20] miRBind CNN cleanup --- code/machine_learning/encode/README.md | 6 + .../encode/binding_2D_matrix_encoder.py | 26 +-- .../miRBind_CNN_architecture.py | 60 ++++++ ...py => miRBind_training_orig_parameters.py} | 173 ++++++------------ code/machine_learning/train/README.md | 9 +- 5 files changed, 143 insertions(+), 131 deletions(-) create mode 100644 code/machine_learning/train/CNN_miRBind_2022/miRBind_CNN_architecture.py rename code/machine_learning/train/CNN_miRBind_2022/{training.py => miRBind_training_orig_parameters.py} (60%) diff --git a/code/machine_learning/encode/README.md b/code/machine_learning/encode/README.md index 8179719..697c03e 100644 --- a/code/machine_learning/encode/README.md +++ b/code/machine_learning/encode/README.md @@ -9,3 +9,9 @@ Encodes miRNA and gene sequences into 2D-binding matrix. Outputs npy file with encoded matrices and npy file with corresponding labels. +#### Usage +Run the script from the command line with the following syntax: + + +```python binding_2d_matrix_encoder.py --i_file input_dataset_file.tsv --o_prefix output_prefix``` + diff --git a/code/machine_learning/encode/binding_2D_matrix_encoder.py b/code/machine_learning/encode/binding_2D_matrix_encoder.py index 480e524..484e534 100644 --- a/code/machine_learning/encode/binding_2D_matrix_encoder.py +++ b/code/machine_learning/encode/binding_2D_matrix_encoder.py @@ -2,7 +2,7 @@ import numpy as np import argparse import time - + def binding_encoding(df, alphabet, tensor_dim=(50, 20, 1)): """ @@ -20,7 +20,7 @@ def binding_encoding(df, alphabet, tensor_dim=(50, 20, 1)): # Initialize dot matrix with zeros ohe_matrix_2d = np.zeros((len(df), *tensor_dim), dtype="float32") - + df = df.reset_index(drop=True) # Compile matrix with Watson-Crick interactions @@ -34,6 +34,7 @@ def binding_encoding(df, alphabet, tensor_dim=(50, 20, 1)): return ohe_matrix_2d, labels + def encode_large_tsv_to_numpy(tsv_file_path, data_output_path, labels_output_path, chunk_size=10000): """ Encode a large TSV file into a NumPy matrix using chunk processing. @@ -50,7 +51,7 @@ def encode_large_tsv_to_numpy(tsv_file_path, data_output_path, labels_output_pat # Get total number of rows in the dataset num_rows = sum(len(df) for df in pd.read_csv(tsv_file_path, sep='\t', usecols=[0], chunksize=chunk_size)) - + # Determine the shape of the output arrays labels_shape = (num_rows,) data_shape = (num_rows, *tensor_dim) @@ -64,7 +65,7 @@ def encode_large_tsv_to_numpy(tsv_file_path, data_output_path, labels_output_pat # Process each chunk for chunk in pd.read_csv(tsv_file_path, sep='\t', chunksize=chunk_size): encoded_data, encoded_labels = binding_encoding(chunk, alphabet, tensor_dim) - + # Write the chunk's data and labels to the memory-mapped files ohe_matrix_2d[row_offset:row_offset + len(chunk)] = encoded_data labels[row_offset:row_offset + len(chunk)] = encoded_labels @@ -74,25 +75,28 @@ def encode_large_tsv_to_numpy(tsv_file_path, data_output_path, labels_output_pat ohe_matrix_2d.flush() labels.flush() + def main(): - """ + """ Based on "miRBind: A deep learning method for miRNA binding classification." Genes 13.12 (2022): 2323. https://doi.org/10.3390/genes13122323. Original implementation: https://github.com/ML-Bioinfo-CEITEC/miRBind Encodes miRNA and gene sequences into 2D-binding matrix. 2D-binding matrix has shape (gene_max_len=50, miRNA_max_len=20, 1) and contains 1 for Watson-Crick interactions and 0 otherwise. """ - - parser = argparse.ArgumentParser(description="Encode dataset to miRNA x target binding matrix. Outputs numpy file with matrices and and numpy file with corresponding labels. Expected columns of the dataset are 'noncodingRNA', 'gene' and 'label'") - parser.add_argument('--i_file', type=str, required=True, help="Input dataset file name") - parser.add_argument('--o_prefix', type=str, required=True, help="Output file name prefix") + + parser = argparse.ArgumentParser( + description="Encode dataset to miRNA x target binding matrix. Outputs numpy file with matrices and and numpy file with corresponding labels. Expected columns of the dataset are 'noncodingRNA', 'gene' and 'label'") + parser.add_argument('-i', '--i_file', type=str, required=True, help="Input dataset file name") + parser.add_argument('-o', '--o_prefix', type=str, required=True, help="Output file name prefix") args = parser.parse_args() - + start = time.time() encode_large_tsv_to_numpy(args.i_file, args.o_prefix + '_dataset.npy', args.o_prefix + '_labels.npy') end = time.time() - + print("Elapsed time: ", end - start, " s.") + if __name__ == "__main__": main() \ No newline at end of file diff --git a/code/machine_learning/train/CNN_miRBind_2022/miRBind_CNN_architecture.py b/code/machine_learning/train/CNN_miRBind_2022/miRBind_CNN_architecture.py new file mode 100644 index 0000000..48d8bef --- /dev/null +++ b/code/machine_learning/train/CNN_miRBind_2022/miRBind_CNN_architecture.py @@ -0,0 +1,60 @@ +import tensorflow as tf +from tensorflow import keras as K +from tensorflow.keras.optimizers import Adam +from tensorflow.keras.layers import Input, Conv2D, LeakyReLU, BatchNormalization, MaxPooling2D, Dropout, Flatten, Dense + + +class miRBind_CNN(): + """ + Build model architecture based on the CNN model presented in miRBind paper (2022) https://doi.org/10.3390/genes13122323 + The default parameters are same as the ones used in the paper + """ + def __init__(self, cnn_num = 6, kernel_size = 5, pool_size = 2, dropout_rate = 0.3, dense_num = 2): + + x = Input(shape=(50,20,1), dtype='float32') + main_input = x + + for cnn_i in range(cnn_num): + x = Conv2D( + filters=32 * (cnn_i + 1), + kernel_size=(kernel_size, kernel_size), + padding="same", + data_format="channels_last")(x) + x = LeakyReLU()(x) + x = BatchNormalization()(x) + x = MaxPooling2D(pool_size=(pool_size, pool_size), padding='same')(x) + x = Dropout(rate=dropout_rate)(x) + + x = Flatten()(x) + + for dense_i in range(dense_num): + neurons = 32 * (cnn_num - dense_i) + x = Dense(neurons)(x) + x = LeakyReLU()(x) + x = BatchNormalization()(x) + x = Dropout(rate=dropout_rate)(x) + + main_output = Dense(1, activation='sigmoid')(x) + + model = K.Model(inputs=[main_input], outputs=[main_output], name='miRBind_CNN') + + self.model = model + + def compile_model(self, lr=0.00152): + K.backend.clear_session() + model = self.model + + opt = Adam( + learning_rate=lr, + beta_1=0.9, + beta_2=0.999, + epsilon=1e-07, + amsgrad=False, + name="Adam") + + model.compile( + optimizer=opt, + loss='binary_crossentropy', + metrics=['accuracy'] + ) + return model \ No newline at end of file diff --git a/code/machine_learning/train/CNN_miRBind_2022/training.py b/code/machine_learning/train/CNN_miRBind_2022/miRBind_training_orig_parameters.py similarity index 60% rename from code/machine_learning/train/CNN_miRBind_2022/training.py rename to code/machine_learning/train/CNN_miRBind_2022/miRBind_training_orig_parameters.py index 48b4124..6d8e1cc 100644 --- a/code/machine_learning/train/CNN_miRBind_2022/training.py +++ b/code/machine_learning/train/CNN_miRBind_2022/miRBind_training_orig_parameters.py @@ -1,141 +1,42 @@ -#!/usr/bin/env python -# coding: utf-8 - import numpy as np import argparse import time import tensorflow as tf from tensorflow import keras as K import matplotlib.pyplot as plt -from tensorflow.keras.layers import ( - BatchNormalization, LeakyReLU, - Input, Dense, Conv2D, - MaxPooling2D, Flatten, Dropout) -from tensorflow.keras.optimizers import Adam from tensorflow.keras.utils import Sequence - -def make_architecture(): - """ - Build model architecture - The parameters are based on the CNN model presented in miRBind paper (2022) https://doi.org/10.3390/genes13122323 - """ - cnn_num = 6 - kernel_size = 5 - pool_size = 2 - dropout_rate = 0.3 - dense_num = 2 - - x = Input(shape=(50,20,1), - dtype='float32', name='main_input' - ) - main_input = x - - for cnn_i in range(cnn_num): - x = Conv2D( - filters=32 * (cnn_i + 1), - kernel_size=(kernel_size, kernel_size), - padding="same", - data_format="channels_last", - name="conv_" + str(cnn_i + 1))(x) - x = LeakyReLU()(x) - x = BatchNormalization()(x) - x = MaxPooling2D(pool_size=(pool_size, pool_size), padding='same', name='Max_' + str(cnn_i + 1))(x) - x = Dropout(rate=dropout_rate)(x) - - x = Flatten(name='2d_matrix')(x) - - for dense_i in range(dense_num): - neurons = 32 * (cnn_num - dense_i) - x = Dense(neurons)(x) - x = LeakyReLU()(x) - x = BatchNormalization()(x) - x = Dropout(rate=dropout_rate)(x) - - main_output = Dense(1, activation='sigmoid', name='main_output')(x) - - model = K.Model(inputs=[main_input], outputs=[main_output], name='arch_00') - - return model - - -def compile_model(): - K.backend.clear_session() - model = make_architecture() - - opt = Adam( - learning_rate=0.00152, - beta_1=0.9, - beta_2=0.999, - epsilon=1e-07, - amsgrad=False, - name="Adam") - - model.compile( - optimizer=opt, - loss='binary_crossentropy', - metrics=['accuracy'] - ) - return model - - -def plot_history(history, ratio): - """ - Plot history of the model training, - accuracy and loss of the training and validation set - """ - - acc = history.history['accuracy'] - val_acc = history.history['val_accuracy'] - loss = history.history['loss'] - val_loss = history.history['val_loss'] - - epochs = range(1, len(acc) + 1) - - plt.figure(figsize=(8, 6), dpi=80) - - plt.plot(epochs, acc, 'bo', label='Training acc') - plt.plot(epochs, val_acc, 'b', label='Validation acc') - plt.title('Accuracy') - plt.legend() - plt.savefig(f"training_acc_1_{ratio}.jpg") - - plt.figure() - - plt.plot(epochs, loss, 'bo', label='Training loss') - plt.plot(epochs, val_loss, 'b', label='Validation loss') - plt.title('Loss') - plt.legend() - plt.savefig(f"training_loss_1_{ratio}.jpg") +from miRBind_CNN_architecture import miRBind_CNN class DataGenerator(Sequence): - def __init__(self, data_path, labels_path, dataset_size, batch_size, ratio, validation_split=0.1, is_validation=False, shuffle=True): - # preload the encoded numpy data + # preload the encoded numpy data + def __init__(self, data_path, labels_path, dataset_size, batch_size, validation_split=0.1, + is_validation=False, shuffle=True): # the dataset size is needed to properly load the numpy files self.size = dataset_size - + self.data = np.memmap(data_path, dtype='float32', mode='r', shape=(self.size, 50, 20, 1)) self.labels = np.memmap(labels_path, dtype='float32', mode='r', shape=(self.size,)) self.batch_size = batch_size self.shuffle = shuffle - + # Determine number of train and validation samples self.validation_split = validation_split self.num_samples = len(self.data) self.num_validation_samples = int(self.num_samples * validation_split) self.num_train_samples = self.num_samples - self.num_validation_samples - + # Determine indices for validation and training indices = np.arange(self.num_samples) if shuffle: np.random.shuffle(indices) - + if is_validation: self.indices = indices[self.num_train_samples:] else: self.indices = indices[:self.num_train_samples] - + # Shuffle the data initially self.on_epoch_end() @@ -156,18 +57,49 @@ def on_epoch_end(self): np.random.shuffle(self.indices) -def train_model(data, labels, dataset_size, ratio, model_file): +def plot_history(history, ratio): + """ + Plot history of the model training, + accuracy and loss of the training and validation set + """ + + acc = history.history['accuracy'] + val_acc = history.history['val_accuracy'] + loss = history.history['loss'] + val_loss = history.history['val_loss'] + + epochs = range(1, len(acc) + 1) + plt.figure(figsize=(8, 6), dpi=80) + + plt.plot(epochs, acc, 'bo', label='Training acc') + plt.plot(epochs, val_acc, 'b', label='Validation acc') + plt.title('Accuracy') + plt.legend() + plt.savefig(f"training_acc_1_{ratio}.jpg") + + plt.figure() + + plt.plot(epochs, loss, 'bo', label='Training loss') + plt.plot(epochs, val_loss, 'b', label='Validation loss') + plt.title('Loss') + plt.legend() + plt.savefig(f"training_loss_1_{ratio}.jpg") + + +def train_model(data, labels, dataset_size, ratio, model_file, debug=False): # set random state for reproducibility np.random.seed(42) tf.random.set_seed(42) K.utils.set_random_seed(42) # TODO still not fully reproducible? why? - train_data_gen = DataGenerator(data, labels, dataset_size, batch_size=32, ratio=ratio, validation_split=0.1, is_validation=False) - val_data_gen = DataGenerator(data, labels, dataset_size, batch_size=32, ratio=ratio, validation_split=0.1, is_validation=True) + train_data_gen = DataGenerator(data, labels, dataset_size, batch_size=32, validation_split=0.1, + is_validation=False) + val_data_gen = DataGenerator(data, labels, dataset_size, batch_size=32, validation_split=0.1, + is_validation=True) - model = compile_model() + model = miRBind_CNN().compile_model() model_history = model.fit( train_data_gen, validation_data=val_data_gen, @@ -175,7 +107,8 @@ def train_model(data, labels, dataset_size, ratio, model_file): class_weight={0: 1, 1: ratio} ) - plot_history(model_history, ratio) + if debug: + plot_history(model_history, ratio) model.save(model_file) @@ -185,20 +118,22 @@ def main(): parser.add_argument('--ratio', type=int, required=True, help="Ratio of pos:neg in the training dataset") parser.add_argument('--data', type=str, required=True, help="File with the encoded dataset") parser.add_argument('--labels', type=str, required=True, help="File with the dataset labels") - parser.add_argument('--dataset_size', type=int, required=True, help="Number of samples in the dataset. Needed to properly load the nump files.") + parser.add_argument('--dataset_size', type=int, required=True, + help="Number of samples in the dataset. Needed to properly load the numpy files.") parser.add_argument('--model', type=str, required=False, help="Filename to save the trained model") + parser.add_argument('--debug', type=bool, default=False, help="Set to True to output some plots about training") args = parser.parse_args() if args.model is None: - args.model = f"model_Manakov22_{args.ratio}.keras" + args.model = f"model_1_{args.ratio}.keras" start = time.time() - train_model(args.data, args.labels, args.dataset_size, args.ratio, args.model) + train_model(data=args.data, labels=args.labels, dataset_size=args.dataset_size, ratio=args.ratio, + model_file=args.model, debug=args.debug) end = time.time() - + print("Elapsed time: ", end - start, " s.") + if __name__ == "__main__": main() - - diff --git a/code/machine_learning/train/README.md b/code/machine_learning/train/README.md index 2eb5163..a9e0b97 100644 --- a/code/machine_learning/train/README.md +++ b/code/machine_learning/train/README.md @@ -1 +1,8 @@ -# Training the models \ No newline at end of file +# Training the models + +### CNN miRBind 2022 +This directory aggregates models based on the miRBind CNN architecture. It was presented in this miRBind paper (2022) https://doi.org/10.3390/genes13122323 + +[miRBind CNN architecture](CNN_miRBind_2022/miRBind_CNN_architecture.py) - containing definition of the CNN model architecture + +[miRBind CNN training with original parameters](CNN_miRBind_2022/miRBind_training_orig_parameters.py) - containing training of the CNN model with the original parameters described in the paper \ No newline at end of file From 63f05587a676f702725fec6699fd8db4aeed2a72 Mon Sep 17 00:00:00 2001 From: evaklimentova Date: Tue, 22 Oct 2024 10:45:56 +0200 Subject: [PATCH 03/20] adding pipeline for running miRBin CNN training --- .../README.md | 15 +++++++++++++++ .../run_retraining.sh | 18 ++++++++++++++++++ ...=> miRBind_CNN_training_orig_parameters.py} | 0 code/machine_learning/train/README.md | 2 +- 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 analysis/miRBind_CNN_retraining_orig_parameters/README.md create mode 100644 analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh rename code/machine_learning/train/CNN_miRBind_2022/{miRBind_training_orig_parameters.py => miRBind_CNN_training_orig_parameters.py} (100%) diff --git a/analysis/miRBind_CNN_retraining_orig_parameters/README.md b/analysis/miRBind_CNN_retraining_orig_parameters/README.md new file mode 100644 index 0000000..de651fb --- /dev/null +++ b/analysis/miRBind_CNN_retraining_orig_parameters/README.md @@ -0,0 +1,15 @@ +# miRBind CNN retraining with original parameters + +Run +```bash run_retraining.sh``` +to retrain the miRBind CNN as presented in the [miRBind paper](https://doi.org/10.3390/genes13122323) on Manakov 1:1 train dataset. +The training is done with the original hyperparameters used in the paper. + +### Dependencies + +- python=3.8 +- tensorflow=2.13 +- matplotlib +- numpy +- pandas + diff --git a/analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh b/analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh new file mode 100644 index 0000000..3afd191 --- /dev/null +++ b/analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +DATASET="../../data/chimeric_datasets/Manakov2022/AGO_eCLIP_Manakov2022_1_train_dataset.tsv" +MODEL="../../models/miRBind_CNN_retrained_Manakov_1_orig_parameters.keras" +CODE="../../code/machine_learning" + +mkdir -p encoded_dataset + +# encode dataset +python $CODE/encode/binding_2D_matrix_encoder.py --i_file $DATASET --o_prefix encoded_dataset/AGO2_eCLIP_Manakov2022_1_train + +# train model +python $CODE/train/CNN_miRBind_2022/miRBind_CNN_training_orig_parameters.py \ +--data encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_dataset.npy \ +--labels encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_labels.npy \ +--dataset_size 2524246 \ +--ratio 1 \ +--model $MODEL \ No newline at end of file diff --git a/code/machine_learning/train/CNN_miRBind_2022/miRBind_training_orig_parameters.py b/code/machine_learning/train/CNN_miRBind_2022/miRBind_CNN_training_orig_parameters.py similarity index 100% rename from code/machine_learning/train/CNN_miRBind_2022/miRBind_training_orig_parameters.py rename to code/machine_learning/train/CNN_miRBind_2022/miRBind_CNN_training_orig_parameters.py diff --git a/code/machine_learning/train/README.md b/code/machine_learning/train/README.md index a9e0b97..ef527dd 100644 --- a/code/machine_learning/train/README.md +++ b/code/machine_learning/train/README.md @@ -5,4 +5,4 @@ This directory aggregates models based on the miRBind CNN architecture. It was p [miRBind CNN architecture](CNN_miRBind_2022/miRBind_CNN_architecture.py) - containing definition of the CNN model architecture -[miRBind CNN training with original parameters](CNN_miRBind_2022/miRBind_training_orig_parameters.py) - containing training of the CNN model with the original parameters described in the paper \ No newline at end of file +[miRBind CNN training with original parameters](CNN_miRBind_2022/miRBind_CNN_training_orig_parameters.py) - containing training of the CNN model with the original parameters described in the paper \ No newline at end of file From 410cf03ce881d4fad695ffd62303ad6c84c5ae49 Mon Sep 17 00:00:00 2001 From: evaklimentova Date: Tue, 22 Oct 2024 13:17:58 +0200 Subject: [PATCH 04/20] optimization notebook as tutorial --- .../Hyperparam_optimization.ipynb | 396 ++++++++++++++++++ 1 file changed, 396 insertions(+) create mode 100644 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/Hyperparam_optimization.ipynb diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/Hyperparam_optimization.ipynb b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/Hyperparam_optimization.ipynb new file mode 100644 index 0000000..c4d78a9 --- /dev/null +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/Hyperparam_optimization.ipynb @@ -0,0 +1,396 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "a56bd246-c7dd-4bec-94c9-99c1d8844f50", + "metadata": {}, + "source": [ + "# Hyperparam optimization notebook\n", + "\n", + "In this notebook, we will try to optimize the hyperparameters of the miRBind CNN model. Quick guide to what is a [parameter vs. hyperparameter](https://machinelearningmastery.com/difference-between-a-parameter-and-a-hyperparameter/).\n", + "\n", + "We will use [Optuna](https://optuna.org/) framework for this. It will try for us a bunch of different hyperparameter settings and see what combination works the best. \n", + "\n", + "Let's try to optimize number of blocks with convolution layer, kernel size of the convolution, size of the pooling layer, number of blocks with the dense layer and learning rate - these are our hyperparameters.\n", + "\n", + "Our metrics to optimize will be the AU PRC on the validation set (we split the train set into actual training set and validation set)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "038a2787-7e91-473b-bc4b-338200529612", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "from tensorflow import keras as K\n", + "from tensorflow.keras.optimizers import Adam\n", + "from tensorflow.keras.utils import Sequence\n", + "\n", + "import plotly\n", + "import logging\n", + "import optuna\n", + "import optuna.visualization as vis\n", + "from optuna.integration import TFKerasPruningCallback" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "9f138a3f-a28f-4ef7-b4cb-37d8034e7320", + "metadata": {}, + "outputs": [], + "source": [ + "import sys\n", + "sys.path.append(\"../../code/machine_learning/train/CNN_miRBind_2022/\")\n", + "\n", + "from miRBind_CNN_architecture import miRBind_CNN" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "e1cb92e2-78e0-45f1-b5c6-e4cabf6a4411", + "metadata": {}, + "outputs": [], + "source": [ + "# it's here for to be able to display plots in jupyter notebook\n", + "plotly.io.renderers.default = 'iframe'" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "be1adc2d-d4b5-470f-922d-86e5fc317e17", + "metadata": { + "id": "oZ591qC0Femi" + }, + "outputs": [], + "source": [ + "def compile_model(model, lr):\n", + " \n", + " opt = Adam(\n", + " learning_rate=lr,\n", + " beta_1=0.9,\n", + " beta_2=0.999,\n", + " epsilon=1e-07,\n", + " amsgrad=False,\n", + " name=\"Adam\")\n", + "\n", + " model.compile(\n", + " optimizer=opt,\n", + " loss='binary_crossentropy',\n", + " metrics=['accuracy', K.metrics.AUC(curve='PR')] # adding the metrics on which we want to optimize\n", + " )\n", + " return model" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "cb6f5a12-01ef-4b38-86d3-7f3d8ff49c39", + "metadata": {}, + "outputs": [], + "source": [ + "class DataGenerator(Sequence):\n", + " def __init__(self, data_path, labels_path, dataset_size, batch_size=32, validation_split=0.1, is_validation=False, shuffle=True): \n", + " # preload the encoded numpy data\n", + " # the size needed to properly load the array\n", + " self.size = dataset_size\n", + " \n", + " self.data = np.memmap(data_path, dtype='float32', mode='r', shape=(self.size, 50, 20, 1))\n", + " self.labels = np.memmap(labels_path, dtype='float32', mode='r', shape=(self.size,))\n", + " self.batch_size = batch_size\n", + " self.shuffle = shuffle\n", + " \n", + " # Determine number of train and validation samples\n", + " self.validation_split = validation_split\n", + " self.num_samples = len(self.data)\n", + " self.num_validation_samples = int(self.num_samples * validation_split)\n", + " self.num_train_samples = self.num_samples - self.num_validation_samples\n", + " \n", + " # Determine indices for validation and training\n", + " indices = np.arange(self.num_samples)\n", + " if shuffle:\n", + " np.random.shuffle(indices)\n", + " \n", + " if is_validation:\n", + " self.indices = indices[self.num_train_samples:]\n", + " else:\n", + " self.indices = indices[:self.num_train_samples]\n", + " \n", + " # Shuffle the data initially\n", + " self.on_epoch_end()\n", + "\n", + " def __len__(self):\n", + " # Denotes the number of batches per epoch\n", + " return int(np.ceil(len(self.indices) / float(self.batch_size)))\n", + "\n", + " def __getitem__(self, idx):\n", + " # Generate one batch of data\n", + " batch_indices = self.indices[idx * self.batch_size:(idx + 1) * self.batch_size]\n", + " batch_data = self.data[batch_indices]\n", + " batch_labels = self.labels[batch_indices]\n", + " return batch_data, batch_labels\n", + "\n", + " def on_epoch_end(self):\n", + " # Updates indices after each epoch for shuffling\n", + " if self.shuffle:\n", + " np.random.shuffle(self.indices)" + ] + }, + { + "cell_type": "markdown", + "id": "58a51c3e-0bf2-42e8-8eda-15cc4b92a251", + "metadata": {}, + "source": [ + "------------------------------\n", + "Choose a dataset on which you want to train. It has to be already encoded with the ```binding_2D_matrix_encoder.py```" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "e95fa7f9-103c-419e-a976-804af5bef9c8", + "metadata": {}, + "outputs": [], + "source": [ + "DATASET = \"../../AmiRBench/code/dataset_vOct/Manakov_1_train_dataset.npy\"\n", + "LABELS = \"../../AmiRBench/code/dataset_vOct/Manakov_1_train_labels.npy\"\n", + "DATASET_RATIO = 1\n", + "DATASET_SIZE = 2524246" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "448d3e9a-0ee5-483c-be44-e704d957434f", + "metadata": {}, + "outputs": [], + "source": [ + "train_data_gen = DataGenerator(DATASET, LABELS, dataset_size=DATASET_SIZE, validation_split=0.1, is_validation=False)\n", + "\n", + "val_data_gen = DataGenerator(DATASET, LABELS, dataset_size=DATASET_SIZE, validation_split=0.1, is_validation=True)" + ] + }, + { + "cell_type": "markdown", + "id": "9f4fdeb8-ceb6-4d06-81ff-fe3979a5fe69", + "metadata": {}, + "source": [ + "----------------------------\n", + "This is the function that creates a model with suggested hyperparameters, trains it and sees how well it performs on the validation set\n", + "\n", + "**Some explanations**\n", + "\n", + "`trial` is the object that \"carries the information\" about the hyperparameter optimization. `trial.suggest_` means \"give me some value for the hyperparameter\" that might work well for the model.\n", + "\n", + "`TFKerasPruningCallback` is another hack, where you can stop unpromising training in the middle and scratch it. E.g. when you are training the model with some hyperparameters and after few epochs you see the model doesn't learn anything, you can simply stop the training, remember that this set of hyperparameters didn't work well and you don't have to waste time with worthless training finishing.\n", + "\n", + "`K.callbacks.EarlyStopping` - early stopping method helps to train for the right amount of epochs. It monitors the performance on the validation set and when the model starts overfitting and performing worse, it stops the training." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "a90ef512-5b42-489c-a9ba-584c9877b662", + "metadata": {}, + "outputs": [], + "source": [ + "best_model = None\n", + "best_val_auc = 0\n", + "\n", + "def objective(trial):\n", + " global best_model, best_val_auc\n", + " \n", + " K.backend.clear_session()\n", + "\n", + " # build the model based on suggested hyperparameters\n", + " cnn_num = trial.suggest_int('cnn_layers_num', 2, 10)\n", + " kernel_size = trial.suggest_int('kernel_size', 3, 10)\n", + " pool_size = trial.suggest_int('pool_size', 1, 8)\n", + " dense_num = trial.suggest_int('dense_layers_num', 2, cnn_num)\n", + " model = miRBind_CNN(cnn_num=cnn_num, kernel_size=kernel_size, pool_size=pool_size, dense_num=dense_num).model\n", + " lr = trial.suggest_float('learning_rate', 0.00001, 0.1) \n", + " model = compile_model(model, lr=lr)\n", + " \n", + " model_history = model.fit(\n", + " train_data_gen,\n", + " validation_data=val_data_gen,\n", + " epochs=50,\n", + " class_weight={0: 1, 1: DATASET_RATIO},\n", + " callbacks=[TFKerasPruningCallback(trial, \"val_auc\"), # get rid of attempts with unpromising hyperparam combination\n", + " K.callbacks.EarlyStopping(patience=5, restore_best_weights=True)],\n", + " )\n", + " \n", + " num_epochs_trained = np.argmax(model_history.history['val_auc'])\n", + " val_auc = model_history.history['val_auc'][num_epochs_trained]\n", + "\n", + " # check performance of this trial\n", + " if val_auc > best_val_auc:\n", + " best_val_auc = val_auc\n", + " best_model = model # save the current best model\n", + " model.save('best_model.keras') # save the model to disk\n", + " logger.info(f\"New best model found and saved with Validation AUC: {val_auc}\")\n", + " \n", + " print(f\"Validation AU PRC: {val_auc}\")\n", + " \n", + " return val_auc" + ] + }, + { + "cell_type": "markdown", + "id": "f6a868f2-2695-467d-b217-353fd0b2fe0f", + "metadata": {}, + "source": [ + "Set up a logger for logging the optimization process to a file" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "4be25843-e179-44d9-9c36-4e500fdd62bf", + "metadata": {}, + "outputs": [], + "source": [ + "logger = logging.getLogger('optuna')\n", + "logger.setLevel(logging.INFO)\n", + "file_handler = logging.FileHandler('hyperparam_optimization.log', 'w')\n", + "file_handler.setFormatter(logging.Formatter('%(asctime)s - %(message)s'))\n", + "logger.addHandler(file_handler)" + ] + }, + { + "cell_type": "markdown", + "id": "5afc729b-d257-4126-abb7-ce47503bd675", + "metadata": {}, + "source": [ + "This is the place where we start running the optimization process" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7c4c6d6d-fa85-47c3-9df9-a67fc9cbe8d2", + "metadata": { + "id": "vIEbdJxqydNm" + }, + "outputs": [], + "source": [ + "study = optuna.create_study(direction='maximize', study_name='miRBind_CNN')\n", + "study.optimize(objective, n_trials=20)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5c710194-6b3b-4589-83ed-b170fb812015", + "metadata": {}, + "outputs": [], + "source": [ + "logger.info(\"\\n\")\n", + "logger.info(f\"Best hyperparameters: {study.best_params}\")\n", + "logger.info(f\"Best value (validation AU PRC): {study.best_value}\")" + ] + }, + { + "cell_type": "markdown", + "id": "29b123f2-ec2f-46bb-87d0-08a3c95d757f", + "metadata": {}, + "source": [ + "Let's plot now how the optimization process went, what was the best set of hyperparameters etc." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "978cee44-b292-4075-823a-f6a7f7e32b03", + "metadata": {}, + "outputs": [], + "source": [ + "vis.plot_optimization_history(study)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6830924e-dc38-42de-b8f3-138ffb69cc67", + "metadata": {}, + "outputs": [], + "source": [ + "vis.plot_optimization_history(study)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "908574f9-2800-4733-bdc5-08fe81fc7e8c", + "metadata": {}, + "outputs": [], + "source": [ + "vis.plot_contour(study)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9f6406a0-4c1e-4258-80c4-6075b91149dd", + "metadata": {}, + "outputs": [], + "source": [ + "vis.plot_param_importances(study)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dbf36bfc-fb06-4062-a4b0-e211131ef5d1", + "metadata": {}, + "outputs": [], + "source": [ + "vis.plot_slice(study)" + ] + }, + { + "cell_type": "markdown", + "id": "cf32651c-49f8-43db-a572-7f6863027ee8", + "metadata": {}, + "source": [ + "If you want to, you can save the plots like this:" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "122b996f-24c8-49d2-8ded-0e54453081f9", + "metadata": {}, + "outputs": [], + "source": [ + "fig = vis.plot_optimization_history(study)\n", + "fig.write_image(\"optimization_history.png\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:miRBench2]", + "language": "python", + "name": "conda-env-miRBench2-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 2fdcc3d88adba529b802a925adbe4f63dfdb058f Mon Sep 17 00:00:00 2001 From: davidcechak Date: Wed, 1 Jan 2025 21:09:15 +0100 Subject: [PATCH 05/20] Tmp commit, completition of miRBind CNN retraining and optimisation --- .../Hyperparam_optimization.ipynb | 599 +++++++++++++++++- ...ram_optimization-tmp_made_from_notebook.py | 202 ++++++ .../hyperparam_optimization.log | 207 ++++++ .../hyperparam_optimization.py | 187 ++++++ .../run_data_encoding.sh | 23 + .../run_retraining.sh | 32 +- .../miRBind_CNN_architecture.cpython-38.pyc | Bin 0 -> 2215 bytes 7 files changed, 1224 insertions(+), 26 deletions(-) create mode 100644 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization-tmp_made_from_notebook.py create mode 100644 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.log create mode 100644 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py create mode 100755 analysis/miRBind_CNN_retraining_orig_parameters/run_data_encoding.sh mode change 100644 => 100755 analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh create mode 100644 code/machine_learning/train/CNN_miRBind_2022/__pycache__/miRBind_CNN_architecture.cpython-38.pyc diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/Hyperparam_optimization.ipynb b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/Hyperparam_optimization.ipynb index c4d78a9..85483ca 100644 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/Hyperparam_optimization.ipynb +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/Hyperparam_optimization.ipynb @@ -18,10 +18,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "038a2787-7e91-473b-bc4b-338200529612", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", + " from .autonotebook import tqdm as notebook_tqdm\n" + ] + } + ], "source": [ "import numpy as np\n", "from tensorflow import keras as K\n", @@ -43,7 +52,7 @@ "outputs": [], "source": [ "import sys\n", - "sys.path.append(\"../../code/machine_learning/train/CNN_miRBind_2022/\")\n", + "sys.path.append(\"../../../code/machine_learning/train/CNN_miRBind_2022/\")\n", "\n", "from miRBind_CNN_architecture import miRBind_CNN" ] @@ -151,20 +160,293 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 20, "id": "e95fa7f9-103c-419e-a976-804af5bef9c8", "metadata": {}, "outputs": [], "source": [ - "DATASET = \"../../AmiRBench/code/dataset_vOct/Manakov_1_train_dataset.npy\"\n", - "LABELS = \"../../AmiRBench/code/dataset_vOct/Manakov_1_train_labels.npy\"\n", + "# DATASET = \"../../../AmiRBench/code/dataset_vOct/Manakov_1_train_dataset.npy\"\n", + "# DATASET = '../miRBind_CNN_retraining_orig_parameters/encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_dataset.npy'\n", + "DATASET = '../../miRBind_CNN_retraining_orig_parameters/encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_dataset.npy'\n", + "\n", + "# LABELS = \"../../../AmiRBench/code/dataset_vOct/Manakov_1_train_labels.npy\"\n", + "# LABELS = '../miRBind_CNN_retraining_orig_parameters/encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_labels.npy'\n", + "LABELS = '../../miRBind_CNN_retraining_orig_parameters/encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_labels.npy'\n", + "\n", "DATASET_RATIO = 1\n", - "DATASET_SIZE = 2524246" + "# DATASET_SIZE = 2524246\n", + "DATASET_SIZE = 2516195\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "bfe763bc-4015-4a24-9b92-1c6dde016549", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
genenoncodingRNAnoncodingRNA_namenoncodingRNA_famfeaturelabelchrstartendstrandgene_cluster_ID
0CTACCTGATCCGTTTACTCACTATGCCCCCTTGCCATCCTGGCCTT...CTGTACAGCCTCCTAGCTTTCChsa-let-7a-2-3plet-7exon110102141236.0102141285+22755
1CAGCCCATGCCATTGTTTCGGTGAACGGTACTACGATTGAAGGACA...CTATACAATCTACTGTCTTTChsa-let-7a-3plet-7exon110119577134.0119577183-129700
2CCTCAGCATTAAATGCTTTAGCAAATGACACATTAGACCTACCTCA...CTATACAATCTACTGTCTTTChsa-let-7a-3plet-7exon11330659280.030659329+10171
3GAAACCACGTATTTGGAGCCAGGAAAGATCAGTGTGAATTGTGGAC...CTATACAATCTACTGTCTTTChsa-let-7a-3plet-7exon11548738406.048738455-283322
4TGCAGTTTTCCCCTTGATTGGCGTGTGTGTATATATGGATAAATAT...CTATACAATCTACTGTCTTTChsa-let-7a-3plet-7exon12118108420.0118108469+43376
....................................
2516190AACAGCAAAACCAATTAAGAAACAATAATTAGGGCCAGGTGCCCTA...CACCCGTAGAACCGACCTTGCGhsa-miR-99b-5pmir-10intron0933276894.033276943+230577
2516191GTTGAATGCAGATGTGCTGAGTTAGAGGTGGGATTTGGAAAAGGGC...CACCCGTAGAACCGACCTTGCGhsa-miR-99b-5pmir-10three_prime_utr0642080276.042080325+497680
2516192ATTAAGGTAGCTTTGGTTTGGAAAACATACTCAGTATACAGAAACA...CACCCGTAGAACCGACCTTGCGhsa-miR-99b-5pmir-10intron0X155920076.0155920125+435974
2516193TTTGAGAAGTAGGAGAGCAGGGTGGTACCGTGTGGGCTCTTACCCT...CACCCGTAGAACCGACCTTGCGhsa-miR-99b-5pmir-10three_prime_utr02101270216.0101270265+331079
2516194CTGGCTTTAGCCAGCCTGGCCAGCCTCACTCTTGCCCTTGGTCATC...CACCCGTAGAACCGACCTTGCGhsa-miR-99b-5pmir-10NaN03156530482.0156530531-547491
\n", + "

2516195 rows × 11 columns

\n", + "
" + ], + "text/plain": [ + " gene \\\n", + "0 CTACCTGATCCGTTTACTCACTATGCCCCCTTGCCATCCTGGCCTT... \n", + "1 CAGCCCATGCCATTGTTTCGGTGAACGGTACTACGATTGAAGGACA... \n", + "2 CCTCAGCATTAAATGCTTTAGCAAATGACACATTAGACCTACCTCA... \n", + "3 GAAACCACGTATTTGGAGCCAGGAAAGATCAGTGTGAATTGTGGAC... \n", + "4 TGCAGTTTTCCCCTTGATTGGCGTGTGTGTATATATGGATAAATAT... \n", + "... ... \n", + "2516190 AACAGCAAAACCAATTAAGAAACAATAATTAGGGCCAGGTGCCCTA... \n", + "2516191 GTTGAATGCAGATGTGCTGAGTTAGAGGTGGGATTTGGAAAAGGGC... \n", + "2516192 ATTAAGGTAGCTTTGGTTTGGAAAACATACTCAGTATACAGAAACA... \n", + "2516193 TTTGAGAAGTAGGAGAGCAGGGTGGTACCGTGTGGGCTCTTACCCT... \n", + "2516194 CTGGCTTTAGCCAGCCTGGCCAGCCTCACTCTTGCCCTTGGTCATC... \n", + "\n", + " noncodingRNA noncodingRNA_name noncodingRNA_fam \\\n", + "0 CTGTACAGCCTCCTAGCTTTCC hsa-let-7a-2-3p let-7 \n", + "1 CTATACAATCTACTGTCTTTC hsa-let-7a-3p let-7 \n", + "2 CTATACAATCTACTGTCTTTC hsa-let-7a-3p let-7 \n", + "3 CTATACAATCTACTGTCTTTC hsa-let-7a-3p let-7 \n", + "4 CTATACAATCTACTGTCTTTC hsa-let-7a-3p let-7 \n", + "... ... ... ... \n", + "2516190 CACCCGTAGAACCGACCTTGCG hsa-miR-99b-5p mir-10 \n", + "2516191 CACCCGTAGAACCGACCTTGCG hsa-miR-99b-5p mir-10 \n", + "2516192 CACCCGTAGAACCGACCTTGCG hsa-miR-99b-5p mir-10 \n", + "2516193 CACCCGTAGAACCGACCTTGCG hsa-miR-99b-5p mir-10 \n", + "2516194 CACCCGTAGAACCGACCTTGCG hsa-miR-99b-5p mir-10 \n", + "\n", + " feature label chr start end strand \\\n", + "0 exon 1 10 102141236.0 102141285 + \n", + "1 exon 1 10 119577134.0 119577183 - \n", + "2 exon 1 13 30659280.0 30659329 + \n", + "3 exon 1 15 48738406.0 48738455 - \n", + "4 exon 1 2 118108420.0 118108469 + \n", + "... ... ... .. ... ... ... \n", + "2516190 intron 0 9 33276894.0 33276943 + \n", + "2516191 three_prime_utr 0 6 42080276.0 42080325 + \n", + "2516192 intron 0 X 155920076.0 155920125 + \n", + "2516193 three_prime_utr 0 2 101270216.0 101270265 + \n", + "2516194 NaN 0 3 156530482.0 156530531 - \n", + "\n", + " gene_cluster_ID \n", + "0 22755 \n", + "1 129700 \n", + "2 10171 \n", + "3 283322 \n", + "4 43376 \n", + "... ... \n", + "2516190 230577 \n", + "2516191 497680 \n", + "2516192 435974 \n", + "2516193 331079 \n", + "2516194 547491 \n", + "\n", + "[2516195 rows x 11 columns]" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd \n", + "pd.read_csv(\"../../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train.tsv\", sep='\\t')" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 21, "id": "448d3e9a-0ee5-483c-be44-e704d957434f", "metadata": {}, "outputs": [], @@ -193,7 +475,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 22, "id": "a90ef512-5b42-489c-a9ba-584c9877b662", "metadata": {}, "outputs": [], @@ -249,7 +531,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 23, "id": "4be25843-e179-44d9-9c36-4e500fdd62bf", "metadata": {}, "outputs": [], @@ -271,12 +553,265 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "id": "7c4c6d6d-fa85-47c3-9df9-a67fc9cbe8d2", "metadata": { "id": "vIEbdJxqydNm" }, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[I 2024-12-19 15:16:16,997] A new study created in memory with name: miRBind_CNN\n", + "2024-12-19 15:16:17.160889: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", + "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", + "2024-12-19 15:16:17.764088: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1532] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 600 MB memory: -> device: 0, name: NVIDIA A40, pci bus id: 0000:27:00.0, compute capability: 8.6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 1/50\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2024-12-19 15:16:20.660212: E tensorflow/stream_executor/cuda/cuda_dnn.cc:389] Could not create cudnn handle: CUDNN_STATUS_NOT_INITIALIZED\n", + "2024-12-19 15:16:20.660358: E tensorflow/stream_executor/cuda/cuda_dnn.cc:394] Error retrieving driver version: NOT_FOUND: could not find kernel module information in driver version file contents: \"NVRM version: NVIDIA UNIX Open Kernel Module for x86_64 565.57.01 Release Build (dvs-builder@U16-A24-9-2) Thu Oct 10 12:15:00 UTC 2024\n", + "GCC version: gcc version 12.3.0 (Ubuntu 12.3.0-1ubuntu1~22.04) \n", + "\"\n", + "2024-12-19 15:16:20.660409: W tensorflow/core/framework/op_kernel.cc:1745] OP_REQUIRES failed at conv_ops.cc:1120 : UNIMPLEMENTED: DNN library is not found.\n", + "[W 2024-12-19 15:16:20,727] Trial 0 failed with parameters: {'cnn_layers_num': 7, 'kernel_size': 6, 'pool_size': 5, 'dense_layers_num': 5, 'learning_rate': 0.05887436900846115} because of the following error: UnimplementedError().\n", + "Traceback (most recent call last):\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 197, in _run_trial\n", + " value_or_values = func(trial)\n", + " File \"/tmp/ipykernel_7812/1713806393.py\", line 18, in objective\n", + " model_history = model.fit(\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 67, in error_handler\n", + " raise e.with_traceback(filtered_tb) from None\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tensorflow/python/eager/execute.py\", line 54, in quick_execute\n", + " tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,\n", + "tensorflow.python.framework.errors_impl.UnimplementedError: Graph execution error:\n", + "\n", + "Detected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last):\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 194, in _run_module_as_main\n", + " return _run_code(code, main_globals, None,\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 87, in _run_code\n", + " exec(code, run_globals)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py\", line 17, in \n", + " app.launch_new_instance()\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py\", line 1043, in launch_instance\n", + " app.start()\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py\", line 725, in start\n", + " self.io_loop.start()\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py\", line 215, in start\n", + " self.asyncio_loop.run_forever()\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 570, in run_forever\n", + " self._run_once()\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 1859, in _run_once\n", + " handle._run()\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py\", line 81, in _run\n", + " self._context.run(self._callback, *self._args)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 513, in dispatch_queue\n", + " await self.process_one()\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 502, in process_one\n", + " await dispatch(*args)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 409, in dispatch_shell\n", + " await result\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 729, in execute_request\n", + " reply_content = await reply_content\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py\", line 422, in do_execute\n", + " res = shell.run_cell(\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py\", line 540, in run_cell\n", + " return super().run_cell(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 2961, in run_cell\n", + " result = self._run_cell(\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3016, in _run_cell\n", + " result = runner(coro)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py\", line 129, in _pseudo_sync_runner\n", + " coro.send(None)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3221, in run_cell_async\n", + " has_raised = await self.run_ast_nodes(code_ast.body, cell_name,\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3400, in run_ast_nodes\n", + " if await self.run_code(code, result, async_=asy):\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3460, in run_code\n", + " exec(code_obj, self.user_global_ns, self.user_ns)\n", + " File \"/tmp/ipykernel_7812/2048605125.py\", line 2, in \n", + " study.optimize(objective, n_trials=20)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py\", line 475, in optimize\n", + " _optimize(\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 63, in _optimize\n", + " _optimize_sequential(\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 160, in _optimize_sequential\n", + " frozen_trial = _run_trial(study, func, catch)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 197, in _run_trial\n", + " value_or_values = func(trial)\n", + " File \"/tmp/ipykernel_7812/1713806393.py\", line 18, in objective\n", + " model_history = model.fit(\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", + " return fn(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1409, in fit\n", + " tmp_logs = self.train_function(iterator)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1051, in train_function\n", + " return step_function(self, iterator)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1040, in step_function\n", + " outputs = model.distribute_strategy.run(run_step, args=(data,))\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1030, in run_step\n", + " outputs = model.train_step(data)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 889, in train_step\n", + " y_pred = self(x, training=True)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", + " return fn(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 490, in __call__\n", + " return super().__call__(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", + " return fn(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n", + " outputs = call_fn(inputs, *args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n", + " return fn(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 458, in call\n", + " return self._run_internal_graph(\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 596, in _run_internal_graph\n", + " outputs = node.layer(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", + " return fn(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n", + " outputs = call_fn(inputs, *args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n", + " return fn(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 250, in call\n", + " outputs = self.convolution_op(inputs, self.kernel)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 225, in convolution_op\n", + " return tf.nn.convolution(\n", + "Node: 'miRBind_CNN/conv2d/Conv2D'\n", + "Detected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last):\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 194, in _run_module_as_main\n", + " return _run_code(code, main_globals, None,\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 87, in _run_code\n", + " exec(code, run_globals)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py\", line 17, in \n", + " app.launch_new_instance()\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py\", line 1043, in launch_instance\n", + " app.start()\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py\", line 725, in start\n", + " self.io_loop.start()\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py\", line 215, in start\n", + " self.asyncio_loop.run_forever()\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 570, in run_forever\n", + " self._run_once()\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 1859, in _run_once\n", + " handle._run()\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py\", line 81, in _run\n", + " self._context.run(self._callback, *self._args)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 513, in dispatch_queue\n", + " await self.process_one()\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 502, in process_one\n", + " await dispatch(*args)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 409, in dispatch_shell\n", + " await result\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 729, in execute_request\n", + " reply_content = await reply_content\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py\", line 422, in do_execute\n", + " res = shell.run_cell(\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py\", line 540, in run_cell\n", + " return super().run_cell(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 2961, in run_cell\n", + " result = self._run_cell(\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3016, in _run_cell\n", + " result = runner(coro)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py\", line 129, in _pseudo_sync_runner\n", + " coro.send(None)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3221, in run_cell_async\n", + " has_raised = await self.run_ast_nodes(code_ast.body, cell_name,\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3400, in run_ast_nodes\n", + " if await self.run_code(code, result, async_=asy):\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3460, in run_code\n", + " exec(code_obj, self.user_global_ns, self.user_ns)\n", + " File \"/tmp/ipykernel_7812/2048605125.py\", line 2, in \n", + " study.optimize(objective, n_trials=20)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py\", line 475, in optimize\n", + " _optimize(\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 63, in _optimize\n", + " _optimize_sequential(\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 160, in _optimize_sequential\n", + " frozen_trial = _run_trial(study, func, catch)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 197, in _run_trial\n", + " value_or_values = func(trial)\n", + " File \"/tmp/ipykernel_7812/1713806393.py\", line 18, in objective\n", + " model_history = model.fit(\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", + " return fn(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1409, in fit\n", + " tmp_logs = self.train_function(iterator)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1051, in train_function\n", + " return step_function(self, iterator)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1040, in step_function\n", + " outputs = model.distribute_strategy.run(run_step, args=(data,))\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1030, in run_step\n", + " outputs = model.train_step(data)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 889, in train_step\n", + " y_pred = self(x, training=True)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", + " return fn(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 490, in __call__\n", + " return super().__call__(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", + " return fn(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n", + " outputs = call_fn(inputs, *args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n", + " return fn(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 458, in call\n", + " return self._run_internal_graph(\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 596, in _run_internal_graph\n", + " outputs = node.layer(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", + " return fn(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n", + " outputs = call_fn(inputs, *args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n", + " return fn(*args, **kwargs)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 250, in call\n", + " outputs = self.convolution_op(inputs, self.kernel)\n", + " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 225, in convolution_op\n", + " return tf.nn.convolution(\n", + "Node: 'miRBind_CNN/conv2d/Conv2D'\n", + "2 root error(s) found.\n", + " (0) UNIMPLEMENTED: DNN library is not found.\n", + "\t [[{{node miRBind_CNN/conv2d/Conv2D}}]]\n", + "\t [[assert_greater_equal/Assert/AssertGuard/pivot_f/_3/_41]]\n", + " (1) UNIMPLEMENTED: DNN library is not found.\n", + "\t [[{{node miRBind_CNN/conv2d/Conv2D}}]]\n", + "0 successful operations.\n", + "0 derived errors ignored. [Op:__inference_train_function_4880]\n", + "[W 2024-12-19 15:16:20,730] Trial 0 failed with value None.\n" + ] + }, + { + "ename": "UnimplementedError", + "evalue": "Graph execution error:\n\nDetected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 194, in _run_module_as_main\n return _run_code(code, main_globals, None,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 87, in _run_code\n exec(code, run_globals)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py\", line 17, in \n app.launch_new_instance()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py\", line 1043, in launch_instance\n app.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py\", line 725, in start\n self.io_loop.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py\", line 215, in start\n self.asyncio_loop.run_forever()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 570, in run_forever\n self._run_once()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 1859, in _run_once\n handle._run()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py\", line 81, in _run\n self._context.run(self._callback, *self._args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 513, in dispatch_queue\n await self.process_one()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 502, in process_one\n await dispatch(*args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 409, in dispatch_shell\n await result\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 729, in execute_request\n reply_content = await reply_content\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py\", line 422, in do_execute\n res = shell.run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py\", line 540, in run_cell\n return super().run_cell(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 2961, in run_cell\n result = self._run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3016, in _run_cell\n result = runner(coro)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py\", line 129, in _pseudo_sync_runner\n coro.send(None)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3221, in run_cell_async\n has_raised = await self.run_ast_nodes(code_ast.body, cell_name,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3400, in run_ast_nodes\n if await self.run_code(code, result, async_=asy):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3460, in run_code\n exec(code_obj, self.user_global_ns, self.user_ns)\n File \"/tmp/ipykernel_7812/2048605125.py\", line 2, in \n study.optimize(objective, n_trials=20)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py\", line 475, in optimize\n _optimize(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 63, in _optimize\n _optimize_sequential(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 160, in _optimize_sequential\n frozen_trial = _run_trial(study, func, catch)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 197, in _run_trial\n value_or_values = func(trial)\n File \"/tmp/ipykernel_7812/1713806393.py\", line 18, in objective\n model_history = model.fit(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1409, in fit\n tmp_logs = self.train_function(iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1051, in train_function\n return step_function(self, iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1040, in step_function\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1030, in run_step\n outputs = model.train_step(data)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 889, in train_step\n y_pred = self(x, training=True)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 490, in __call__\n return super().__call__(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 458, in call\n return self._run_internal_graph(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 596, in _run_internal_graph\n outputs = node.layer(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 250, in call\n outputs = self.convolution_op(inputs, self.kernel)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 225, in convolution_op\n return tf.nn.convolution(\nNode: 'miRBind_CNN/conv2d/Conv2D'\nDetected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 194, in _run_module_as_main\n return _run_code(code, main_globals, None,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 87, in _run_code\n exec(code, run_globals)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py\", line 17, in \n app.launch_new_instance()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py\", line 1043, in launch_instance\n app.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py\", line 725, in start\n self.io_loop.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py\", line 215, in start\n self.asyncio_loop.run_forever()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 570, in run_forever\n self._run_once()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 1859, in _run_once\n handle._run()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py\", line 81, in _run\n self._context.run(self._callback, *self._args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 513, in dispatch_queue\n await self.process_one()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 502, in process_one\n await dispatch(*args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 409, in dispatch_shell\n await result\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 729, in execute_request\n reply_content = await reply_content\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py\", line 422, in do_execute\n res = shell.run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py\", line 540, in run_cell\n return super().run_cell(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 2961, in run_cell\n result = self._run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3016, in _run_cell\n result = runner(coro)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py\", line 129, in _pseudo_sync_runner\n coro.send(None)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3221, in run_cell_async\n has_raised = await self.run_ast_nodes(code_ast.body, cell_name,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3400, in run_ast_nodes\n if await self.run_code(code, result, async_=asy):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3460, in run_code\n exec(code_obj, self.user_global_ns, self.user_ns)\n File \"/tmp/ipykernel_7812/2048605125.py\", line 2, in \n study.optimize(objective, n_trials=20)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py\", line 475, in optimize\n _optimize(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 63, in _optimize\n _optimize_sequential(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 160, in _optimize_sequential\n frozen_trial = _run_trial(study, func, catch)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 197, in _run_trial\n value_or_values = func(trial)\n File \"/tmp/ipykernel_7812/1713806393.py\", line 18, in objective\n model_history = model.fit(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1409, in fit\n tmp_logs = self.train_function(iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1051, in train_function\n return step_function(self, iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1040, in step_function\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1030, in run_step\n outputs = model.train_step(data)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 889, in train_step\n y_pred = self(x, training=True)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 490, in __call__\n return super().__call__(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 458, in call\n return self._run_internal_graph(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 596, in _run_internal_graph\n outputs = node.layer(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 250, in call\n outputs = self.convolution_op(inputs, self.kernel)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 225, in convolution_op\n return tf.nn.convolution(\nNode: 'miRBind_CNN/conv2d/Conv2D'\n2 root error(s) found.\n (0) UNIMPLEMENTED: DNN library is not found.\n\t [[{{node miRBind_CNN/conv2d/Conv2D}}]]\n\t [[assert_greater_equal/Assert/AssertGuard/pivot_f/_3/_41]]\n (1) UNIMPLEMENTED: DNN library is not found.\n\t [[{{node miRBind_CNN/conv2d/Conv2D}}]]\n0 successful operations.\n0 derived errors ignored. [Op:__inference_train_function_4880]", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mUnimplementedError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[24], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m study \u001b[38;5;241m=\u001b[39m optuna\u001b[38;5;241m.\u001b[39mcreate_study(direction\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmaximize\u001b[39m\u001b[38;5;124m'\u001b[39m, study_name\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmiRBind_CNN\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m \u001b[43mstudy\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moptimize\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobjective\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mn_trials\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m20\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py:475\u001b[0m, in \u001b[0;36mStudy.optimize\u001b[0;34m(self, func, n_trials, timeout, n_jobs, catch, callbacks, gc_after_trial, show_progress_bar)\u001b[0m\n\u001b[1;32m 373\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21moptimize\u001b[39m(\n\u001b[1;32m 374\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 375\u001b[0m func: ObjectiveFuncType,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 382\u001b[0m show_progress_bar: \u001b[38;5;28mbool\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[1;32m 383\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 384\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Optimize an objective function.\u001b[39;00m\n\u001b[1;32m 385\u001b[0m \n\u001b[1;32m 386\u001b[0m \u001b[38;5;124;03m Optimization is done by choosing a suitable set of hyperparameter values from a given\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 473\u001b[0m \u001b[38;5;124;03m If nested invocation of this method occurs.\u001b[39;00m\n\u001b[1;32m 474\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 475\u001b[0m \u001b[43m_optimize\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 476\u001b[0m \u001b[43m \u001b[49m\u001b[43mstudy\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 477\u001b[0m \u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 478\u001b[0m \u001b[43m \u001b[49m\u001b[43mn_trials\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mn_trials\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 479\u001b[0m \u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 480\u001b[0m \u001b[43m \u001b[49m\u001b[43mn_jobs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mn_jobs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 481\u001b[0m \u001b[43m \u001b[49m\u001b[43mcatch\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mtuple\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43misinstance\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mIterable\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m,\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 482\u001b[0m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 483\u001b[0m \u001b[43m \u001b[49m\u001b[43mgc_after_trial\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mgc_after_trial\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 484\u001b[0m \u001b[43m \u001b[49m\u001b[43mshow_progress_bar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mshow_progress_bar\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 485\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py:63\u001b[0m, in \u001b[0;36m_optimize\u001b[0;34m(study, func, n_trials, timeout, n_jobs, catch, callbacks, gc_after_trial, show_progress_bar)\u001b[0m\n\u001b[1;32m 61\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 62\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m n_jobs \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m---> 63\u001b[0m \u001b[43m_optimize_sequential\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 64\u001b[0m \u001b[43m \u001b[49m\u001b[43mstudy\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 65\u001b[0m \u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 66\u001b[0m \u001b[43m \u001b[49m\u001b[43mn_trials\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 67\u001b[0m \u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 68\u001b[0m \u001b[43m \u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 69\u001b[0m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 70\u001b[0m \u001b[43m \u001b[49m\u001b[43mgc_after_trial\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 71\u001b[0m \u001b[43m \u001b[49m\u001b[43mreseed_sampler_rng\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 72\u001b[0m \u001b[43m \u001b[49m\u001b[43mtime_start\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 73\u001b[0m \u001b[43m \u001b[49m\u001b[43mprogress_bar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mprogress_bar\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 74\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 75\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 76\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m n_jobs \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m:\n", + "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py:160\u001b[0m, in \u001b[0;36m_optimize_sequential\u001b[0;34m(study, func, n_trials, timeout, catch, callbacks, gc_after_trial, reseed_sampler_rng, time_start, progress_bar)\u001b[0m\n\u001b[1;32m 157\u001b[0m \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 160\u001b[0m frozen_trial \u001b[38;5;241m=\u001b[39m \u001b[43m_run_trial\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstudy\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 161\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m 162\u001b[0m \u001b[38;5;66;03m# The following line mitigates memory problems that can be occurred in some\u001b[39;00m\n\u001b[1;32m 163\u001b[0m \u001b[38;5;66;03m# environments (e.g., services that use computing containers such as GitHub Actions).\u001b[39;00m\n\u001b[1;32m 164\u001b[0m \u001b[38;5;66;03m# Please refer to the following PR for further details:\u001b[39;00m\n\u001b[1;32m 165\u001b[0m \u001b[38;5;66;03m# https://github.com/optuna/optuna/pull/325.\u001b[39;00m\n\u001b[1;32m 166\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m gc_after_trial:\n", + "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py:248\u001b[0m, in \u001b[0;36m_run_trial\u001b[0;34m(study, func, catch)\u001b[0m\n\u001b[1;32m 241\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mShould not reach.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 243\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\n\u001b[1;32m 244\u001b[0m frozen_trial\u001b[38;5;241m.\u001b[39mstate \u001b[38;5;241m==\u001b[39m TrialState\u001b[38;5;241m.\u001b[39mFAIL\n\u001b[1;32m 245\u001b[0m \u001b[38;5;129;01mand\u001b[39;00m func_err \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 246\u001b[0m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(func_err, catch)\n\u001b[1;32m 247\u001b[0m ):\n\u001b[0;32m--> 248\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m func_err\n\u001b[1;32m 249\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m frozen_trial\n", + "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py:197\u001b[0m, in \u001b[0;36m_run_trial\u001b[0;34m(study, func, catch)\u001b[0m\n\u001b[1;32m 195\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m get_heartbeat_thread(trial\u001b[38;5;241m.\u001b[39m_trial_id, study\u001b[38;5;241m.\u001b[39m_storage):\n\u001b[1;32m 196\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 197\u001b[0m value_or_values \u001b[38;5;241m=\u001b[39m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtrial\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 198\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m exceptions\u001b[38;5;241m.\u001b[39mTrialPruned \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 199\u001b[0m \u001b[38;5;66;03m# TODO(mamu): Handle multi-objective cases.\u001b[39;00m\n\u001b[1;32m 200\u001b[0m state \u001b[38;5;241m=\u001b[39m TrialState\u001b[38;5;241m.\u001b[39mPRUNED\n", + "Cell \u001b[0;32mIn[22], line 18\u001b[0m, in \u001b[0;36mobjective\u001b[0;34m(trial)\u001b[0m\n\u001b[1;32m 15\u001b[0m lr \u001b[38;5;241m=\u001b[39m trial\u001b[38;5;241m.\u001b[39msuggest_float(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlearning_rate\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;241m0.00001\u001b[39m, \u001b[38;5;241m0.1\u001b[39m) \n\u001b[1;32m 16\u001b[0m model \u001b[38;5;241m=\u001b[39m compile_model(model, lr\u001b[38;5;241m=\u001b[39mlr)\n\u001b[0;32m---> 18\u001b[0m model_history \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 19\u001b[0m \u001b[43m \u001b[49m\u001b[43mtrain_data_gen\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 20\u001b[0m \u001b[43m \u001b[49m\u001b[43mvalidation_data\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mval_data_gen\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 21\u001b[0m \u001b[43m \u001b[49m\u001b[43mepochs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m50\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 22\u001b[0m \u001b[43m \u001b[49m\u001b[43mclass_weight\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m{\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mDATASET_RATIO\u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 23\u001b[0m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43mTFKerasPruningCallback\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtrial\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mval_auc\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;66;43;03m# get rid of attempts with unpromising hyperparam combination\u001b[39;49;00m\n\u001b[1;32m 24\u001b[0m \u001b[43m \u001b[49m\u001b[43mK\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mEarlyStopping\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpatience\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrestore_best_weights\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 25\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 27\u001b[0m num_epochs_trained \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39margmax(model_history\u001b[38;5;241m.\u001b[39mhistory[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mval_auc\u001b[39m\u001b[38;5;124m'\u001b[39m])\n\u001b[1;32m 28\u001b[0m val_auc \u001b[38;5;241m=\u001b[39m model_history\u001b[38;5;241m.\u001b[39mhistory[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mval_auc\u001b[39m\u001b[38;5;124m'\u001b[39m][num_epochs_trained]\n", + "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py:67\u001b[0m, in \u001b[0;36mfilter_traceback..error_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 65\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e: \u001b[38;5;66;03m# pylint: disable=broad-except\u001b[39;00m\n\u001b[1;32m 66\u001b[0m filtered_tb \u001b[38;5;241m=\u001b[39m _process_traceback_frames(e\u001b[38;5;241m.\u001b[39m__traceback__)\n\u001b[0;32m---> 67\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\u001b[38;5;241m.\u001b[39mwith_traceback(filtered_tb) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28mNone\u001b[39m\n\u001b[1;32m 68\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m 69\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m filtered_tb\n", + "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tensorflow/python/eager/execute.py:54\u001b[0m, in \u001b[0;36mquick_execute\u001b[0;34m(op_name, num_outputs, inputs, attrs, ctx, name)\u001b[0m\n\u001b[1;32m 52\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 53\u001b[0m ctx\u001b[38;5;241m.\u001b[39mensure_initialized()\n\u001b[0;32m---> 54\u001b[0m tensors \u001b[38;5;241m=\u001b[39m pywrap_tfe\u001b[38;5;241m.\u001b[39mTFE_Py_Execute(ctx\u001b[38;5;241m.\u001b[39m_handle, device_name, op_name,\n\u001b[1;32m 55\u001b[0m inputs, attrs, num_outputs)\n\u001b[1;32m 56\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m core\u001b[38;5;241m.\u001b[39m_NotOkStatusException \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 57\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m name \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", + "\u001b[0;31mUnimplementedError\u001b[0m: Graph execution error:\n\nDetected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 194, in _run_module_as_main\n return _run_code(code, main_globals, None,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 87, in _run_code\n exec(code, run_globals)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py\", line 17, in \n app.launch_new_instance()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py\", line 1043, in launch_instance\n app.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py\", line 725, in start\n self.io_loop.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py\", line 215, in start\n self.asyncio_loop.run_forever()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 570, in run_forever\n self._run_once()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 1859, in _run_once\n handle._run()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py\", line 81, in _run\n self._context.run(self._callback, *self._args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 513, in dispatch_queue\n await self.process_one()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 502, in process_one\n await dispatch(*args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 409, in dispatch_shell\n await result\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 729, in execute_request\n reply_content = await reply_content\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py\", line 422, in do_execute\n res = shell.run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py\", line 540, in run_cell\n return super().run_cell(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 2961, in run_cell\n result = self._run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3016, in _run_cell\n result = runner(coro)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py\", line 129, in _pseudo_sync_runner\n coro.send(None)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3221, in run_cell_async\n has_raised = await self.run_ast_nodes(code_ast.body, cell_name,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3400, in run_ast_nodes\n if await self.run_code(code, result, async_=asy):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3460, in run_code\n exec(code_obj, self.user_global_ns, self.user_ns)\n File \"/tmp/ipykernel_7812/2048605125.py\", line 2, in \n study.optimize(objective, n_trials=20)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py\", line 475, in optimize\n _optimize(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 63, in _optimize\n _optimize_sequential(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 160, in _optimize_sequential\n frozen_trial = _run_trial(study, func, catch)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 197, in _run_trial\n value_or_values = func(trial)\n File \"/tmp/ipykernel_7812/1713806393.py\", line 18, in objective\n model_history = model.fit(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1409, in fit\n tmp_logs = self.train_function(iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1051, in train_function\n return step_function(self, iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1040, in step_function\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1030, in run_step\n outputs = model.train_step(data)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 889, in train_step\n y_pred = self(x, training=True)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 490, in __call__\n return super().__call__(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 458, in call\n return self._run_internal_graph(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 596, in _run_internal_graph\n outputs = node.layer(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 250, in call\n outputs = self.convolution_op(inputs, self.kernel)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 225, in convolution_op\n return tf.nn.convolution(\nNode: 'miRBind_CNN/conv2d/Conv2D'\nDetected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 194, in _run_module_as_main\n return _run_code(code, main_globals, None,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 87, in _run_code\n exec(code, run_globals)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py\", line 17, in \n app.launch_new_instance()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py\", line 1043, in launch_instance\n app.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py\", line 725, in start\n self.io_loop.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py\", line 215, in start\n self.asyncio_loop.run_forever()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 570, in run_forever\n self._run_once()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 1859, in _run_once\n handle._run()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py\", line 81, in _run\n self._context.run(self._callback, *self._args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 513, in dispatch_queue\n await self.process_one()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 502, in process_one\n await dispatch(*args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 409, in dispatch_shell\n await result\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 729, in execute_request\n reply_content = await reply_content\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py\", line 422, in do_execute\n res = shell.run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py\", line 540, in run_cell\n return super().run_cell(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 2961, in run_cell\n result = self._run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3016, in _run_cell\n result = runner(coro)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py\", line 129, in _pseudo_sync_runner\n coro.send(None)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3221, in run_cell_async\n has_raised = await self.run_ast_nodes(code_ast.body, cell_name,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3400, in run_ast_nodes\n if await self.run_code(code, result, async_=asy):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3460, in run_code\n exec(code_obj, self.user_global_ns, self.user_ns)\n File \"/tmp/ipykernel_7812/2048605125.py\", line 2, in \n study.optimize(objective, n_trials=20)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py\", line 475, in optimize\n _optimize(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 63, in _optimize\n _optimize_sequential(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 160, in _optimize_sequential\n frozen_trial = _run_trial(study, func, catch)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 197, in _run_trial\n value_or_values = func(trial)\n File \"/tmp/ipykernel_7812/1713806393.py\", line 18, in objective\n model_history = model.fit(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1409, in fit\n tmp_logs = self.train_function(iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1051, in train_function\n return step_function(self, iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1040, in step_function\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1030, in run_step\n outputs = model.train_step(data)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 889, in train_step\n y_pred = self(x, training=True)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 490, in __call__\n return super().__call__(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 458, in call\n return self._run_internal_graph(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 596, in _run_internal_graph\n outputs = node.layer(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 250, in call\n outputs = self.convolution_op(inputs, self.kernel)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 225, in convolution_op\n return tf.nn.convolution(\nNode: 'miRBind_CNN/conv2d/Conv2D'\n2 root error(s) found.\n (0) UNIMPLEMENTED: DNN library is not found.\n\t [[{{node miRBind_CNN/conv2d/Conv2D}}]]\n\t [[assert_greater_equal/Assert/AssertGuard/pivot_f/_3/_41]]\n (1) UNIMPLEMENTED: DNN library is not found.\n\t [[{{node miRBind_CNN/conv2d/Conv2D}}]]\n0 successful operations.\n0 derived errors ignored. [Op:__inference_train_function_4880]" + ] + } + ], "source": [ "study = optuna.create_study(direction='maximize', study_name='miRBind_CNN')\n", "study.optimize(objective, n_trials=20)" @@ -362,21 +897,51 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 25, "id": "122b996f-24c8-49d2-8ded-0e54453081f9", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[W 2024-12-19 15:16:20,937] There are no complete trials.\n" + ] + }, + { + "ename": "ValueError", + "evalue": "\nImage export using the \"kaleido\" engine requires the kaleido package,\nwhich can be installed using pip:\n $ pip install -U kaleido\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[25], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m fig \u001b[38;5;241m=\u001b[39m vis\u001b[38;5;241m.\u001b[39mplot_optimization_history(study)\n\u001b[0;32m----> 2\u001b[0m \u001b[43mfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwrite_image\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43moptimization_history.png\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/plotly/basedatatypes.py:3821\u001b[0m, in \u001b[0;36mBaseFigure.write_image\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 3761\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 3762\u001b[0m \u001b[38;5;124;03mConvert a figure to a static image and write it to a file or writeable\u001b[39;00m\n\u001b[1;32m 3763\u001b[0m \u001b[38;5;124;03mobject\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 3817\u001b[0m \u001b[38;5;124;03mNone\u001b[39;00m\n\u001b[1;32m 3818\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 3819\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mplotly\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mio\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mpio\u001b[39;00m\n\u001b[0;32m-> 3821\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mpio\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwrite_image\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/plotly/io/_kaleido.py:267\u001b[0m, in \u001b[0;36mwrite_image\u001b[0;34m(fig, file, format, scale, width, height, validate, engine)\u001b[0m\n\u001b[1;32m 251\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 252\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 253\u001b[0m \u001b[38;5;124;03mCannot infer image type from output path '{file}'.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 261\u001b[0m )\n\u001b[1;32m 262\u001b[0m )\n\u001b[1;32m 264\u001b[0m \u001b[38;5;66;03m# Request image\u001b[39;00m\n\u001b[1;32m 265\u001b[0m \u001b[38;5;66;03m# -------------\u001b[39;00m\n\u001b[1;32m 266\u001b[0m \u001b[38;5;66;03m# Do this first so we don't create a file if image conversion fails\u001b[39;00m\n\u001b[0;32m--> 267\u001b[0m img_data \u001b[38;5;241m=\u001b[39m \u001b[43mto_image\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 268\u001b[0m \u001b[43m \u001b[49m\u001b[43mfig\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 269\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mformat\u001b[39;49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mformat\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 270\u001b[0m \u001b[43m \u001b[49m\u001b[43mscale\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mscale\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 271\u001b[0m \u001b[43m \u001b[49m\u001b[43mwidth\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwidth\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 272\u001b[0m \u001b[43m \u001b[49m\u001b[43mheight\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mheight\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 273\u001b[0m \u001b[43m \u001b[49m\u001b[43mvalidate\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mvalidate\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 274\u001b[0m \u001b[43m \u001b[49m\u001b[43mengine\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mengine\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 275\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 277\u001b[0m \u001b[38;5;66;03m# Open file\u001b[39;00m\n\u001b[1;32m 278\u001b[0m \u001b[38;5;66;03m# ---------\u001b[39;00m\n\u001b[1;32m 279\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m path \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 280\u001b[0m \u001b[38;5;66;03m# We previously failed to make sense of `file` as a pathlib object.\u001b[39;00m\n\u001b[1;32m 281\u001b[0m \u001b[38;5;66;03m# Attempt to write to `file` as an open file descriptor.\u001b[39;00m\n", + "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/plotly/io/_kaleido.py:133\u001b[0m, in \u001b[0;36mto_image\u001b[0;34m(fig, format, width, height, scale, validate, engine)\u001b[0m\n\u001b[1;32m 131\u001b[0m \u001b[38;5;66;03m# Raise informative error message if Kaleido is not installed\u001b[39;00m\n\u001b[1;32m 132\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m scope \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 133\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 134\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 135\u001b[0m \u001b[38;5;124;03mImage export using the \"kaleido\" engine requires the kaleido package,\u001b[39;00m\n\u001b[1;32m 136\u001b[0m \u001b[38;5;124;03mwhich can be installed using pip:\u001b[39;00m\n\u001b[1;32m 137\u001b[0m \u001b[38;5;124;03m $ pip install -U kaleido\u001b[39;00m\n\u001b[1;32m 138\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 139\u001b[0m )\n\u001b[1;32m 141\u001b[0m \u001b[38;5;66;03m# Validate figure\u001b[39;00m\n\u001b[1;32m 142\u001b[0m \u001b[38;5;66;03m# ---------------\u001b[39;00m\n\u001b[1;32m 143\u001b[0m fig_dict \u001b[38;5;241m=\u001b[39m validate_coerce_fig_to_dict(fig, validate)\n", + "\u001b[0;31mValueError\u001b[0m: \nImage export using the \"kaleido\" engine requires the kaleido package,\nwhich can be installed using pip:\n $ pip install -U kaleido\n" + ] + } + ], "source": [ "fig = vis.plot_optimization_history(study)\n", "fig.write_image(\"optimization_history.png\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9e02ff5e-d791-4fe3-91c8-99900ad682ff", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python [conda env:miRBench2]", + "display_name": "Python [conda env:deepExperimentTF2.7]", "language": "python", - "name": "conda-env-miRBench2-py" + "name": "conda-env-deepExperimentTF2.7-py" }, "language_info": { "codemirror_mode": { @@ -388,7 +953,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.8.16" } }, "nbformat": 4, diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization-tmp_made_from_notebook.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization-tmp_made_from_notebook.py new file mode 100644 index 0000000..ed021a9 --- /dev/null +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization-tmp_made_from_notebook.py @@ -0,0 +1,202 @@ +# Hyperparam optimization notebook +""" +In this notebook, we will try to optimize the hyperparameters of the miRBind CNN model. Quick guide to what is a [parameter vs. hyperparameter](https://machinelearningmastery.com/difference-between-a-parameter-and-a-hyperparameter/). + +We will use [Optuna](https://optuna.org/) framework for this. It will try for us a bunch of different hyperparameter settings and see what combination works the best. + +Let's try to optimize number of blocks with convolution layer, kernel size of the convolution, size of the pooling layer, number of blocks with the dense layer and learning rate - these are our hyperparameters. + +Our metrics to optimize will be the AU PRC on the validation set (we split the train set into actual training set and validation set). +""" + +import numpy as np +from tensorflow import keras as K +from tensorflow.keras.optimizers import Adam +from tensorflow.keras.utils import Sequence + +import plotly +import logging +import optuna +import optuna.visualization as vis +from optuna.integration import TFKerasPruningCallback + +import sys +sys.path.append("../../../code/machine_learning/train/CNN_miRBind_2022/") + +from miRBind_CNN_architecture import miRBind_CNN + +# it's here for to be able to display plots in jupyter notebook +plotly.io.renderers.default = 'iframe' + +def compile_model(model, lr): + + opt = Adam( + learning_rate=lr, + beta_1=0.9, + beta_2=0.999, + epsilon=1e-07, + amsgrad=False, + name="Adam") + + model.compile( + optimizer=opt, + loss='binary_crossentropy', + metrics=['accuracy', K.metrics.AUC(curve='PR')] # adding the metrics on which we want to optimize + ) + return model + +class DataGenerator(Sequence): + def __init__(self, data_path, labels_path, dataset_size, batch_size=32, validation_split=0.1, is_validation=False, shuffle=True): + # preload the encoded numpy data + # the size needed to properly load the array + self.size = dataset_size + + self.data = np.memmap(data_path, dtype='float32', mode='r', shape=(self.size, 50, 20, 1)) + self.labels = np.memmap(labels_path, dtype='float32', mode='r', shape=(self.size,)) + self.batch_size = batch_size + self.shuffle = shuffle + + # Determine number of train and validation samples + self.validation_split = validation_split + self.num_samples = len(self.data) + self.num_validation_samples = int(self.num_samples * validation_split) + self.num_train_samples = self.num_samples - self.num_validation_samples + + # Determine indices for validation and training + indices = np.arange(self.num_samples) + if shuffle: + np.random.shuffle(indices) + + if is_validation: + self.indices = indices[self.num_train_samples:] + else: + self.indices = indices[:self.num_train_samples] + + # Shuffle the data initially + self.on_epoch_end() + + def __len__(self): + # Denotes the number of batches per epoch + return int(np.ceil(len(self.indices) / float(self.batch_size))) + + def __getitem__(self, idx): + # Generate one batch of data + batch_indices = self.indices[idx * self.batch_size:(idx + 1) * self.batch_size] + batch_data = self.data[batch_indices] + batch_labels = self.labels[batch_indices] + return batch_data, batch_labels + + def on_epoch_end(self): + # Updates indices after each epoch for shuffling + if self.shuffle: + np.random.shuffle(self.indices) + +"""------------------------------ +Choose a dataset on which you want to train. It has to be already encoded with the ```binding_2D_matrix_encoder.py``` +""" + +# DATASET = "../../../AmiRBench/code/dataset_vOct/Manakov_1_train_dataset.npy" +# DATASET = '../miRBind_CNN_retraining_orig_parameters/encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_dataset.npy' +DATASET = '../../miRBind_CNN_retraining_orig_parameters/encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_dataset.npy' + +# LABELS = "../../../AmiRBench/code/dataset_vOct/Manakov_1_train_labels.npy" +# LABELS = '../miRBind_CNN_retraining_orig_parameters/encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_labels.npy' +LABELS = '../../miRBind_CNN_retraining_orig_parameters/encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_labels.npy' + +DATASET_RATIO = 1 +# DATASET_SIZE = 2524246 +DATASET_SIZE = 2516195 + +import pandas as pd +pd.read_csv("../../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train.tsv", sep='\t') + +train_data_gen = DataGenerator(DATASET, LABELS, dataset_size=DATASET_SIZE, validation_split=0.1, is_validation=False) + +val_data_gen = DataGenerator(DATASET, LABELS, dataset_size=DATASET_SIZE, validation_split=0.1, is_validation=True) + +"""---------------------------- +This is the function that creates a model with suggested hyperparameters, trains it and sees how well it performs on the validation set + +**Some explanations** + +`trial` is the object that "carries the information" about the hyperparameter optimization. `trial.suggest_` means "give me some value for the hyperparameter" that might work well for the model. + +`TFKerasPruningCallback` is another hack, where you can stop unpromising training in the middle and scratch it. E.g. when you are training the model with some hyperparameters and after few epochs you see the model doesn't learn anything, you can simply stop the training, remember that this set of hyperparameters didn't work well and you don't have to waste time with worthless training finishing. + +`K.callbacks.EarlyStopping` - early stopping method helps to train for the right amount of epochs. It monitors the performance on the validation set and when the model starts overfitting and performing worse, it stops the training. +""" + +best_model = None +best_val_auc = 0 + +def objective(trial): + global best_model, best_val_auc + + K.backend.clear_session() + + # build the model based on suggested hyperparameters + cnn_num = trial.suggest_int('cnn_layers_num', 2, 10) + kernel_size = trial.suggest_int('kernel_size', 3, 10) + pool_size = trial.suggest_int('pool_size', 1, 8) + dense_num = trial.suggest_int('dense_layers_num', 2, cnn_num) + model = miRBind_CNN(cnn_num=cnn_num, kernel_size=kernel_size, pool_size=pool_size, dense_num=dense_num).model + lr = trial.suggest_float('learning_rate', 0.00001, 0.1) + model = compile_model(model, lr=lr) + + model_history = model.fit( + train_data_gen, + validation_data=val_data_gen, + epochs=50, + class_weight={0: 1, 1: DATASET_RATIO}, + callbacks=[TFKerasPruningCallback(trial, "val_auc"), # get rid of attempts with unpromising hyperparam combination + K.callbacks.EarlyStopping(patience=5, restore_best_weights=True)], + ) + + num_epochs_trained = np.argmax(model_history.history['val_auc']) + val_auc = model_history.history['val_auc'][num_epochs_trained] + + # check performance of this trial + if val_auc > best_val_auc: + best_val_auc = val_auc + best_model = model # save the current best model + model.save('best_model.keras') # save the model to disk + logger.info(f"New best model found and saved with Validation AUC: {val_auc}") + + print(f"Validation AU PRC: {val_auc}") + + return val_auc + +"""Set up a logger for logging the optimization process to a file""" + +logger = logging.getLogger('optuna') +logger.setLevel(logging.INFO) +file_handler = logging.FileHandler('hyperparam_optimization.log', 'w') +file_handler.setFormatter(logging.Formatter('%(asctime)s - %(message)s')) +logger.addHandler(file_handler) + +"""This is the place where we start running the optimization process""" + +study = optuna.create_study(direction='maximize', study_name='miRBind_CNN') +study.optimize(objective, n_trials=20) + +logger.info("\n") +logger.info(f"Best hyperparameters: {study.best_params}") +logger.info(f"Best value (validation AU PRC): {study.best_value}") + +"""Let's plot now how the optimization process went, what was the best set of hyperparameters etc.""" + +vis.plot_optimization_history(study) + +vis.plot_optimization_history(study) + +vis.plot_contour(study) + +vis.plot_param_importances(study) + +vis.plot_slice(study) + +"""If you want to, you can save the plots like this:""" + +fig = vis.plot_optimization_history(study) +fig.write_image("optimization_history.png") + diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.log b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.log new file mode 100644 index 0000000..aa801f1 --- /dev/null +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.log @@ -0,0 +1,207 @@ +2024-12-19 15:16:16,997 - A new study created in memory with name: miRBind_CNN +2024-12-19 15:16:20,727 - Trial 0 failed with parameters: {'cnn_layers_num': 7, 'kernel_size': 6, 'pool_size': 5, 'dense_layers_num': 5, 'learning_rate': 0.05887436900846115} because of the following error: UnimplementedError(). +Traceback (most recent call last): + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py", line 197, in _run_trial + value_or_values = func(trial) + File "/tmp/ipykernel_7812/1713806393.py", line 18, in objective + model_history = model.fit( + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler + raise e.with_traceback(filtered_tb) from None + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tensorflow/python/eager/execute.py", line 54, in quick_execute + tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, +tensorflow.python.framework.errors_impl.UnimplementedError: Graph execution error: + +Detected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last): + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py", line 194, in _run_module_as_main + return _run_code(code, main_globals, None, + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py", line 87, in _run_code + exec(code, run_globals) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py", line 17, in + app.launch_new_instance() + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py", line 1043, in launch_instance + app.start() + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py", line 725, in start + self.io_loop.start() + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py", line 215, in start + self.asyncio_loop.run_forever() + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py", line 570, in run_forever + self._run_once() + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py", line 1859, in _run_once + handle._run() + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py", line 81, in _run + self._context.run(self._callback, *self._args) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 513, in dispatch_queue + await self.process_one() + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 502, in process_one + await dispatch(*args) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 409, in dispatch_shell + await result + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 729, in execute_request + reply_content = await reply_content + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py", line 422, in do_execute + res = shell.run_cell( + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py", line 540, in run_cell + return super().run_cell(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 2961, in run_cell + result = self._run_cell( + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3016, in _run_cell + result = runner(coro) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner + coro.send(None) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3221, in run_cell_async + has_raised = await self.run_ast_nodes(code_ast.body, cell_name, + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3400, in run_ast_nodes + if await self.run_code(code, result, async_=asy): + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3460, in run_code + exec(code_obj, self.user_global_ns, self.user_ns) + File "/tmp/ipykernel_7812/2048605125.py", line 2, in + study.optimize(objective, n_trials=20) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py", line 475, in optimize + _optimize( + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py", line 63, in _optimize + _optimize_sequential( + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py", line 160, in _optimize_sequential + frozen_trial = _run_trial(study, func, catch) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py", line 197, in _run_trial + value_or_values = func(trial) + File "/tmp/ipykernel_7812/1713806393.py", line 18, in objective + model_history = model.fit( + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler + return fn(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1409, in fit + tmp_logs = self.train_function(iterator) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1051, in train_function + return step_function(self, iterator) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1040, in step_function + outputs = model.distribute_strategy.run(run_step, args=(data,)) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1030, in run_step + outputs = model.train_step(data) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 889, in train_step + y_pred = self(x, training=True) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler + return fn(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 490, in __call__ + return super().__call__(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler + return fn(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py", line 1014, in __call__ + outputs = call_fn(inputs, *args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler + return fn(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py", line 458, in call + return self._run_internal_graph( + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py", line 596, in _run_internal_graph + outputs = node.layer(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler + return fn(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py", line 1014, in __call__ + outputs = call_fn(inputs, *args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler + return fn(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py", line 250, in call + outputs = self.convolution_op(inputs, self.kernel) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py", line 225, in convolution_op + return tf.nn.convolution( +Node: 'miRBind_CNN/conv2d/Conv2D' +Detected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last): + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py", line 194, in _run_module_as_main + return _run_code(code, main_globals, None, + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py", line 87, in _run_code + exec(code, run_globals) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py", line 17, in + app.launch_new_instance() + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py", line 1043, in launch_instance + app.start() + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py", line 725, in start + self.io_loop.start() + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py", line 215, in start + self.asyncio_loop.run_forever() + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py", line 570, in run_forever + self._run_once() + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py", line 1859, in _run_once + handle._run() + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py", line 81, in _run + self._context.run(self._callback, *self._args) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 513, in dispatch_queue + await self.process_one() + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 502, in process_one + await dispatch(*args) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 409, in dispatch_shell + await result + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 729, in execute_request + reply_content = await reply_content + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py", line 422, in do_execute + res = shell.run_cell( + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py", line 540, in run_cell + return super().run_cell(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 2961, in run_cell + result = self._run_cell( + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3016, in _run_cell + result = runner(coro) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner + coro.send(None) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3221, in run_cell_async + has_raised = await self.run_ast_nodes(code_ast.body, cell_name, + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3400, in run_ast_nodes + if await self.run_code(code, result, async_=asy): + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3460, in run_code + exec(code_obj, self.user_global_ns, self.user_ns) + File "/tmp/ipykernel_7812/2048605125.py", line 2, in + study.optimize(objective, n_trials=20) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py", line 475, in optimize + _optimize( + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py", line 63, in _optimize + _optimize_sequential( + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py", line 160, in _optimize_sequential + frozen_trial = _run_trial(study, func, catch) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py", line 197, in _run_trial + value_or_values = func(trial) + File "/tmp/ipykernel_7812/1713806393.py", line 18, in objective + model_history = model.fit( + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler + return fn(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1409, in fit + tmp_logs = self.train_function(iterator) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1051, in train_function + return step_function(self, iterator) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1040, in step_function + outputs = model.distribute_strategy.run(run_step, args=(data,)) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1030, in run_step + outputs = model.train_step(data) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 889, in train_step + y_pred = self(x, training=True) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler + return fn(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 490, in __call__ + return super().__call__(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler + return fn(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py", line 1014, in __call__ + outputs = call_fn(inputs, *args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler + return fn(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py", line 458, in call + return self._run_internal_graph( + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py", line 596, in _run_internal_graph + outputs = node.layer(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler + return fn(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py", line 1014, in __call__ + outputs = call_fn(inputs, *args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler + return fn(*args, **kwargs) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py", line 250, in call + outputs = self.convolution_op(inputs, self.kernel) + File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py", line 225, in convolution_op + return tf.nn.convolution( +Node: 'miRBind_CNN/conv2d/Conv2D' +2 root error(s) found. + (0) UNIMPLEMENTED: DNN library is not found. + [[{{node miRBind_CNN/conv2d/Conv2D}}]] + [[assert_greater_equal/Assert/AssertGuard/pivot_f/_3/_41]] + (1) UNIMPLEMENTED: DNN library is not found. + [[{{node miRBind_CNN/conv2d/Conv2D}}]] +0 successful operations. +0 derived errors ignored. [Op:__inference_train_function_4880] +2024-12-19 15:16:20,730 - Trial 0 failed with value None. +2024-12-19 15:16:20,937 - There are no complete trials. diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py new file mode 100644 index 0000000..c740418 --- /dev/null +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py @@ -0,0 +1,187 @@ +#!/usr/bin/env python3 + +import argparse +import numpy as np +import logging +from tensorflow import keras as K +from tensorflow.keras.optimizers import Adam +from tensorflow.keras.utils import Sequence +import optuna +import optuna.visualization as vis +from optuna.integration import TFKerasPruningCallback +import sys +sys.path.append("../../../code/machine_learning/train/CNN_miRBind_2022/") +from miRBind_CNN_architecture import miRBind_CNN + +class DataGenerator(Sequence): + def __init__(self, data_path, labels_path, dataset_size, batch_size=32, validation_split=0.1, is_validation=False, shuffle=True): + self.size = dataset_size + self.data = np.memmap(data_path, dtype='float32', mode='r', shape=(self.size, 50, 20, 1)) + self.labels = np.memmap(labels_path, dtype='float32', mode='r', shape=(self.size,)) + self.batch_size = batch_size + self.shuffle = shuffle + self.validation_split = validation_split + self.num_samples = len(self.data) + self.num_validation_samples = int(self.num_samples * validation_split) + self.num_train_samples = self.num_samples - self.num_validation_samples + + indices = np.arange(self.num_samples) + if shuffle: + np.random.shuffle(indices) + + if is_validation: + self.indices = indices[self.num_train_samples:] + else: + self.indices = indices[:self.num_train_samples] + + self.on_epoch_end() + + def __len__(self): + return int(np.ceil(len(self.indices) / float(self.batch_size))) + + def __getitem__(self, idx): + batch_indices = self.indices[idx * self.batch_size:(idx + 1) * self.batch_size] + batch_data = self.data[batch_indices] + batch_labels = self.labels[batch_indices] + return batch_data, batch_labels + + def on_epoch_end(self): + if self.shuffle: + np.random.shuffle(self.indices) + +def compile_model(model, lr): + opt = Adam( + learning_rate=lr, + beta_1=0.9, + beta_2=0.999, + epsilon=1e-07, + amsgrad=False, + name="Adam") + + model.compile( + optimizer=opt, + loss='binary_crossentropy', + metrics=['accuracy', K.metrics.AUC(curve='PR')] + ) + return model + +def objective(trial, train_data_gen, val_data_gen, dataset_ratio): + global best_model, best_val_auc + + K.backend.clear_session() + + cnn_num = trial.suggest_int('cnn_layers_num', 2, 10) + kernel_size = trial.suggest_int('kernel_size', 3, 10) + pool_size = trial.suggest_int('pool_size', 1, 8) + dense_num = trial.suggest_int('dense_layers_num', 2, cnn_num) + model = miRBind_CNN(cnn_num=cnn_num, kernel_size=kernel_size, pool_size=pool_size, dense_num=dense_num).model + lr = trial.suggest_float('learning_rate', 0.00001, 0.1) + model = compile_model(model, lr=lr) + + model_history = model.fit( + train_data_gen, + validation_data=val_data_gen, + epochs=50, + class_weight={0: 1, 1: dataset_ratio}, + callbacks=[ + TFKerasPruningCallback(trial, "val_auc"), + K.callbacks.EarlyStopping(patience=5, restore_best_weights=True) + ], + ) + + num_epochs_trained = np.argmax(model_history.history['val_auc']) + val_auc = model_history.history['val_auc'][num_epochs_trained] + + if val_auc > best_val_auc: + best_val_auc = val_auc + best_model = model + model.save('best_model.keras') + logger.info(f"New best model found and saved with Validation AUC: {val_auc}") + + print(f"Validation AU PRC: {val_auc}") + return val_auc + +def setup_logger(log_file): + logger = logging.getLogger('optuna') + logger.setLevel(logging.INFO) + file_handler = logging.FileHandler(log_file, 'w') + file_handler.setFormatter(logging.Formatter('%(asctime)s - %(message)s')) + logger.addHandler(file_handler) + return logger + +def main(): + parser = argparse.ArgumentParser(description='Hyperparameter optimization for miRBind CNN model') + parser.add_argument('--dataset', type=str, + default='../../miRBind_CNN_retraining_orig_parameters/encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_dataset.npy', + help='Path to the training dataset') + parser.add_argument('--labels', type=str, + default='../../miRBind_CNN_retraining_orig_parameters/encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_labels.npy', + help='Path to the training labels') + parser.add_argument('--dataset-size', type=int, default=2516195, + help='Size of the dataset') + parser.add_argument('--dataset-ratio', type=float, default=1, + help='Dataset ratio for class weighting') + parser.add_argument('--batch-size', type=int, default=32, + help='Batch size for training') + parser.add_argument('--validation-split', type=float, default=0.1, + help='Validation split ratio') + parser.add_argument('--n-trials', type=int, default=20, + help='Number of optimization trials') + parser.add_argument('--log-file', type=str, default='hyperparam_optimization.log', + help='Path to the log file') + parser.add_argument('--save-plots', action='store_true', + help='Save optimization plots') + args = parser.parse_args() + + global logger, best_model, best_val_auc + logger = setup_logger(args.log_file) + best_model = None + best_val_auc = 0 + + # Initialize data generators + train_data_gen = DataGenerator( + args.dataset, + args.labels, + dataset_size=args.dataset_size, + batch_size=args.batch_size, + validation_split=args.validation_split, + is_validation=False + ) + + val_data_gen = DataGenerator( + args.dataset, + args.labels, + dataset_size=args.dataset_size, + batch_size=args.batch_size, + validation_split=args.validation_split, + is_validation=True + ) + + # Create and run the study + study = optuna.create_study(direction='maximize', study_name='miRBind_CNN') + study.optimize(lambda trial: objective(trial, train_data_gen, val_data_gen, args.dataset_ratio), + n_trials=args.n_trials) + + # Log results + logger.info("\n") + logger.info(f"Best hyperparameters: {study.best_params}") + logger.info(f"Best value (validation AU PRC): {study.best_value}") + + # Save plots if requested + if args.save_plots: + plots = { + 'optimization_history': vis.plot_optimization_history, + 'contour': vis.plot_contour, + 'param_importances': vis.plot_param_importances, + 'slice': vis.plot_slice + } + + for name, plot_func in plots.items(): + try: + fig = plot_func(study) + fig.write_image(f"{name}.png") + except Exception as e: + logger.error(f"Failed to save {name} plot: {str(e)}") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/analysis/miRBind_CNN_retraining_orig_parameters/run_data_encoding.sh b/analysis/miRBind_CNN_retraining_orig_parameters/run_data_encoding.sh new file mode 100755 index 0000000..9e524c8 --- /dev/null +++ b/analysis/miRBind_CNN_retraining_orig_parameters/run_data_encoding.sh @@ -0,0 +1,23 @@ +#!/bin/bash + + +# train_file = "manakov/AGO2_eCLIP_Manakov2022_train.tsv" +# test_file_1 = "manakov/AGO2_eCLIP_Manakov2022_test.tsv" +# test_file_2 = "manakov/AGO2_eCLIP_Manakov2022_leftout.tsv" +DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test.tsv" +# DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train.tsv" +# DATASET="../../data/chimeric_datasets/Manakov2022/AGO2_eCLIP_Manakov2022_1_train_dataset.tsv" +# DATASET="../../data/chimeric_datasets/Manakov2022/AGO2_eCLIP_Manakov2022_1_test_dataset.tsv" + + +DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_test" +# DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train" +# DATASET_OUT="encoded_dataset/AGO2_eCLIP_Manakov2022_1_test" + + +CODE="../../code/machine_learning" + +mkdir -p encoded_dataset/Manakov2022_flat + +# encode dataset +python $CODE/encode/binding_2D_matrix_encoder.py --i_file $DATASET --o_prefix $DATASET_OUT diff --git a/analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh b/analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh old mode 100644 new mode 100755 index 3afd191..8dd5d3b --- a/analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh +++ b/analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh @@ -1,18 +1,32 @@ +# #!/bin/bash + +# DATASET="../../data/chimeric_datasets/Manakov2022/AGO2_eCLIP_Manakov2022_1_train_dataset.tsv" +# MODEL="../../models/miRBind_CNN_retrained_Manakov_1_orig_parameters.keras" +# CODE="../../code/machine_learning" + +# mkdir -p encoded_dataset + +# # encode dataset +# python $CODE/encode/binding_2D_matrix_encoder.py --i_file $DATASET --o_prefix encoded_dataset/AGO2_eCLIP_Manakov2022_1_train + +# # train model +# python $CODE/train/CNN_miRBind_2022/miRBind_CNN_training_orig_parameters.py \ +# --data encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_dataset.npy \ +# --labels encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_labels.npy \ +# --dataset_size 2524246 \ +# --ratio 1 \ +# --model $MODEL + + #!/bin/bash -DATASET="../../data/chimeric_datasets/Manakov2022/AGO_eCLIP_Manakov2022_1_train_dataset.tsv" MODEL="../../models/miRBind_CNN_retrained_Manakov_1_orig_parameters.keras" CODE="../../code/machine_learning" -mkdir -p encoded_dataset - -# encode dataset -python $CODE/encode/binding_2D_matrix_encoder.py --i_file $DATASET --o_prefix encoded_dataset/AGO2_eCLIP_Manakov2022_1_train - # train model python $CODE/train/CNN_miRBind_2022/miRBind_CNN_training_orig_parameters.py \ ---data encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_dataset.npy \ ---labels encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_labels.npy \ ---dataset_size 2524246 \ +--data encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_dataset.npy \ +--labels encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_labels.npy \ +--dataset_size 2516195 \ --ratio 1 \ --model $MODEL \ No newline at end of file diff --git a/code/machine_learning/train/CNN_miRBind_2022/__pycache__/miRBind_CNN_architecture.cpython-38.pyc b/code/machine_learning/train/CNN_miRBind_2022/__pycache__/miRBind_CNN_architecture.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..30e85dbd1e5d04b1e759e69c633442779f34f415 GIT binary patch literal 2215 zcma)7O^6&t6z>0?>6zKAW-*b7_CX{~bY^$vWRPr3mZ+F45)Fn1X)0Z{Grj4bNmWhE zEIp?UdRh-2MMRIDJbClxRlRr+>*^sa2!aSgQ1HE)nau`0STp_lRn@EaUcL9e*Ec$y zz<}|@;O6jC-!Oi`$?;*s+Pp`A9H`J4th)hS@6)#Ah?Gc$(rD|Uz_ui_iJS&GF z>~HaET)}DoN=~F?rE<4eDOifujt>_m7vT}l17Z4(u|>_;rfup`|GpVJv_qRT0M4Z? z@OVYFitPHOtfa~1?d`p9gAm50NtV;Fs3^-r!qZVESt=)-g|~=cG_1-{j#vot3z;!z zf|U}svNBw#3&&*4czCwI-tR}@NXoG|-|NvT>sEZ&+gR@o1~0GohOA^_W3bWh5BdXb z`yJ3s*$$cH5>#+fFv++8H&`fO5)z?}S8y(zpf_L=z0u~hhC>f7e~6(tw17Mq=<(xb zitEeK0R}3?xD1c@1BisGpcb_cteIDPGhdpuxo>V6@5Qr*w5U@zsC&v-Gis~0b}Wo0 zZ5%kYw{OloY3~~g&N<@T+OBOa**vfoyI$?k;J}>E?wpqXk+fO6+S-L#6Kt7v^EB9+ z1+q~G`wRL1wDP#d4a^Pf^W3p5W ziXS!-k?k?%j|rtv$Ern%B*_lexdi3SG1Rvf;m?3lgbF7Tu~(oxGKmgin){R{|R%lnn%nr0Il{bPCnm zTxN?aCkGkjLwn;{Dk6`+fExT;B)!c_3gAKy62XLk?}08F?VIw`su*WEiyR)|2`lG% zSJ|LjSJj-ngqxj(aH4Dc94v_th=EzMAWM&a550z?XU^ZAyZ7|*+&rdZJ90VpPQHTV z1d`Py7n!^VJcJJl1YwyZDoD`wBuBoTBzGnxU+nlvLaQ`Mco)xaB0)?XBX|XkdqlB7 zJU2|o37(txf>jm3mrzt3`qlf&k~ Date: Wed, 1 Jan 2025 22:06:21 +0100 Subject: [PATCH 06/20] Add .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7043de7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.ipynb_checkpoints +.keras +*.keras From cf62cce85458b4c1cf55499ae52cfba822af4e6a Mon Sep 17 00:00:00 2001 From: davidcechak Date: Thu, 20 Feb 2025 15:49:31 +0100 Subject: [PATCH 07/20] Encode data for mirbind memory mapping dataset --- .../run_data_encoding.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100755 analysis/miRBind_CNN_retraining_optimized/run_data_encoding.sh diff --git a/analysis/miRBind_CNN_retraining_optimized/run_data_encoding.sh b/analysis/miRBind_CNN_retraining_optimized/run_data_encoding.sh new file mode 100755 index 0000000..cb4d784 --- /dev/null +++ b/analysis/miRBind_CNN_retraining_optimized/run_data_encoding.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_leftout.tsv" +# DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test.tsv" +# DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train.tsv" + + +DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_leftout.tsv" +# DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test" +# DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train" + + +CODE="../../code/machine_learning" + +mkdir -p encoded_dataset/Manakov2022_flat + +# encode dataset +python $CODE/encode/binding_2D_matrix_encoder.py --i_file $DATASET --o_prefix $DATASET_OUT From 3a018adfe36161403ff9709d1bf67ad74caefb83 Mon Sep 17 00:00:00 2001 From: davidcechak Date: Thu, 20 Feb 2025 15:55:07 +0100 Subject: [PATCH 08/20] Use optuna to optimise hyperparamters of miRBind model --- .../Hyperparam_optimization.ipynb | 961 ------------------ .../data_generators.py | 86 ++ ...ram_optimization-tmp_made_from_notebook.py | 202 ---- .../hyperparam_optimization.py | 124 ++- .../hyperparam_optimization.sh | 24 + 5 files changed, 169 insertions(+), 1228 deletions(-) delete mode 100644 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/Hyperparam_optimization.ipynb create mode 100644 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/data_generators.py delete mode 100644 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization-tmp_made_from_notebook.py create mode 100755 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.sh diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/Hyperparam_optimization.ipynb b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/Hyperparam_optimization.ipynb deleted file mode 100644 index 85483ca..0000000 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/Hyperparam_optimization.ipynb +++ /dev/null @@ -1,961 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "a56bd246-c7dd-4bec-94c9-99c1d8844f50", - "metadata": {}, - "source": [ - "# Hyperparam optimization notebook\n", - "\n", - "In this notebook, we will try to optimize the hyperparameters of the miRBind CNN model. Quick guide to what is a [parameter vs. hyperparameter](https://machinelearningmastery.com/difference-between-a-parameter-and-a-hyperparameter/).\n", - "\n", - "We will use [Optuna](https://optuna.org/) framework for this. It will try for us a bunch of different hyperparameter settings and see what combination works the best. \n", - "\n", - "Let's try to optimize number of blocks with convolution layer, kernel size of the convolution, size of the pooling layer, number of blocks with the dense layer and learning rate - these are our hyperparameters.\n", - "\n", - "Our metrics to optimize will be the AU PRC on the validation set (we split the train set into actual training set and validation set)." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "038a2787-7e91-473b-bc4b-338200529612", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", - " from .autonotebook import tqdm as notebook_tqdm\n" - ] - } - ], - "source": [ - "import numpy as np\n", - "from tensorflow import keras as K\n", - "from tensorflow.keras.optimizers import Adam\n", - "from tensorflow.keras.utils import Sequence\n", - "\n", - "import plotly\n", - "import logging\n", - "import optuna\n", - "import optuna.visualization as vis\n", - "from optuna.integration import TFKerasPruningCallback" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "9f138a3f-a28f-4ef7-b4cb-37d8034e7320", - "metadata": {}, - "outputs": [], - "source": [ - "import sys\n", - "sys.path.append(\"../../../code/machine_learning/train/CNN_miRBind_2022/\")\n", - "\n", - "from miRBind_CNN_architecture import miRBind_CNN" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "e1cb92e2-78e0-45f1-b5c6-e4cabf6a4411", - "metadata": {}, - "outputs": [], - "source": [ - "# it's here for to be able to display plots in jupyter notebook\n", - "plotly.io.renderers.default = 'iframe'" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "be1adc2d-d4b5-470f-922d-86e5fc317e17", - "metadata": { - "id": "oZ591qC0Femi" - }, - "outputs": [], - "source": [ - "def compile_model(model, lr):\n", - " \n", - " opt = Adam(\n", - " learning_rate=lr,\n", - " beta_1=0.9,\n", - " beta_2=0.999,\n", - " epsilon=1e-07,\n", - " amsgrad=False,\n", - " name=\"Adam\")\n", - "\n", - " model.compile(\n", - " optimizer=opt,\n", - " loss='binary_crossentropy',\n", - " metrics=['accuracy', K.metrics.AUC(curve='PR')] # adding the metrics on which we want to optimize\n", - " )\n", - " return model" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "cb6f5a12-01ef-4b38-86d3-7f3d8ff49c39", - "metadata": {}, - "outputs": [], - "source": [ - "class DataGenerator(Sequence):\n", - " def __init__(self, data_path, labels_path, dataset_size, batch_size=32, validation_split=0.1, is_validation=False, shuffle=True): \n", - " # preload the encoded numpy data\n", - " # the size needed to properly load the array\n", - " self.size = dataset_size\n", - " \n", - " self.data = np.memmap(data_path, dtype='float32', mode='r', shape=(self.size, 50, 20, 1))\n", - " self.labels = np.memmap(labels_path, dtype='float32', mode='r', shape=(self.size,))\n", - " self.batch_size = batch_size\n", - " self.shuffle = shuffle\n", - " \n", - " # Determine number of train and validation samples\n", - " self.validation_split = validation_split\n", - " self.num_samples = len(self.data)\n", - " self.num_validation_samples = int(self.num_samples * validation_split)\n", - " self.num_train_samples = self.num_samples - self.num_validation_samples\n", - " \n", - " # Determine indices for validation and training\n", - " indices = np.arange(self.num_samples)\n", - " if shuffle:\n", - " np.random.shuffle(indices)\n", - " \n", - " if is_validation:\n", - " self.indices = indices[self.num_train_samples:]\n", - " else:\n", - " self.indices = indices[:self.num_train_samples]\n", - " \n", - " # Shuffle the data initially\n", - " self.on_epoch_end()\n", - "\n", - " def __len__(self):\n", - " # Denotes the number of batches per epoch\n", - " return int(np.ceil(len(self.indices) / float(self.batch_size)))\n", - "\n", - " def __getitem__(self, idx):\n", - " # Generate one batch of data\n", - " batch_indices = self.indices[idx * self.batch_size:(idx + 1) * self.batch_size]\n", - " batch_data = self.data[batch_indices]\n", - " batch_labels = self.labels[batch_indices]\n", - " return batch_data, batch_labels\n", - "\n", - " def on_epoch_end(self):\n", - " # Updates indices after each epoch for shuffling\n", - " if self.shuffle:\n", - " np.random.shuffle(self.indices)" - ] - }, - { - "cell_type": "markdown", - "id": "58a51c3e-0bf2-42e8-8eda-15cc4b92a251", - "metadata": {}, - "source": [ - "------------------------------\n", - "Choose a dataset on which you want to train. It has to be already encoded with the ```binding_2D_matrix_encoder.py```" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "id": "e95fa7f9-103c-419e-a976-804af5bef9c8", - "metadata": {}, - "outputs": [], - "source": [ - "# DATASET = \"../../../AmiRBench/code/dataset_vOct/Manakov_1_train_dataset.npy\"\n", - "# DATASET = '../miRBind_CNN_retraining_orig_parameters/encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_dataset.npy'\n", - "DATASET = '../../miRBind_CNN_retraining_orig_parameters/encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_dataset.npy'\n", - "\n", - "# LABELS = \"../../../AmiRBench/code/dataset_vOct/Manakov_1_train_labels.npy\"\n", - "# LABELS = '../miRBind_CNN_retraining_orig_parameters/encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_labels.npy'\n", - "LABELS = '../../miRBind_CNN_retraining_orig_parameters/encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_labels.npy'\n", - "\n", - "DATASET_RATIO = 1\n", - "# DATASET_SIZE = 2524246\n", - "DATASET_SIZE = 2516195\n" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "id": "bfe763bc-4015-4a24-9b92-1c6dde016549", - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
genenoncodingRNAnoncodingRNA_namenoncodingRNA_famfeaturelabelchrstartendstrandgene_cluster_ID
0CTACCTGATCCGTTTACTCACTATGCCCCCTTGCCATCCTGGCCTT...CTGTACAGCCTCCTAGCTTTCChsa-let-7a-2-3plet-7exon110102141236.0102141285+22755
1CAGCCCATGCCATTGTTTCGGTGAACGGTACTACGATTGAAGGACA...CTATACAATCTACTGTCTTTChsa-let-7a-3plet-7exon110119577134.0119577183-129700
2CCTCAGCATTAAATGCTTTAGCAAATGACACATTAGACCTACCTCA...CTATACAATCTACTGTCTTTChsa-let-7a-3plet-7exon11330659280.030659329+10171
3GAAACCACGTATTTGGAGCCAGGAAAGATCAGTGTGAATTGTGGAC...CTATACAATCTACTGTCTTTChsa-let-7a-3plet-7exon11548738406.048738455-283322
4TGCAGTTTTCCCCTTGATTGGCGTGTGTGTATATATGGATAAATAT...CTATACAATCTACTGTCTTTChsa-let-7a-3plet-7exon12118108420.0118108469+43376
....................................
2516190AACAGCAAAACCAATTAAGAAACAATAATTAGGGCCAGGTGCCCTA...CACCCGTAGAACCGACCTTGCGhsa-miR-99b-5pmir-10intron0933276894.033276943+230577
2516191GTTGAATGCAGATGTGCTGAGTTAGAGGTGGGATTTGGAAAAGGGC...CACCCGTAGAACCGACCTTGCGhsa-miR-99b-5pmir-10three_prime_utr0642080276.042080325+497680
2516192ATTAAGGTAGCTTTGGTTTGGAAAACATACTCAGTATACAGAAACA...CACCCGTAGAACCGACCTTGCGhsa-miR-99b-5pmir-10intron0X155920076.0155920125+435974
2516193TTTGAGAAGTAGGAGAGCAGGGTGGTACCGTGTGGGCTCTTACCCT...CACCCGTAGAACCGACCTTGCGhsa-miR-99b-5pmir-10three_prime_utr02101270216.0101270265+331079
2516194CTGGCTTTAGCCAGCCTGGCCAGCCTCACTCTTGCCCTTGGTCATC...CACCCGTAGAACCGACCTTGCGhsa-miR-99b-5pmir-10NaN03156530482.0156530531-547491
\n", - "

2516195 rows × 11 columns

\n", - "
" - ], - "text/plain": [ - " gene \\\n", - "0 CTACCTGATCCGTTTACTCACTATGCCCCCTTGCCATCCTGGCCTT... \n", - "1 CAGCCCATGCCATTGTTTCGGTGAACGGTACTACGATTGAAGGACA... \n", - "2 CCTCAGCATTAAATGCTTTAGCAAATGACACATTAGACCTACCTCA... \n", - "3 GAAACCACGTATTTGGAGCCAGGAAAGATCAGTGTGAATTGTGGAC... \n", - "4 TGCAGTTTTCCCCTTGATTGGCGTGTGTGTATATATGGATAAATAT... \n", - "... ... \n", - "2516190 AACAGCAAAACCAATTAAGAAACAATAATTAGGGCCAGGTGCCCTA... \n", - "2516191 GTTGAATGCAGATGTGCTGAGTTAGAGGTGGGATTTGGAAAAGGGC... \n", - "2516192 ATTAAGGTAGCTTTGGTTTGGAAAACATACTCAGTATACAGAAACA... \n", - "2516193 TTTGAGAAGTAGGAGAGCAGGGTGGTACCGTGTGGGCTCTTACCCT... \n", - "2516194 CTGGCTTTAGCCAGCCTGGCCAGCCTCACTCTTGCCCTTGGTCATC... \n", - "\n", - " noncodingRNA noncodingRNA_name noncodingRNA_fam \\\n", - "0 CTGTACAGCCTCCTAGCTTTCC hsa-let-7a-2-3p let-7 \n", - "1 CTATACAATCTACTGTCTTTC hsa-let-7a-3p let-7 \n", - "2 CTATACAATCTACTGTCTTTC hsa-let-7a-3p let-7 \n", - "3 CTATACAATCTACTGTCTTTC hsa-let-7a-3p let-7 \n", - "4 CTATACAATCTACTGTCTTTC hsa-let-7a-3p let-7 \n", - "... ... ... ... \n", - "2516190 CACCCGTAGAACCGACCTTGCG hsa-miR-99b-5p mir-10 \n", - "2516191 CACCCGTAGAACCGACCTTGCG hsa-miR-99b-5p mir-10 \n", - "2516192 CACCCGTAGAACCGACCTTGCG hsa-miR-99b-5p mir-10 \n", - "2516193 CACCCGTAGAACCGACCTTGCG hsa-miR-99b-5p mir-10 \n", - "2516194 CACCCGTAGAACCGACCTTGCG hsa-miR-99b-5p mir-10 \n", - "\n", - " feature label chr start end strand \\\n", - "0 exon 1 10 102141236.0 102141285 + \n", - "1 exon 1 10 119577134.0 119577183 - \n", - "2 exon 1 13 30659280.0 30659329 + \n", - "3 exon 1 15 48738406.0 48738455 - \n", - "4 exon 1 2 118108420.0 118108469 + \n", - "... ... ... .. ... ... ... \n", - "2516190 intron 0 9 33276894.0 33276943 + \n", - "2516191 three_prime_utr 0 6 42080276.0 42080325 + \n", - "2516192 intron 0 X 155920076.0 155920125 + \n", - "2516193 three_prime_utr 0 2 101270216.0 101270265 + \n", - "2516194 NaN 0 3 156530482.0 156530531 - \n", - "\n", - " gene_cluster_ID \n", - "0 22755 \n", - "1 129700 \n", - "2 10171 \n", - "3 283322 \n", - "4 43376 \n", - "... ... \n", - "2516190 230577 \n", - "2516191 497680 \n", - "2516192 435974 \n", - "2516193 331079 \n", - "2516194 547491 \n", - "\n", - "[2516195 rows x 11 columns]" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import pandas as pd \n", - "pd.read_csv(\"../../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train.tsv\", sep='\\t')" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "id": "448d3e9a-0ee5-483c-be44-e704d957434f", - "metadata": {}, - "outputs": [], - "source": [ - "train_data_gen = DataGenerator(DATASET, LABELS, dataset_size=DATASET_SIZE, validation_split=0.1, is_validation=False)\n", - "\n", - "val_data_gen = DataGenerator(DATASET, LABELS, dataset_size=DATASET_SIZE, validation_split=0.1, is_validation=True)" - ] - }, - { - "cell_type": "markdown", - "id": "9f4fdeb8-ceb6-4d06-81ff-fe3979a5fe69", - "metadata": {}, - "source": [ - "----------------------------\n", - "This is the function that creates a model with suggested hyperparameters, trains it and sees how well it performs on the validation set\n", - "\n", - "**Some explanations**\n", - "\n", - "`trial` is the object that \"carries the information\" about the hyperparameter optimization. `trial.suggest_` means \"give me some value for the hyperparameter\" that might work well for the model.\n", - "\n", - "`TFKerasPruningCallback` is another hack, where you can stop unpromising training in the middle and scratch it. E.g. when you are training the model with some hyperparameters and after few epochs you see the model doesn't learn anything, you can simply stop the training, remember that this set of hyperparameters didn't work well and you don't have to waste time with worthless training finishing.\n", - "\n", - "`K.callbacks.EarlyStopping` - early stopping method helps to train for the right amount of epochs. It monitors the performance on the validation set and when the model starts overfitting and performing worse, it stops the training." - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "id": "a90ef512-5b42-489c-a9ba-584c9877b662", - "metadata": {}, - "outputs": [], - "source": [ - "best_model = None\n", - "best_val_auc = 0\n", - "\n", - "def objective(trial):\n", - " global best_model, best_val_auc\n", - " \n", - " K.backend.clear_session()\n", - "\n", - " # build the model based on suggested hyperparameters\n", - " cnn_num = trial.suggest_int('cnn_layers_num', 2, 10)\n", - " kernel_size = trial.suggest_int('kernel_size', 3, 10)\n", - " pool_size = trial.suggest_int('pool_size', 1, 8)\n", - " dense_num = trial.suggest_int('dense_layers_num', 2, cnn_num)\n", - " model = miRBind_CNN(cnn_num=cnn_num, kernel_size=kernel_size, pool_size=pool_size, dense_num=dense_num).model\n", - " lr = trial.suggest_float('learning_rate', 0.00001, 0.1) \n", - " model = compile_model(model, lr=lr)\n", - " \n", - " model_history = model.fit(\n", - " train_data_gen,\n", - " validation_data=val_data_gen,\n", - " epochs=50,\n", - " class_weight={0: 1, 1: DATASET_RATIO},\n", - " callbacks=[TFKerasPruningCallback(trial, \"val_auc\"), # get rid of attempts with unpromising hyperparam combination\n", - " K.callbacks.EarlyStopping(patience=5, restore_best_weights=True)],\n", - " )\n", - " \n", - " num_epochs_trained = np.argmax(model_history.history['val_auc'])\n", - " val_auc = model_history.history['val_auc'][num_epochs_trained]\n", - "\n", - " # check performance of this trial\n", - " if val_auc > best_val_auc:\n", - " best_val_auc = val_auc\n", - " best_model = model # save the current best model\n", - " model.save('best_model.keras') # save the model to disk\n", - " logger.info(f\"New best model found and saved with Validation AUC: {val_auc}\")\n", - " \n", - " print(f\"Validation AU PRC: {val_auc}\")\n", - " \n", - " return val_auc" - ] - }, - { - "cell_type": "markdown", - "id": "f6a868f2-2695-467d-b217-353fd0b2fe0f", - "metadata": {}, - "source": [ - "Set up a logger for logging the optimization process to a file" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "id": "4be25843-e179-44d9-9c36-4e500fdd62bf", - "metadata": {}, - "outputs": [], - "source": [ - "logger = logging.getLogger('optuna')\n", - "logger.setLevel(logging.INFO)\n", - "file_handler = logging.FileHandler('hyperparam_optimization.log', 'w')\n", - "file_handler.setFormatter(logging.Formatter('%(asctime)s - %(message)s'))\n", - "logger.addHandler(file_handler)" - ] - }, - { - "cell_type": "markdown", - "id": "5afc729b-d257-4126-abb7-ce47503bd675", - "metadata": {}, - "source": [ - "This is the place where we start running the optimization process" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "id": "7c4c6d6d-fa85-47c3-9df9-a67fc9cbe8d2", - "metadata": { - "id": "vIEbdJxqydNm" - }, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "[I 2024-12-19 15:16:16,997] A new study created in memory with name: miRBind_CNN\n", - "2024-12-19 15:16:17.160889: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA\n", - "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", - "2024-12-19 15:16:17.764088: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1532] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 600 MB memory: -> device: 0, name: NVIDIA A40, pci bus id: 0000:27:00.0, compute capability: 8.6\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Epoch 1/50\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2024-12-19 15:16:20.660212: E tensorflow/stream_executor/cuda/cuda_dnn.cc:389] Could not create cudnn handle: CUDNN_STATUS_NOT_INITIALIZED\n", - "2024-12-19 15:16:20.660358: E tensorflow/stream_executor/cuda/cuda_dnn.cc:394] Error retrieving driver version: NOT_FOUND: could not find kernel module information in driver version file contents: \"NVRM version: NVIDIA UNIX Open Kernel Module for x86_64 565.57.01 Release Build (dvs-builder@U16-A24-9-2) Thu Oct 10 12:15:00 UTC 2024\n", - "GCC version: gcc version 12.3.0 (Ubuntu 12.3.0-1ubuntu1~22.04) \n", - "\"\n", - "2024-12-19 15:16:20.660409: W tensorflow/core/framework/op_kernel.cc:1745] OP_REQUIRES failed at conv_ops.cc:1120 : UNIMPLEMENTED: DNN library is not found.\n", - "[W 2024-12-19 15:16:20,727] Trial 0 failed with parameters: {'cnn_layers_num': 7, 'kernel_size': 6, 'pool_size': 5, 'dense_layers_num': 5, 'learning_rate': 0.05887436900846115} because of the following error: UnimplementedError().\n", - "Traceback (most recent call last):\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 197, in _run_trial\n", - " value_or_values = func(trial)\n", - " File \"/tmp/ipykernel_7812/1713806393.py\", line 18, in objective\n", - " model_history = model.fit(\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 67, in error_handler\n", - " raise e.with_traceback(filtered_tb) from None\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tensorflow/python/eager/execute.py\", line 54, in quick_execute\n", - " tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,\n", - "tensorflow.python.framework.errors_impl.UnimplementedError: Graph execution error:\n", - "\n", - "Detected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last):\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 194, in _run_module_as_main\n", - " return _run_code(code, main_globals, None,\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 87, in _run_code\n", - " exec(code, run_globals)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py\", line 17, in \n", - " app.launch_new_instance()\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py\", line 1043, in launch_instance\n", - " app.start()\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py\", line 725, in start\n", - " self.io_loop.start()\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py\", line 215, in start\n", - " self.asyncio_loop.run_forever()\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 570, in run_forever\n", - " self._run_once()\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 1859, in _run_once\n", - " handle._run()\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py\", line 81, in _run\n", - " self._context.run(self._callback, *self._args)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 513, in dispatch_queue\n", - " await self.process_one()\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 502, in process_one\n", - " await dispatch(*args)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 409, in dispatch_shell\n", - " await result\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 729, in execute_request\n", - " reply_content = await reply_content\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py\", line 422, in do_execute\n", - " res = shell.run_cell(\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py\", line 540, in run_cell\n", - " return super().run_cell(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 2961, in run_cell\n", - " result = self._run_cell(\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3016, in _run_cell\n", - " result = runner(coro)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py\", line 129, in _pseudo_sync_runner\n", - " coro.send(None)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3221, in run_cell_async\n", - " has_raised = await self.run_ast_nodes(code_ast.body, cell_name,\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3400, in run_ast_nodes\n", - " if await self.run_code(code, result, async_=asy):\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3460, in run_code\n", - " exec(code_obj, self.user_global_ns, self.user_ns)\n", - " File \"/tmp/ipykernel_7812/2048605125.py\", line 2, in \n", - " study.optimize(objective, n_trials=20)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py\", line 475, in optimize\n", - " _optimize(\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 63, in _optimize\n", - " _optimize_sequential(\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 160, in _optimize_sequential\n", - " frozen_trial = _run_trial(study, func, catch)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 197, in _run_trial\n", - " value_or_values = func(trial)\n", - " File \"/tmp/ipykernel_7812/1713806393.py\", line 18, in objective\n", - " model_history = model.fit(\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", - " return fn(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1409, in fit\n", - " tmp_logs = self.train_function(iterator)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1051, in train_function\n", - " return step_function(self, iterator)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1040, in step_function\n", - " outputs = model.distribute_strategy.run(run_step, args=(data,))\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1030, in run_step\n", - " outputs = model.train_step(data)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 889, in train_step\n", - " y_pred = self(x, training=True)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", - " return fn(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 490, in __call__\n", - " return super().__call__(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", - " return fn(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n", - " outputs = call_fn(inputs, *args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n", - " return fn(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 458, in call\n", - " return self._run_internal_graph(\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 596, in _run_internal_graph\n", - " outputs = node.layer(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", - " return fn(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n", - " outputs = call_fn(inputs, *args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n", - " return fn(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 250, in call\n", - " outputs = self.convolution_op(inputs, self.kernel)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 225, in convolution_op\n", - " return tf.nn.convolution(\n", - "Node: 'miRBind_CNN/conv2d/Conv2D'\n", - "Detected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last):\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 194, in _run_module_as_main\n", - " return _run_code(code, main_globals, None,\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 87, in _run_code\n", - " exec(code, run_globals)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py\", line 17, in \n", - " app.launch_new_instance()\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py\", line 1043, in launch_instance\n", - " app.start()\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py\", line 725, in start\n", - " self.io_loop.start()\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py\", line 215, in start\n", - " self.asyncio_loop.run_forever()\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 570, in run_forever\n", - " self._run_once()\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 1859, in _run_once\n", - " handle._run()\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py\", line 81, in _run\n", - " self._context.run(self._callback, *self._args)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 513, in dispatch_queue\n", - " await self.process_one()\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 502, in process_one\n", - " await dispatch(*args)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 409, in dispatch_shell\n", - " await result\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 729, in execute_request\n", - " reply_content = await reply_content\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py\", line 422, in do_execute\n", - " res = shell.run_cell(\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py\", line 540, in run_cell\n", - " return super().run_cell(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 2961, in run_cell\n", - " result = self._run_cell(\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3016, in _run_cell\n", - " result = runner(coro)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py\", line 129, in _pseudo_sync_runner\n", - " coro.send(None)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3221, in run_cell_async\n", - " has_raised = await self.run_ast_nodes(code_ast.body, cell_name,\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3400, in run_ast_nodes\n", - " if await self.run_code(code, result, async_=asy):\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3460, in run_code\n", - " exec(code_obj, self.user_global_ns, self.user_ns)\n", - " File \"/tmp/ipykernel_7812/2048605125.py\", line 2, in \n", - " study.optimize(objective, n_trials=20)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py\", line 475, in optimize\n", - " _optimize(\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 63, in _optimize\n", - " _optimize_sequential(\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 160, in _optimize_sequential\n", - " frozen_trial = _run_trial(study, func, catch)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 197, in _run_trial\n", - " value_or_values = func(trial)\n", - " File \"/tmp/ipykernel_7812/1713806393.py\", line 18, in objective\n", - " model_history = model.fit(\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", - " return fn(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1409, in fit\n", - " tmp_logs = self.train_function(iterator)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1051, in train_function\n", - " return step_function(self, iterator)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1040, in step_function\n", - " outputs = model.distribute_strategy.run(run_step, args=(data,))\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1030, in run_step\n", - " outputs = model.train_step(data)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 889, in train_step\n", - " y_pred = self(x, training=True)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", - " return fn(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 490, in __call__\n", - " return super().__call__(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", - " return fn(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n", - " outputs = call_fn(inputs, *args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n", - " return fn(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 458, in call\n", - " return self._run_internal_graph(\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 596, in _run_internal_graph\n", - " outputs = node.layer(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n", - " return fn(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n", - " outputs = call_fn(inputs, *args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n", - " return fn(*args, **kwargs)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 250, in call\n", - " outputs = self.convolution_op(inputs, self.kernel)\n", - " File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 225, in convolution_op\n", - " return tf.nn.convolution(\n", - "Node: 'miRBind_CNN/conv2d/Conv2D'\n", - "2 root error(s) found.\n", - " (0) UNIMPLEMENTED: DNN library is not found.\n", - "\t [[{{node miRBind_CNN/conv2d/Conv2D}}]]\n", - "\t [[assert_greater_equal/Assert/AssertGuard/pivot_f/_3/_41]]\n", - " (1) UNIMPLEMENTED: DNN library is not found.\n", - "\t [[{{node miRBind_CNN/conv2d/Conv2D}}]]\n", - "0 successful operations.\n", - "0 derived errors ignored. [Op:__inference_train_function_4880]\n", - "[W 2024-12-19 15:16:20,730] Trial 0 failed with value None.\n" - ] - }, - { - "ename": "UnimplementedError", - "evalue": "Graph execution error:\n\nDetected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 194, in _run_module_as_main\n return _run_code(code, main_globals, None,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 87, in _run_code\n exec(code, run_globals)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py\", line 17, in \n app.launch_new_instance()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py\", line 1043, in launch_instance\n app.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py\", line 725, in start\n self.io_loop.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py\", line 215, in start\n self.asyncio_loop.run_forever()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 570, in run_forever\n self._run_once()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 1859, in _run_once\n handle._run()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py\", line 81, in _run\n self._context.run(self._callback, *self._args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 513, in dispatch_queue\n await self.process_one()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 502, in process_one\n await dispatch(*args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 409, in dispatch_shell\n await result\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 729, in execute_request\n reply_content = await reply_content\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py\", line 422, in do_execute\n res = shell.run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py\", line 540, in run_cell\n return super().run_cell(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 2961, in run_cell\n result = self._run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3016, in _run_cell\n result = runner(coro)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py\", line 129, in _pseudo_sync_runner\n coro.send(None)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3221, in run_cell_async\n has_raised = await self.run_ast_nodes(code_ast.body, cell_name,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3400, in run_ast_nodes\n if await self.run_code(code, result, async_=asy):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3460, in run_code\n exec(code_obj, self.user_global_ns, self.user_ns)\n File \"/tmp/ipykernel_7812/2048605125.py\", line 2, in \n study.optimize(objective, n_trials=20)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py\", line 475, in optimize\n _optimize(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 63, in _optimize\n _optimize_sequential(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 160, in _optimize_sequential\n frozen_trial = _run_trial(study, func, catch)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 197, in _run_trial\n value_or_values = func(trial)\n File \"/tmp/ipykernel_7812/1713806393.py\", line 18, in objective\n model_history = model.fit(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1409, in fit\n tmp_logs = self.train_function(iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1051, in train_function\n return step_function(self, iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1040, in step_function\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1030, in run_step\n outputs = model.train_step(data)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 889, in train_step\n y_pred = self(x, training=True)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 490, in __call__\n return super().__call__(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 458, in call\n return self._run_internal_graph(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 596, in _run_internal_graph\n outputs = node.layer(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 250, in call\n outputs = self.convolution_op(inputs, self.kernel)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 225, in convolution_op\n return tf.nn.convolution(\nNode: 'miRBind_CNN/conv2d/Conv2D'\nDetected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 194, in _run_module_as_main\n return _run_code(code, main_globals, None,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 87, in _run_code\n exec(code, run_globals)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py\", line 17, in \n app.launch_new_instance()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py\", line 1043, in launch_instance\n app.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py\", line 725, in start\n self.io_loop.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py\", line 215, in start\n self.asyncio_loop.run_forever()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 570, in run_forever\n self._run_once()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 1859, in _run_once\n handle._run()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py\", line 81, in _run\n self._context.run(self._callback, *self._args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 513, in dispatch_queue\n await self.process_one()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 502, in process_one\n await dispatch(*args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 409, in dispatch_shell\n await result\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 729, in execute_request\n reply_content = await reply_content\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py\", line 422, in do_execute\n res = shell.run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py\", line 540, in run_cell\n return super().run_cell(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 2961, in run_cell\n result = self._run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3016, in _run_cell\n result = runner(coro)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py\", line 129, in _pseudo_sync_runner\n coro.send(None)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3221, in run_cell_async\n has_raised = await self.run_ast_nodes(code_ast.body, cell_name,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3400, in run_ast_nodes\n if await self.run_code(code, result, async_=asy):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3460, in run_code\n exec(code_obj, self.user_global_ns, self.user_ns)\n File \"/tmp/ipykernel_7812/2048605125.py\", line 2, in \n study.optimize(objective, n_trials=20)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py\", line 475, in optimize\n _optimize(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 63, in _optimize\n _optimize_sequential(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 160, in _optimize_sequential\n frozen_trial = _run_trial(study, func, catch)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 197, in _run_trial\n value_or_values = func(trial)\n File \"/tmp/ipykernel_7812/1713806393.py\", line 18, in objective\n model_history = model.fit(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1409, in fit\n tmp_logs = self.train_function(iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1051, in train_function\n return step_function(self, iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1040, in step_function\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1030, in run_step\n outputs = model.train_step(data)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 889, in train_step\n y_pred = self(x, training=True)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 490, in __call__\n return super().__call__(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 458, in call\n return self._run_internal_graph(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 596, in _run_internal_graph\n outputs = node.layer(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 250, in call\n outputs = self.convolution_op(inputs, self.kernel)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 225, in convolution_op\n return tf.nn.convolution(\nNode: 'miRBind_CNN/conv2d/Conv2D'\n2 root error(s) found.\n (0) UNIMPLEMENTED: DNN library is not found.\n\t [[{{node miRBind_CNN/conv2d/Conv2D}}]]\n\t [[assert_greater_equal/Assert/AssertGuard/pivot_f/_3/_41]]\n (1) UNIMPLEMENTED: DNN library is not found.\n\t [[{{node miRBind_CNN/conv2d/Conv2D}}]]\n0 successful operations.\n0 derived errors ignored. [Op:__inference_train_function_4880]", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mUnimplementedError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[24], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m study \u001b[38;5;241m=\u001b[39m optuna\u001b[38;5;241m.\u001b[39mcreate_study(direction\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmaximize\u001b[39m\u001b[38;5;124m'\u001b[39m, study_name\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmiRBind_CNN\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m \u001b[43mstudy\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moptimize\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobjective\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mn_trials\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m20\u001b[39;49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py:475\u001b[0m, in \u001b[0;36mStudy.optimize\u001b[0;34m(self, func, n_trials, timeout, n_jobs, catch, callbacks, gc_after_trial, show_progress_bar)\u001b[0m\n\u001b[1;32m 373\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21moptimize\u001b[39m(\n\u001b[1;32m 374\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 375\u001b[0m func: ObjectiveFuncType,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 382\u001b[0m show_progress_bar: \u001b[38;5;28mbool\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[1;32m 383\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 384\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Optimize an objective function.\u001b[39;00m\n\u001b[1;32m 385\u001b[0m \n\u001b[1;32m 386\u001b[0m \u001b[38;5;124;03m Optimization is done by choosing a suitable set of hyperparameter values from a given\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 473\u001b[0m \u001b[38;5;124;03m If nested invocation of this method occurs.\u001b[39;00m\n\u001b[1;32m 474\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 475\u001b[0m \u001b[43m_optimize\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 476\u001b[0m \u001b[43m \u001b[49m\u001b[43mstudy\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 477\u001b[0m \u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 478\u001b[0m \u001b[43m \u001b[49m\u001b[43mn_trials\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mn_trials\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 479\u001b[0m \u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 480\u001b[0m \u001b[43m \u001b[49m\u001b[43mn_jobs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mn_jobs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 481\u001b[0m \u001b[43m \u001b[49m\u001b[43mcatch\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mtuple\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43misinstance\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mIterable\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m,\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 482\u001b[0m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 483\u001b[0m \u001b[43m \u001b[49m\u001b[43mgc_after_trial\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mgc_after_trial\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 484\u001b[0m \u001b[43m \u001b[49m\u001b[43mshow_progress_bar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mshow_progress_bar\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 485\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py:63\u001b[0m, in \u001b[0;36m_optimize\u001b[0;34m(study, func, n_trials, timeout, n_jobs, catch, callbacks, gc_after_trial, show_progress_bar)\u001b[0m\n\u001b[1;32m 61\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 62\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m n_jobs \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m---> 63\u001b[0m \u001b[43m_optimize_sequential\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 64\u001b[0m \u001b[43m \u001b[49m\u001b[43mstudy\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 65\u001b[0m \u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 66\u001b[0m \u001b[43m \u001b[49m\u001b[43mn_trials\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 67\u001b[0m \u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 68\u001b[0m \u001b[43m \u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 69\u001b[0m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 70\u001b[0m \u001b[43m \u001b[49m\u001b[43mgc_after_trial\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 71\u001b[0m \u001b[43m \u001b[49m\u001b[43mreseed_sampler_rng\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 72\u001b[0m \u001b[43m \u001b[49m\u001b[43mtime_start\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 73\u001b[0m \u001b[43m \u001b[49m\u001b[43mprogress_bar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mprogress_bar\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 74\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 75\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 76\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m n_jobs \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m:\n", - "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py:160\u001b[0m, in \u001b[0;36m_optimize_sequential\u001b[0;34m(study, func, n_trials, timeout, catch, callbacks, gc_after_trial, reseed_sampler_rng, time_start, progress_bar)\u001b[0m\n\u001b[1;32m 157\u001b[0m \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 160\u001b[0m frozen_trial \u001b[38;5;241m=\u001b[39m \u001b[43m_run_trial\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstudy\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcatch\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 161\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m 162\u001b[0m \u001b[38;5;66;03m# The following line mitigates memory problems that can be occurred in some\u001b[39;00m\n\u001b[1;32m 163\u001b[0m \u001b[38;5;66;03m# environments (e.g., services that use computing containers such as GitHub Actions).\u001b[39;00m\n\u001b[1;32m 164\u001b[0m \u001b[38;5;66;03m# Please refer to the following PR for further details:\u001b[39;00m\n\u001b[1;32m 165\u001b[0m \u001b[38;5;66;03m# https://github.com/optuna/optuna/pull/325.\u001b[39;00m\n\u001b[1;32m 166\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m gc_after_trial:\n", - "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py:248\u001b[0m, in \u001b[0;36m_run_trial\u001b[0;34m(study, func, catch)\u001b[0m\n\u001b[1;32m 241\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mShould not reach.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 243\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\n\u001b[1;32m 244\u001b[0m frozen_trial\u001b[38;5;241m.\u001b[39mstate \u001b[38;5;241m==\u001b[39m TrialState\u001b[38;5;241m.\u001b[39mFAIL\n\u001b[1;32m 245\u001b[0m \u001b[38;5;129;01mand\u001b[39;00m func_err \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 246\u001b[0m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(func_err, catch)\n\u001b[1;32m 247\u001b[0m ):\n\u001b[0;32m--> 248\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m func_err\n\u001b[1;32m 249\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m frozen_trial\n", - "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py:197\u001b[0m, in \u001b[0;36m_run_trial\u001b[0;34m(study, func, catch)\u001b[0m\n\u001b[1;32m 195\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m get_heartbeat_thread(trial\u001b[38;5;241m.\u001b[39m_trial_id, study\u001b[38;5;241m.\u001b[39m_storage):\n\u001b[1;32m 196\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 197\u001b[0m value_or_values \u001b[38;5;241m=\u001b[39m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtrial\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 198\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m exceptions\u001b[38;5;241m.\u001b[39mTrialPruned \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 199\u001b[0m \u001b[38;5;66;03m# TODO(mamu): Handle multi-objective cases.\u001b[39;00m\n\u001b[1;32m 200\u001b[0m state \u001b[38;5;241m=\u001b[39m TrialState\u001b[38;5;241m.\u001b[39mPRUNED\n", - "Cell \u001b[0;32mIn[22], line 18\u001b[0m, in \u001b[0;36mobjective\u001b[0;34m(trial)\u001b[0m\n\u001b[1;32m 15\u001b[0m lr \u001b[38;5;241m=\u001b[39m trial\u001b[38;5;241m.\u001b[39msuggest_float(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlearning_rate\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;241m0.00001\u001b[39m, \u001b[38;5;241m0.1\u001b[39m) \n\u001b[1;32m 16\u001b[0m model \u001b[38;5;241m=\u001b[39m compile_model(model, lr\u001b[38;5;241m=\u001b[39mlr)\n\u001b[0;32m---> 18\u001b[0m model_history \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 19\u001b[0m \u001b[43m \u001b[49m\u001b[43mtrain_data_gen\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 20\u001b[0m \u001b[43m \u001b[49m\u001b[43mvalidation_data\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mval_data_gen\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 21\u001b[0m \u001b[43m \u001b[49m\u001b[43mepochs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m50\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 22\u001b[0m \u001b[43m \u001b[49m\u001b[43mclass_weight\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m{\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mDATASET_RATIO\u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 23\u001b[0m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43mTFKerasPruningCallback\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtrial\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mval_auc\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;66;43;03m# get rid of attempts with unpromising hyperparam combination\u001b[39;49;00m\n\u001b[1;32m 24\u001b[0m \u001b[43m \u001b[49m\u001b[43mK\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcallbacks\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mEarlyStopping\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpatience\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrestore_best_weights\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 25\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 27\u001b[0m num_epochs_trained \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39margmax(model_history\u001b[38;5;241m.\u001b[39mhistory[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mval_auc\u001b[39m\u001b[38;5;124m'\u001b[39m])\n\u001b[1;32m 28\u001b[0m val_auc \u001b[38;5;241m=\u001b[39m model_history\u001b[38;5;241m.\u001b[39mhistory[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mval_auc\u001b[39m\u001b[38;5;124m'\u001b[39m][num_epochs_trained]\n", - "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py:67\u001b[0m, in \u001b[0;36mfilter_traceback..error_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 65\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e: \u001b[38;5;66;03m# pylint: disable=broad-except\u001b[39;00m\n\u001b[1;32m 66\u001b[0m filtered_tb \u001b[38;5;241m=\u001b[39m _process_traceback_frames(e\u001b[38;5;241m.\u001b[39m__traceback__)\n\u001b[0;32m---> 67\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\u001b[38;5;241m.\u001b[39mwith_traceback(filtered_tb) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28mNone\u001b[39m\n\u001b[1;32m 68\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m 69\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m filtered_tb\n", - "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tensorflow/python/eager/execute.py:54\u001b[0m, in \u001b[0;36mquick_execute\u001b[0;34m(op_name, num_outputs, inputs, attrs, ctx, name)\u001b[0m\n\u001b[1;32m 52\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 53\u001b[0m ctx\u001b[38;5;241m.\u001b[39mensure_initialized()\n\u001b[0;32m---> 54\u001b[0m tensors \u001b[38;5;241m=\u001b[39m pywrap_tfe\u001b[38;5;241m.\u001b[39mTFE_Py_Execute(ctx\u001b[38;5;241m.\u001b[39m_handle, device_name, op_name,\n\u001b[1;32m 55\u001b[0m inputs, attrs, num_outputs)\n\u001b[1;32m 56\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m core\u001b[38;5;241m.\u001b[39m_NotOkStatusException \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 57\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m name \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", - "\u001b[0;31mUnimplementedError\u001b[0m: Graph execution error:\n\nDetected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 194, in _run_module_as_main\n return _run_code(code, main_globals, None,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 87, in _run_code\n exec(code, run_globals)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py\", line 17, in \n app.launch_new_instance()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py\", line 1043, in launch_instance\n app.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py\", line 725, in start\n self.io_loop.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py\", line 215, in start\n self.asyncio_loop.run_forever()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 570, in run_forever\n self._run_once()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 1859, in _run_once\n handle._run()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py\", line 81, in _run\n self._context.run(self._callback, *self._args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 513, in dispatch_queue\n await self.process_one()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 502, in process_one\n await dispatch(*args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 409, in dispatch_shell\n await result\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 729, in execute_request\n reply_content = await reply_content\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py\", line 422, in do_execute\n res = shell.run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py\", line 540, in run_cell\n return super().run_cell(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 2961, in run_cell\n result = self._run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3016, in _run_cell\n result = runner(coro)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py\", line 129, in _pseudo_sync_runner\n coro.send(None)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3221, in run_cell_async\n has_raised = await self.run_ast_nodes(code_ast.body, cell_name,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3400, in run_ast_nodes\n if await self.run_code(code, result, async_=asy):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3460, in run_code\n exec(code_obj, self.user_global_ns, self.user_ns)\n File \"/tmp/ipykernel_7812/2048605125.py\", line 2, in \n study.optimize(objective, n_trials=20)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py\", line 475, in optimize\n _optimize(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 63, in _optimize\n _optimize_sequential(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 160, in _optimize_sequential\n frozen_trial = _run_trial(study, func, catch)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 197, in _run_trial\n value_or_values = func(trial)\n File \"/tmp/ipykernel_7812/1713806393.py\", line 18, in objective\n model_history = model.fit(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1409, in fit\n tmp_logs = self.train_function(iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1051, in train_function\n return step_function(self, iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1040, in step_function\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1030, in run_step\n outputs = model.train_step(data)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 889, in train_step\n y_pred = self(x, training=True)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 490, in __call__\n return super().__call__(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 458, in call\n return self._run_internal_graph(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 596, in _run_internal_graph\n outputs = node.layer(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 250, in call\n outputs = self.convolution_op(inputs, self.kernel)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 225, in convolution_op\n return tf.nn.convolution(\nNode: 'miRBind_CNN/conv2d/Conv2D'\nDetected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 194, in _run_module_as_main\n return _run_code(code, main_globals, None,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py\", line 87, in _run_code\n exec(code, run_globals)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py\", line 17, in \n app.launch_new_instance()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py\", line 1043, in launch_instance\n app.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py\", line 725, in start\n self.io_loop.start()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py\", line 215, in start\n self.asyncio_loop.run_forever()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 570, in run_forever\n self._run_once()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py\", line 1859, in _run_once\n handle._run()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py\", line 81, in _run\n self._context.run(self._callback, *self._args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 513, in dispatch_queue\n await self.process_one()\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 502, in process_one\n await dispatch(*args)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 409, in dispatch_shell\n await result\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py\", line 729, in execute_request\n reply_content = await reply_content\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py\", line 422, in do_execute\n res = shell.run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py\", line 540, in run_cell\n return super().run_cell(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 2961, in run_cell\n result = self._run_cell(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3016, in _run_cell\n result = runner(coro)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py\", line 129, in _pseudo_sync_runner\n coro.send(None)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3221, in run_cell_async\n has_raised = await self.run_ast_nodes(code_ast.body, cell_name,\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3400, in run_ast_nodes\n if await self.run_code(code, result, async_=asy):\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py\", line 3460, in run_code\n exec(code_obj, self.user_global_ns, self.user_ns)\n File \"/tmp/ipykernel_7812/2048605125.py\", line 2, in \n study.optimize(objective, n_trials=20)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py\", line 475, in optimize\n _optimize(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 63, in _optimize\n _optimize_sequential(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 160, in _optimize_sequential\n frozen_trial = _run_trial(study, func, catch)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py\", line 197, in _run_trial\n value_or_values = func(trial)\n File \"/tmp/ipykernel_7812/1713806393.py\", line 18, in objective\n model_history = model.fit(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1409, in fit\n tmp_logs = self.train_function(iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1051, in train_function\n return step_function(self, iterator)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1040, in step_function\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 1030, in run_step\n outputs = model.train_step(data)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 889, in train_step\n y_pred = self(x, training=True)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py\", line 490, in __call__\n return super().__call__(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 458, in call\n return self._run_internal_graph(\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py\", line 596, in _run_internal_graph\n outputs = node.layer(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 64, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py\", line 1014, in __call__\n outputs = call_fn(inputs, *args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py\", line 92, in error_handler\n return fn(*args, **kwargs)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 250, in call\n outputs = self.convolution_op(inputs, self.kernel)\n File \"/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py\", line 225, in convolution_op\n return tf.nn.convolution(\nNode: 'miRBind_CNN/conv2d/Conv2D'\n2 root error(s) found.\n (0) UNIMPLEMENTED: DNN library is not found.\n\t [[{{node miRBind_CNN/conv2d/Conv2D}}]]\n\t [[assert_greater_equal/Assert/AssertGuard/pivot_f/_3/_41]]\n (1) UNIMPLEMENTED: DNN library is not found.\n\t [[{{node miRBind_CNN/conv2d/Conv2D}}]]\n0 successful operations.\n0 derived errors ignored. [Op:__inference_train_function_4880]" - ] - } - ], - "source": [ - "study = optuna.create_study(direction='maximize', study_name='miRBind_CNN')\n", - "study.optimize(objective, n_trials=20)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5c710194-6b3b-4589-83ed-b170fb812015", - "metadata": {}, - "outputs": [], - "source": [ - "logger.info(\"\\n\")\n", - "logger.info(f\"Best hyperparameters: {study.best_params}\")\n", - "logger.info(f\"Best value (validation AU PRC): {study.best_value}\")" - ] - }, - { - "cell_type": "markdown", - "id": "29b123f2-ec2f-46bb-87d0-08a3c95d757f", - "metadata": {}, - "source": [ - "Let's plot now how the optimization process went, what was the best set of hyperparameters etc." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "978cee44-b292-4075-823a-f6a7f7e32b03", - "metadata": {}, - "outputs": [], - "source": [ - "vis.plot_optimization_history(study)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6830924e-dc38-42de-b8f3-138ffb69cc67", - "metadata": {}, - "outputs": [], - "source": [ - "vis.plot_optimization_history(study)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "908574f9-2800-4733-bdc5-08fe81fc7e8c", - "metadata": {}, - "outputs": [], - "source": [ - "vis.plot_contour(study)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9f6406a0-4c1e-4258-80c4-6075b91149dd", - "metadata": {}, - "outputs": [], - "source": [ - "vis.plot_param_importances(study)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "dbf36bfc-fb06-4062-a4b0-e211131ef5d1", - "metadata": {}, - "outputs": [], - "source": [ - "vis.plot_slice(study)" - ] - }, - { - "cell_type": "markdown", - "id": "cf32651c-49f8-43db-a572-7f6863027ee8", - "metadata": {}, - "source": [ - "If you want to, you can save the plots like this:" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "id": "122b996f-24c8-49d2-8ded-0e54453081f9", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "[W 2024-12-19 15:16:20,937] There are no complete trials.\n" - ] - }, - { - "ename": "ValueError", - "evalue": "\nImage export using the \"kaleido\" engine requires the kaleido package,\nwhich can be installed using pip:\n $ pip install -U kaleido\n", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[25], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m fig \u001b[38;5;241m=\u001b[39m vis\u001b[38;5;241m.\u001b[39mplot_optimization_history(study)\n\u001b[0;32m----> 2\u001b[0m \u001b[43mfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwrite_image\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43moptimization_history.png\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/plotly/basedatatypes.py:3821\u001b[0m, in \u001b[0;36mBaseFigure.write_image\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 3761\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 3762\u001b[0m \u001b[38;5;124;03mConvert a figure to a static image and write it to a file or writeable\u001b[39;00m\n\u001b[1;32m 3763\u001b[0m \u001b[38;5;124;03mobject\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 3817\u001b[0m \u001b[38;5;124;03mNone\u001b[39;00m\n\u001b[1;32m 3818\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 3819\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mplotly\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mio\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mpio\u001b[39;00m\n\u001b[0;32m-> 3821\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mpio\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwrite_image\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/plotly/io/_kaleido.py:267\u001b[0m, in \u001b[0;36mwrite_image\u001b[0;34m(fig, file, format, scale, width, height, validate, engine)\u001b[0m\n\u001b[1;32m 251\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 252\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 253\u001b[0m \u001b[38;5;124;03mCannot infer image type from output path '{file}'.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 261\u001b[0m )\n\u001b[1;32m 262\u001b[0m )\n\u001b[1;32m 264\u001b[0m \u001b[38;5;66;03m# Request image\u001b[39;00m\n\u001b[1;32m 265\u001b[0m \u001b[38;5;66;03m# -------------\u001b[39;00m\n\u001b[1;32m 266\u001b[0m \u001b[38;5;66;03m# Do this first so we don't create a file if image conversion fails\u001b[39;00m\n\u001b[0;32m--> 267\u001b[0m img_data \u001b[38;5;241m=\u001b[39m \u001b[43mto_image\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 268\u001b[0m \u001b[43m \u001b[49m\u001b[43mfig\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 269\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mformat\u001b[39;49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mformat\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 270\u001b[0m \u001b[43m \u001b[49m\u001b[43mscale\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mscale\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 271\u001b[0m \u001b[43m \u001b[49m\u001b[43mwidth\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwidth\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 272\u001b[0m \u001b[43m \u001b[49m\u001b[43mheight\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mheight\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 273\u001b[0m \u001b[43m \u001b[49m\u001b[43mvalidate\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mvalidate\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 274\u001b[0m \u001b[43m \u001b[49m\u001b[43mengine\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mengine\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 275\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 277\u001b[0m \u001b[38;5;66;03m# Open file\u001b[39;00m\n\u001b[1;32m 278\u001b[0m \u001b[38;5;66;03m# ---------\u001b[39;00m\n\u001b[1;32m 279\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m path \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 280\u001b[0m \u001b[38;5;66;03m# We previously failed to make sense of `file` as a pathlib object.\u001b[39;00m\n\u001b[1;32m 281\u001b[0m \u001b[38;5;66;03m# Attempt to write to `file` as an open file descriptor.\u001b[39;00m\n", - "File \u001b[0;32m~/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/plotly/io/_kaleido.py:133\u001b[0m, in \u001b[0;36mto_image\u001b[0;34m(fig, format, width, height, scale, validate, engine)\u001b[0m\n\u001b[1;32m 131\u001b[0m \u001b[38;5;66;03m# Raise informative error message if Kaleido is not installed\u001b[39;00m\n\u001b[1;32m 132\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m scope \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 133\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 134\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 135\u001b[0m \u001b[38;5;124;03mImage export using the \"kaleido\" engine requires the kaleido package,\u001b[39;00m\n\u001b[1;32m 136\u001b[0m \u001b[38;5;124;03mwhich can be installed using pip:\u001b[39;00m\n\u001b[1;32m 137\u001b[0m \u001b[38;5;124;03m $ pip install -U kaleido\u001b[39;00m\n\u001b[1;32m 138\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 139\u001b[0m )\n\u001b[1;32m 141\u001b[0m \u001b[38;5;66;03m# Validate figure\u001b[39;00m\n\u001b[1;32m 142\u001b[0m \u001b[38;5;66;03m# ---------------\u001b[39;00m\n\u001b[1;32m 143\u001b[0m fig_dict \u001b[38;5;241m=\u001b[39m validate_coerce_fig_to_dict(fig, validate)\n", - "\u001b[0;31mValueError\u001b[0m: \nImage export using the \"kaleido\" engine requires the kaleido package,\nwhich can be installed using pip:\n $ pip install -U kaleido\n" - ] - } - ], - "source": [ - "fig = vis.plot_optimization_history(study)\n", - "fig.write_image(\"optimization_history.png\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9e02ff5e-d791-4fe3-91c8-99900ad682ff", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python [conda env:deepExperimentTF2.7]", - "language": "python", - "name": "conda-env-deepExperimentTF2.7-py" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.16" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/data_generators.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/data_generators.py new file mode 100644 index 0000000..63691c6 --- /dev/null +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/data_generators.py @@ -0,0 +1,86 @@ +import os +import numpy as np +from tensorflow.keras.utils import Sequence + + +class TrainDataGenerator(Sequence): + def __init__(self, data_path, labels_path, dataset_size, batch_size=32, validation_split=0.1, is_validation=False, shuffle=True): + self.size = dataset_size + self.data = np.memmap(data_path, dtype='float32', mode='r', shape=(self.size, 50, 20, 1)) + self.labels = np.memmap(labels_path, dtype='float32', mode='r', shape=(self.size,)) + self.batch_size = batch_size + self.shuffle = shuffle + self.validation_split = validation_split + self.num_samples = len(self.data) + self.num_validation_samples = int(self.num_samples * validation_split) + self.num_train_samples = self.num_samples - self.num_validation_samples + + indices = np.arange(self.num_samples) + if shuffle: + np.random.shuffle(indices) + + if is_validation: + self.indices = indices[self.num_train_samples:] + else: + self.indices = indices[:self.num_train_samples] + + self.on_epoch_end() + + def __len__(self): + return int(np.ceil(len(self.indices) / float(self.batch_size))) + + def __getitem__(self, idx): + batch_indices = self.indices[idx * self.batch_size:(idx + 1) * self.batch_size] + batch_data = self.data[batch_indices] + batch_labels = self.labels[batch_indices] + return batch_data, batch_labels + + def on_epoch_end(self): + if self.shuffle: + np.random.shuffle(self.indices) + + +class TestDataGenerator: + def __init__(self, data_path, labels_path, batch_size=32, dataset_size=None): + if dataset_size is None: + # Try to determine the dataset size by checking file properties + try: + # First approach: try to load just the header to get shape and dtype + with open(data_path, 'rb') as f: + if f.read(6) == b'\x93NUMPY': + # This is a standard numpy file, we can get info from header + f.seek(0) + version = np.lib.format.read_magic(f) + shape_dict = np.lib.format.read_array_header_1_0(f) if version == (1, 0) else np.lib.format.read_array_header_2_0(f) + shape = shape_dict[0] + dataset_size = shape[0] + else: + # Not a standard numpy file, we'll try other methods + raise ValueError("Not a standard numpy file") + except: + # Second approach: try to infer from file size + # This assumes the files are memory-mapped in a specific format + # For dataset: shape=(n, 50, 20, 1), dtype=float32 (4 bytes) + # For labels: shape=(n,), dtype=float32 (4 bytes) + data_size_bytes = os.path.getsize(data_path) + labels_size_bytes = os.path.getsize(labels_path) + + # Calculate dataset_size based on assumed structure + dataset_size_from_data = data_size_bytes // (4 * 50 * 20) + dataset_size_from_labels = labels_size_bytes // 4 + + # Verify sizes match approximately + if abs(dataset_size_from_data - dataset_size_from_labels) < 10: + dataset_size = dataset_size_from_data + else: + raise ValueError(f"Inconsistent file sizes: data suggests {dataset_size_from_data} samples, labels suggests {dataset_size_from_labels}") + + # Create memory-mapped arrays with the determined size + self.data = np.memmap(data_path, dtype='float32', mode='r', shape=(dataset_size, 50, 20, 1)) + self.labels = np.memmap(labels_path, dtype='float32', mode='r', shape=(dataset_size,)) + self.batch_size = batch_size + self.num_samples = dataset_size + + def get_data(self): + """Return all test data and labels""" + return self.data, self.labels \ No newline at end of file diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization-tmp_made_from_notebook.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization-tmp_made_from_notebook.py deleted file mode 100644 index ed021a9..0000000 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization-tmp_made_from_notebook.py +++ /dev/null @@ -1,202 +0,0 @@ -# Hyperparam optimization notebook -""" -In this notebook, we will try to optimize the hyperparameters of the miRBind CNN model. Quick guide to what is a [parameter vs. hyperparameter](https://machinelearningmastery.com/difference-between-a-parameter-and-a-hyperparameter/). - -We will use [Optuna](https://optuna.org/) framework for this. It will try for us a bunch of different hyperparameter settings and see what combination works the best. - -Let's try to optimize number of blocks with convolution layer, kernel size of the convolution, size of the pooling layer, number of blocks with the dense layer and learning rate - these are our hyperparameters. - -Our metrics to optimize will be the AU PRC on the validation set (we split the train set into actual training set and validation set). -""" - -import numpy as np -from tensorflow import keras as K -from tensorflow.keras.optimizers import Adam -from tensorflow.keras.utils import Sequence - -import plotly -import logging -import optuna -import optuna.visualization as vis -from optuna.integration import TFKerasPruningCallback - -import sys -sys.path.append("../../../code/machine_learning/train/CNN_miRBind_2022/") - -from miRBind_CNN_architecture import miRBind_CNN - -# it's here for to be able to display plots in jupyter notebook -plotly.io.renderers.default = 'iframe' - -def compile_model(model, lr): - - opt = Adam( - learning_rate=lr, - beta_1=0.9, - beta_2=0.999, - epsilon=1e-07, - amsgrad=False, - name="Adam") - - model.compile( - optimizer=opt, - loss='binary_crossentropy', - metrics=['accuracy', K.metrics.AUC(curve='PR')] # adding the metrics on which we want to optimize - ) - return model - -class DataGenerator(Sequence): - def __init__(self, data_path, labels_path, dataset_size, batch_size=32, validation_split=0.1, is_validation=False, shuffle=True): - # preload the encoded numpy data - # the size needed to properly load the array - self.size = dataset_size - - self.data = np.memmap(data_path, dtype='float32', mode='r', shape=(self.size, 50, 20, 1)) - self.labels = np.memmap(labels_path, dtype='float32', mode='r', shape=(self.size,)) - self.batch_size = batch_size - self.shuffle = shuffle - - # Determine number of train and validation samples - self.validation_split = validation_split - self.num_samples = len(self.data) - self.num_validation_samples = int(self.num_samples * validation_split) - self.num_train_samples = self.num_samples - self.num_validation_samples - - # Determine indices for validation and training - indices = np.arange(self.num_samples) - if shuffle: - np.random.shuffle(indices) - - if is_validation: - self.indices = indices[self.num_train_samples:] - else: - self.indices = indices[:self.num_train_samples] - - # Shuffle the data initially - self.on_epoch_end() - - def __len__(self): - # Denotes the number of batches per epoch - return int(np.ceil(len(self.indices) / float(self.batch_size))) - - def __getitem__(self, idx): - # Generate one batch of data - batch_indices = self.indices[idx * self.batch_size:(idx + 1) * self.batch_size] - batch_data = self.data[batch_indices] - batch_labels = self.labels[batch_indices] - return batch_data, batch_labels - - def on_epoch_end(self): - # Updates indices after each epoch for shuffling - if self.shuffle: - np.random.shuffle(self.indices) - -"""------------------------------ -Choose a dataset on which you want to train. It has to be already encoded with the ```binding_2D_matrix_encoder.py``` -""" - -# DATASET = "../../../AmiRBench/code/dataset_vOct/Manakov_1_train_dataset.npy" -# DATASET = '../miRBind_CNN_retraining_orig_parameters/encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_dataset.npy' -DATASET = '../../miRBind_CNN_retraining_orig_parameters/encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_dataset.npy' - -# LABELS = "../../../AmiRBench/code/dataset_vOct/Manakov_1_train_labels.npy" -# LABELS = '../miRBind_CNN_retraining_orig_parameters/encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_labels.npy' -LABELS = '../../miRBind_CNN_retraining_orig_parameters/encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_labels.npy' - -DATASET_RATIO = 1 -# DATASET_SIZE = 2524246 -DATASET_SIZE = 2516195 - -import pandas as pd -pd.read_csv("../../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train.tsv", sep='\t') - -train_data_gen = DataGenerator(DATASET, LABELS, dataset_size=DATASET_SIZE, validation_split=0.1, is_validation=False) - -val_data_gen = DataGenerator(DATASET, LABELS, dataset_size=DATASET_SIZE, validation_split=0.1, is_validation=True) - -"""---------------------------- -This is the function that creates a model with suggested hyperparameters, trains it and sees how well it performs on the validation set - -**Some explanations** - -`trial` is the object that "carries the information" about the hyperparameter optimization. `trial.suggest_` means "give me some value for the hyperparameter" that might work well for the model. - -`TFKerasPruningCallback` is another hack, where you can stop unpromising training in the middle and scratch it. E.g. when you are training the model with some hyperparameters and after few epochs you see the model doesn't learn anything, you can simply stop the training, remember that this set of hyperparameters didn't work well and you don't have to waste time with worthless training finishing. - -`K.callbacks.EarlyStopping` - early stopping method helps to train for the right amount of epochs. It monitors the performance on the validation set and when the model starts overfitting and performing worse, it stops the training. -""" - -best_model = None -best_val_auc = 0 - -def objective(trial): - global best_model, best_val_auc - - K.backend.clear_session() - - # build the model based on suggested hyperparameters - cnn_num = trial.suggest_int('cnn_layers_num', 2, 10) - kernel_size = trial.suggest_int('kernel_size', 3, 10) - pool_size = trial.suggest_int('pool_size', 1, 8) - dense_num = trial.suggest_int('dense_layers_num', 2, cnn_num) - model = miRBind_CNN(cnn_num=cnn_num, kernel_size=kernel_size, pool_size=pool_size, dense_num=dense_num).model - lr = trial.suggest_float('learning_rate', 0.00001, 0.1) - model = compile_model(model, lr=lr) - - model_history = model.fit( - train_data_gen, - validation_data=val_data_gen, - epochs=50, - class_weight={0: 1, 1: DATASET_RATIO}, - callbacks=[TFKerasPruningCallback(trial, "val_auc"), # get rid of attempts with unpromising hyperparam combination - K.callbacks.EarlyStopping(patience=5, restore_best_weights=True)], - ) - - num_epochs_trained = np.argmax(model_history.history['val_auc']) - val_auc = model_history.history['val_auc'][num_epochs_trained] - - # check performance of this trial - if val_auc > best_val_auc: - best_val_auc = val_auc - best_model = model # save the current best model - model.save('best_model.keras') # save the model to disk - logger.info(f"New best model found and saved with Validation AUC: {val_auc}") - - print(f"Validation AU PRC: {val_auc}") - - return val_auc - -"""Set up a logger for logging the optimization process to a file""" - -logger = logging.getLogger('optuna') -logger.setLevel(logging.INFO) -file_handler = logging.FileHandler('hyperparam_optimization.log', 'w') -file_handler.setFormatter(logging.Formatter('%(asctime)s - %(message)s')) -logger.addHandler(file_handler) - -"""This is the place where we start running the optimization process""" - -study = optuna.create_study(direction='maximize', study_name='miRBind_CNN') -study.optimize(objective, n_trials=20) - -logger.info("\n") -logger.info(f"Best hyperparameters: {study.best_params}") -logger.info(f"Best value (validation AU PRC): {study.best_value}") - -"""Let's plot now how the optimization process went, what was the best set of hyperparameters etc.""" - -vis.plot_optimization_history(study) - -vis.plot_optimization_history(study) - -vis.plot_contour(study) - -vis.plot_param_importances(study) - -vis.plot_slice(study) - -"""If you want to, you can save the plots like this:""" - -fig = vis.plot_optimization_history(study) -fig.write_image("optimization_history.png") - diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py index c740418..1d850d1 100644 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py @@ -1,53 +1,27 @@ -#!/usr/bin/env python3 - import argparse import numpy as np import logging -from tensorflow import keras as K -from tensorflow.keras.optimizers import Adam -from tensorflow.keras.utils import Sequence import optuna import optuna.visualization as vis from optuna.integration import TFKerasPruningCallback +from tensorflow import keras as K +from tensorflow.keras.optimizers import Adam +from tensorflow.keras.utils import Sequence +import tensorflow as tf +import random + +from data_generators import TrainDataGenerator import sys -sys.path.append("../../../code/machine_learning/train/CNN_miRBind_2022/") +sys.path.append("../../../code/machine_learning/train/CNN_miRBind_2022/") from miRBind_CNN_architecture import miRBind_CNN -class DataGenerator(Sequence): - def __init__(self, data_path, labels_path, dataset_size, batch_size=32, validation_split=0.1, is_validation=False, shuffle=True): - self.size = dataset_size - self.data = np.memmap(data_path, dtype='float32', mode='r', shape=(self.size, 50, 20, 1)) - self.labels = np.memmap(labels_path, dtype='float32', mode='r', shape=(self.size,)) - self.batch_size = batch_size - self.shuffle = shuffle - self.validation_split = validation_split - self.num_samples = len(self.data) - self.num_validation_samples = int(self.num_samples * validation_split) - self.num_train_samples = self.num_samples - self.num_validation_samples - - indices = np.arange(self.num_samples) - if shuffle: - np.random.shuffle(indices) - - if is_validation: - self.indices = indices[self.num_train_samples:] - else: - self.indices = indices[:self.num_train_samples] - - self.on_epoch_end() - - def __len__(self): - return int(np.ceil(len(self.indices) / float(self.batch_size))) - - def __getitem__(self, idx): - batch_indices = self.indices[idx * self.batch_size:(idx + 1) * self.batch_size] - batch_data = self.data[batch_indices] - batch_labels = self.labels[batch_indices] - return batch_data, batch_labels - - def on_epoch_end(self): - if self.shuffle: - np.random.shuffle(self.indices) + +def set_seeds(seed): + """Set seeds for reproducibility.""" + random.seed(seed) + np.random.seed(seed) + tf.random.set_seed(seed) + def compile_model(model, lr): opt = Adam( @@ -65,7 +39,8 @@ def compile_model(model, lr): ) return model -def objective(trial, train_data_gen, val_data_gen, dataset_ratio): + +def objective(trial, train_data_gen, val_data_gen, dataset_ratio, best_model_path, epochs): global best_model, best_val_auc K.backend.clear_session() @@ -75,17 +50,17 @@ def objective(trial, train_data_gen, val_data_gen, dataset_ratio): pool_size = trial.suggest_int('pool_size', 1, 8) dense_num = trial.suggest_int('dense_layers_num', 2, cnn_num) model = miRBind_CNN(cnn_num=cnn_num, kernel_size=kernel_size, pool_size=pool_size, dense_num=dense_num).model - lr = trial.suggest_float('learning_rate', 0.00001, 0.1) + lr = trial.suggest_float('learning_rate', 0.00001, 0.0001) model = compile_model(model, lr=lr) model_history = model.fit( train_data_gen, validation_data=val_data_gen, - epochs=50, + epochs=epochs, class_weight={0: 1, 1: dataset_ratio}, callbacks=[ TFKerasPruningCallback(trial, "val_auc"), - K.callbacks.EarlyStopping(patience=5, restore_best_weights=True) + K.callbacks.EarlyStopping(patience=3, restore_best_weights=True) ], ) @@ -95,12 +70,13 @@ def objective(trial, train_data_gen, val_data_gen, dataset_ratio): if val_auc > best_val_auc: best_val_auc = val_auc best_model = model - model.save('best_model.keras') + model.save(best_model_path) logger.info(f"New best model found and saved with Validation AUC: {val_auc}") print(f"Validation AU PRC: {val_auc}") return val_auc + def setup_logger(log_file): logger = logging.getLogger('optuna') logger.setLevel(logging.INFO) @@ -109,14 +85,15 @@ def setup_logger(log_file): logger.addHandler(file_handler) return logger + def main(): parser = argparse.ArgumentParser(description='Hyperparameter optimization for miRBind CNN model') - parser.add_argument('--dataset', type=str, - default='../../miRBind_CNN_retraining_orig_parameters/encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_dataset.npy', - help='Path to the training dataset') - parser.add_argument('--labels', type=str, - default='../../miRBind_CNN_retraining_orig_parameters/encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_labels.npy', - help='Path to the training labels') + parser.add_argument('--dataset-train', type=str, + default='../encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train_dataset.npy', + help='Path to the train dataset') + parser.add_argument('--labels-train', type=str, + default='../encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train_labels.npy', + help='Path to the train labels') parser.add_argument('--dataset-size', type=int, default=2516195, help='Size of the dataset') parser.add_argument('--dataset-ratio', type=float, default=1, @@ -127,47 +104,63 @@ def main(): help='Validation split ratio') parser.add_argument('--n-trials', type=int, default=20, help='Number of optimization trials') + parser.add_argument('--epochs', type=int, default=5, + help='Number of max epochs per model') + parser.add_argument('--best-model', type=str, default='best_model.log', + help='Path to the model trained with optimised hyperparameters') parser.add_argument('--log-file', type=str, default='hyperparam_optimization.log', help='Path to the log file') parser.add_argument('--save-plots', action='store_true', help='Save optimization plots') + parser.add_argument('--seed', type=int, default=42, + help='Random seed for reproducibility') args = parser.parse_args() + # Set seeds for reproducibility + set_seeds(args.seed) + global logger, best_model, best_val_auc logger = setup_logger(args.log_file) best_model = None best_val_auc = 0 - # Initialize data generators - train_data_gen = DataGenerator( - args.dataset, - args.labels, + logger.info(f"Starting optimization with seed: {args.seed}") + + train_data_gen = TrainDataGenerator( + args.dataset_train, + args.labels_train, dataset_size=args.dataset_size, batch_size=args.batch_size, validation_split=args.validation_split, is_validation=False ) - val_data_gen = DataGenerator( - args.dataset, - args.labels, + val_data_gen = TrainDataGenerator( + args.dataset_train, + args.labels_train, dataset_size=args.dataset_size, batch_size=args.batch_size, validation_split=args.validation_split, is_validation=True ) - # Create and run the study - study = optuna.create_study(direction='maximize', study_name='miRBind_CNN') - study.optimize(lambda trial: objective(trial, train_data_gen, val_data_gen, args.dataset_ratio), - n_trials=args.n_trials) + # Set seed for Optuna + optuna_sampler = optuna.samplers.TPESampler(seed=args.seed) + study = optuna.create_study( + direction='maximize', + study_name='miRBind_CNN', + sampler=optuna_sampler + ) + + study.optimize( + lambda trial: objective(trial, train_data_gen, val_data_gen, args.dataset_ratio, args.best_model, args.epochs), + n_trials=args.n_trials + ) - # Log results logger.info("\n") logger.info(f"Best hyperparameters: {study.best_params}") logger.info(f"Best value (validation AU PRC): {study.best_value}") - # Save plots if requested if args.save_plots: plots = { 'optimization_history': vis.plot_optimization_history, @@ -183,5 +176,6 @@ def main(): except Exception as e: logger.error(f"Failed to save {name} plot: {str(e)}") + if __name__ == "__main__": main() \ No newline at end of file diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.sh b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.sh new file mode 100755 index 0000000..1948468 --- /dev/null +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.sh @@ -0,0 +1,24 @@ +#!/bin/bash + + +train_file="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train" + +train_file_size=2516195 +model_dir="mirBind_2002" +best_model_path="models/${model_dir}/best_model.keras" +evaluation_out_dir="evaluation_results/${model_dir}" + + +# run hyper parameter optimisation +python hyperparam_optimization.py \ + --dataset-train "../${train_file}_dataset.npy" \ + --labels-train "../${train_file}_labels.npy" \ + --dataset-size $train_file_size \ + --dataset-ratio 1 \ + --batch-size 32 \ + --validation-split 0.1 \ + --n-trials 30 \ + --best-model "$best_model_path" \ + --log-file "${evaluation_out_dir}/hyperparam_optimization.log" + --seed 42 + --epochs 8 \ No newline at end of file From c4bafe4ec94e81e66c7313bfa2413bbc9d063a4a Mon Sep 17 00:00:00 2001 From: davidcechak Date: Thu, 20 Feb 2025 15:56:09 +0100 Subject: [PATCH 09/20] Clean up logs which should not be in git --- .../hyperparam_optimization.log | 207 ------------------ 1 file changed, 207 deletions(-) delete mode 100644 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.log diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.log b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.log deleted file mode 100644 index aa801f1..0000000 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.log +++ /dev/null @@ -1,207 +0,0 @@ -2024-12-19 15:16:16,997 - A new study created in memory with name: miRBind_CNN -2024-12-19 15:16:20,727 - Trial 0 failed with parameters: {'cnn_layers_num': 7, 'kernel_size': 6, 'pool_size': 5, 'dense_layers_num': 5, 'learning_rate': 0.05887436900846115} because of the following error: UnimplementedError(). -Traceback (most recent call last): - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py", line 197, in _run_trial - value_or_values = func(trial) - File "/tmp/ipykernel_7812/1713806393.py", line 18, in objective - model_history = model.fit( - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler - raise e.with_traceback(filtered_tb) from None - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tensorflow/python/eager/execute.py", line 54, in quick_execute - tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, -tensorflow.python.framework.errors_impl.UnimplementedError: Graph execution error: - -Detected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last): - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py", line 194, in _run_module_as_main - return _run_code(code, main_globals, None, - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py", line 87, in _run_code - exec(code, run_globals) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py", line 17, in - app.launch_new_instance() - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py", line 1043, in launch_instance - app.start() - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py", line 725, in start - self.io_loop.start() - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py", line 215, in start - self.asyncio_loop.run_forever() - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py", line 570, in run_forever - self._run_once() - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py", line 1859, in _run_once - handle._run() - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py", line 81, in _run - self._context.run(self._callback, *self._args) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 513, in dispatch_queue - await self.process_one() - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 502, in process_one - await dispatch(*args) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 409, in dispatch_shell - await result - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 729, in execute_request - reply_content = await reply_content - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py", line 422, in do_execute - res = shell.run_cell( - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py", line 540, in run_cell - return super().run_cell(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 2961, in run_cell - result = self._run_cell( - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3016, in _run_cell - result = runner(coro) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner - coro.send(None) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3221, in run_cell_async - has_raised = await self.run_ast_nodes(code_ast.body, cell_name, - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3400, in run_ast_nodes - if await self.run_code(code, result, async_=asy): - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3460, in run_code - exec(code_obj, self.user_global_ns, self.user_ns) - File "/tmp/ipykernel_7812/2048605125.py", line 2, in - study.optimize(objective, n_trials=20) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py", line 475, in optimize - _optimize( - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py", line 63, in _optimize - _optimize_sequential( - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py", line 160, in _optimize_sequential - frozen_trial = _run_trial(study, func, catch) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py", line 197, in _run_trial - value_or_values = func(trial) - File "/tmp/ipykernel_7812/1713806393.py", line 18, in objective - model_history = model.fit( - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler - return fn(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1409, in fit - tmp_logs = self.train_function(iterator) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1051, in train_function - return step_function(self, iterator) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1040, in step_function - outputs = model.distribute_strategy.run(run_step, args=(data,)) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1030, in run_step - outputs = model.train_step(data) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 889, in train_step - y_pred = self(x, training=True) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler - return fn(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 490, in __call__ - return super().__call__(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler - return fn(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py", line 1014, in __call__ - outputs = call_fn(inputs, *args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler - return fn(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py", line 458, in call - return self._run_internal_graph( - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py", line 596, in _run_internal_graph - outputs = node.layer(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler - return fn(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py", line 1014, in __call__ - outputs = call_fn(inputs, *args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler - return fn(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py", line 250, in call - outputs = self.convolution_op(inputs, self.kernel) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py", line 225, in convolution_op - return tf.nn.convolution( -Node: 'miRBind_CNN/conv2d/Conv2D' -Detected at node 'miRBind_CNN/conv2d/Conv2D' defined at (most recent call last): - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py", line 194, in _run_module_as_main - return _run_code(code, main_globals, None, - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/runpy.py", line 87, in _run_code - exec(code, run_globals) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel_launcher.py", line 17, in - app.launch_new_instance() - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/traitlets/config/application.py", line 1043, in launch_instance - app.start() - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelapp.py", line 725, in start - self.io_loop.start() - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/tornado/platform/asyncio.py", line 215, in start - self.asyncio_loop.run_forever() - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py", line 570, in run_forever - self._run_once() - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/base_events.py", line 1859, in _run_once - handle._run() - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/asyncio/events.py", line 81, in _run - self._context.run(self._callback, *self._args) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 513, in dispatch_queue - await self.process_one() - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 502, in process_one - await dispatch(*args) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 409, in dispatch_shell - await result - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/kernelbase.py", line 729, in execute_request - reply_content = await reply_content - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/ipkernel.py", line 422, in do_execute - res = shell.run_cell( - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/ipykernel/zmqshell.py", line 540, in run_cell - return super().run_cell(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 2961, in run_cell - result = self._run_cell( - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3016, in _run_cell - result = runner(coro) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner - coro.send(None) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3221, in run_cell_async - has_raised = await self.run_ast_nodes(code_ast.body, cell_name, - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3400, in run_ast_nodes - if await self.run_code(code, result, async_=asy): - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3460, in run_code - exec(code_obj, self.user_global_ns, self.user_ns) - File "/tmp/ipykernel_7812/2048605125.py", line 2, in - study.optimize(objective, n_trials=20) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/study.py", line 475, in optimize - _optimize( - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py", line 63, in _optimize - _optimize_sequential( - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py", line 160, in _optimize_sequential - frozen_trial = _run_trial(study, func, catch) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/optuna/study/_optimize.py", line 197, in _run_trial - value_or_values = func(trial) - File "/tmp/ipykernel_7812/1713806393.py", line 18, in objective - model_history = model.fit( - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler - return fn(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1409, in fit - tmp_logs = self.train_function(iterator) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1051, in train_function - return step_function(self, iterator) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1040, in step_function - outputs = model.distribute_strategy.run(run_step, args=(data,)) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 1030, in run_step - outputs = model.train_step(data) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 889, in train_step - y_pred = self(x, training=True) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler - return fn(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/training.py", line 490, in __call__ - return super().__call__(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler - return fn(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py", line 1014, in __call__ - outputs = call_fn(inputs, *args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler - return fn(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py", line 458, in call - return self._run_internal_graph( - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/functional.py", line 596, in _run_internal_graph - outputs = node.layer(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler - return fn(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/engine/base_layer.py", line 1014, in __call__ - outputs = call_fn(inputs, *args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler - return fn(*args, **kwargs) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py", line 250, in call - outputs = self.convolution_op(inputs, self.kernel) - File "/home/jovyan/my-conda-envs/deepExperimentTF2.7/lib/python3.8/site-packages/keras/layers/convolutional/base_conv.py", line 225, in convolution_op - return tf.nn.convolution( -Node: 'miRBind_CNN/conv2d/Conv2D' -2 root error(s) found. - (0) UNIMPLEMENTED: DNN library is not found. - [[{{node miRBind_CNN/conv2d/Conv2D}}]] - [[assert_greater_equal/Assert/AssertGuard/pivot_f/_3/_41]] - (1) UNIMPLEMENTED: DNN library is not found. - [[{{node miRBind_CNN/conv2d/Conv2D}}]] -0 successful operations. -0 derived errors ignored. [Op:__inference_train_function_4880] -2024-12-19 15:16:20,730 - Trial 0 failed with value None. -2024-12-19 15:16:20,937 - There are no complete trials. From f9299c1bd984115acb7e89033a3a14636df611d8 Mon Sep 17 00:00:00 2001 From: davidcechak Date: Thu, 20 Feb 2025 15:57:14 +0100 Subject: [PATCH 10/20] Evaluate a trained model on test datasets --- .../hyperparam_optimization/evaluate_model.py | 138 ++++++++++++++++++ .../hyperparam_optimization/evaluate_model.sh | 31 ++++ .../hyperparam_optimization/plots.py | 34 +++++ 3 files changed, 203 insertions(+) create mode 100644 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.py create mode 100755 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.sh create mode 100644 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/plots.py diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.py new file mode 100644 index 0000000..64d6524 --- /dev/null +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.py @@ -0,0 +1,138 @@ +import argparse +import os +import logging +import numpy as np +from tensorflow import keras as K +from sklearn.metrics import precision_recall_curve, auc, roc_curve, roc_auc_score, accuracy_score, average_precision_score + +from data_generators import TestDataGenerator +from plots import plot_roc_curve, plot_pr_curve + + +def setup_logger(log_file): + """Set up a logger to record evaluation results""" + logger = logging.getLogger('model_evaluation') + logger.setLevel(logging.INFO) + + # Create handlers + file_handler = logging.FileHandler(log_file, 'w') + console_handler = logging.StreamHandler() + + # Create formatters and add to handlers + formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') + file_handler.setFormatter(formatter) + console_handler.setFormatter(formatter) + + # Add handlers to logger + logger.addHandler(file_handler) + logger.addHandler(console_handler) + + return logger + + +def evaluate_model(model, test_data, test_labels, logger, save_plots=True, output_dir='.', pred_threshold=0.5): + """Evaluate model performance""" + # Get predictions from prediction probabilities + y_pred_proba = model.predict(test_data) + y_pred = (y_pred_proba > pred_threshold).astype(int) + + # Calculate metrics + accuracy = accuracy_score(test_labels, y_pred) + + fpr, tpr, _ = roc_curve(test_labels, y_pred_proba) + roc_auc = roc_auc_score(test_labels, y_pred_proba) + + precision, recall, _ = precision_recall_curve(test_labels, y_pred_proba) + pr_auc = auc(recall, precision) + + avg_precision = average_precision_score(test_labels, y_pred_proba) + + logger.info(f"Model Evaluation Results:") + logger.info(f"Accuracy: {accuracy:.4f}") + logger.info(f"ROC AUC: {roc_auc:.4f}") + logger.info(f"PR AUC: {pr_auc:.4f}") + logger.info(f"Average Precision: {avg_precision:.4f}") + + if save_plots: + os.makedirs(output_dir, exist_ok=True) + + plot_roc_curve(fpr, tpr, roc_auc, output_dir, logger, fig_save_name='roc_curve.png') + plot_pr_curve(recall, precision, pr_auc, avg_precision, output_dir, logger, fig_save_name='pr_curve.png') + + return { + 'accuracy': accuracy, + 'roc_auc': roc_auc, + 'pr_auc': pr_auc, + 'avg_precision': avg_precision, + 'fpr': fpr, + 'tpr': tpr, + 'precision': precision, + 'recall': recall + } + + +def main(): + parser = argparse.ArgumentParser(description='Evaluate trained miRBind CNN model') + parser.add_argument('--model-path', type=str, default='best_model.keras', + help='Path to the trained model file') + parser.add_argument('--dataset-test', type=str, + default='../encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test_dataset.npy', + help='Path to the test dataset') + parser.add_argument('--labels-test', type=str, + default='../encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test_labels.npy', + help='Path to the test labels') + parser.add_argument('--dataset-size', type=int, default=None, + help='Size of the test dataset (number of samples). If not provided, will attempt to determine automatically.') + parser.add_argument('--batch-size', type=int, default=32, + help='Batch size for evaluation') + parser.add_argument('--log-file', type=str, default='model_evaluation.log', + help='Path to the log file') + parser.add_argument('--save-plots', action='store_true', default=True, + help='Save evaluation plots') + parser.add_argument('--output-dir', type=str, default='evaluation_results', + help='Directory to save evaluation results') + args = parser.parse_args() + + # Set up logger + logger = setup_logger(os.path.join(args.output_dir, args.log_file)) + logger.info("Starting model evaluation") + + try: + logger.info(f"Loading model from {args.model_path}") + model = K.models.load_model(args.model_path) + logger.info(f"Model loaded successfully") + + logger.info(f"Loading test data from {args.dataset_test}") + + test_data_generator = TestDataGenerator( + args.dataset_test, + args.labels_test, + batch_size=args.batch_size, + dataset_size=args.dataset_size + ) + test_data, test_labels = test_data_generator.get_data() + logger.info(f"Dataset size: {len(test_data)} samples") + + logger.info("Evaluating model performance...") + results = evaluate_model( + model, + test_data, + test_labels, + logger, + save_plots=args.save_plots, + output_dir=args.output_dir + ) + + logger.info("Model evaluation completed successfully") + + # Save model summary + with open(os.path.join(args.output_dir, 'model_summary.txt'), 'w') as f: + model.summary(print_fn=lambda x: f.write(x + '\n')) + logger.info(f"Model summary saved to {os.path.join(args.output_dir, 'model_summary.txt')}") + + except Exception as e: + logger.error(f"An error occurred during model evaluation: {str(e)}") + raise + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.sh b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.sh new file mode 100755 index 0000000..aafac41 --- /dev/null +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +test_file_out="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test" +leftout_file_out="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_leftout" + +# best_model_path="models/best_model.keras" +best_model_path="evaluation_results/mirBind_2002/models_tmp/best_model.keras" +# evaluation_out_dir="evaluation_results/mirBind_1902" +evaluation_out_dir="evaluation_results/mirBind_2002/models_tmp" + +# evaluate the best model +python evaluate_model.py \ + --model-path $best_model_path \ + --dataset-test "../${test_file_out}_dataset.npy" \ + --labels-test "../${test_file_out}_labels.npy" \ + --batch-size 32 \ + --log-file "model_evaluation_test.log" \ + --save-plots \ + --output-dir $evaluation_out_dir + +python evaluate_model.py \ + --model-path $best_model_path \ + --dataset-test "../${leftout_file_out}_dataset.npy" \ + --labels-test "../${leftout_file_out}_labels.npy" \ + --batch-size 32 \ + --log-file "model_evaluation_leftout.log" \ + --save-plots \ + --output-dir $evaluation_out_dir + + +# hyperparam_optimization/evaluation_results/mirBind_2002/models_tmp/best_model.keras \ No newline at end of file diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/plots.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/plots.py new file mode 100644 index 0000000..aa01400 --- /dev/null +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/plots.py @@ -0,0 +1,34 @@ +import os +import matplotlib.pyplot as plt + + +def plot_roc_curve(fpr, tpr, roc_auc, output_dir, logger, fig_save_name='roc_curve.png'): + save_path = os.path.join(output_dir, fig_save_name) + + plt.figure(figsize=(10, 8)) + plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (area = {roc_auc:.3f})') + plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--') + plt.xlim([0.0, 1.0]) + plt.ylim([0.0, 1.05]) + plt.xlabel('False Positive Rate') + plt.ylabel('True Positive Rate') + plt.title('Receiver Operating Characteristic (ROC) Curve') + plt.legend(loc="lower right") + plt.savefig(save_path) + logger.info(f"Saved ROC curve plot to {save_path}") + + +def plot_pr_curve(recall, precision, pr_auc, avg_precision, output_dir, logger, fig_save_name='pr_curve.png'): + save_path = os.path.join(output_dir, fig_save_name) + + plt.figure(figsize=(10, 8)) + plt.plot(recall, precision, color='green', lw=2, + label=f'PR curve (area = {pr_auc:.3f}, avg precision = {avg_precision:.3f})') + plt.xlabel('Recall') + plt.ylabel('Precision') + plt.ylim([0.0, 1.05]) + plt.xlim([0.0, 1.0]) + plt.title('Precision-Recall Curve') + plt.legend(loc="lower left") + plt.savefig(save_path) + logger.info(f"Saved PR curve plot to {save_path}") \ No newline at end of file From f7d0f924e5dc6241d8908a9b1f9f5743668894d1 Mon Sep 17 00:00:00 2001 From: davidcechak Date: Thu, 20 Feb 2025 15:58:15 +0100 Subject: [PATCH 11/20] Train a model with custom hyperparameters --- .../hyperparam_optimization/train_model.py | 216 ++++++++++++++++++ .../hyperparam_optimization/train_model.sh | 25 ++ .../hyperparam_optimization/utils.py | 29 +++ 3 files changed, 270 insertions(+) create mode 100755 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py create mode 100755 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh create mode 100644 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/utils.py diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py new file mode 100755 index 0000000..83d321a --- /dev/null +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py @@ -0,0 +1,216 @@ +import argparse +import numpy as np +import logging +import tensorflow as tf +from tensorflow import keras as K +from tensorflow.keras.optimizers import Adam +from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, CSVLogger +import random +import os +import sys +import matplotlib.pyplot as plt + +# Import the data generator +from utils import set_seeds, setup_logger +from data_generators import TrainDataGenerator +sys.path.append("../../../code/machine_learning/train/CNN_miRBind_2022/") +from miRBind_CNN_architecture import miRBind_CNN + + +def plot_training_history(history, output_dir): + """Plot and save training metrics.""" + # Create a figure with 3 subplots + plt.figure(figsize=(18, 5)) + + # Plot accuracy + plt.subplot(1, 3, 1) + plt.plot(history.history['accuracy']) + plt.plot(history.history['val_accuracy']) + plt.title('Model Accuracy') + plt.ylabel('Accuracy') + plt.xlabel('Epoch') + plt.legend(['Train', 'Validation'], loc='upper left') + + # Plot AUPRC + plt.subplot(1, 3, 2) + plt.plot(history.history['auprc']) + plt.plot(history.history['val_auprc']) + plt.title('Area Under PR Curve') + plt.ylabel('AUPRC') + plt.xlabel('Epoch') + plt.legend(['Train', 'Validation'], loc='upper left') + + # Plot loss + plt.subplot(1, 3, 3) + plt.plot(history.history['loss']) + plt.plot(history.history['val_loss']) + plt.title('Model Loss') + plt.ylabel('Loss') + plt.xlabel('Epoch') + plt.legend(['Train', 'Validation'], loc='upper left') + + plt.tight_layout() + plt.savefig(os.path.join(output_dir, 'training_history.png')) + plt.close() + + +def main(): + parser = argparse.ArgumentParser(description='Train miRBind CNN model with specified hyperparameters') + + # Data parameters + parser.add_argument('--dataset-train', type=str, required=True, + help='Path to the training dataset (numpy array)') + parser.add_argument('--labels-train', type=str, required=True, + help='Path to the training labels (numpy array)') + parser.add_argument('--dataset-size', type=int, required=True, + help='Size of the dataset (number of samples)') + parser.add_argument('--validation-split', type=float, default=0.1, + help='Validation split ratio (default: 0.1)') + + # Model architecture parameters + parser.add_argument('--cnn-num', type=int, default=6, + help='Number of CNN layers (default: 6)') + parser.add_argument('--kernel-size', type=int, default=5, + help='Kernel size for CNN layers (default: 5)') + parser.add_argument('--pool-size', type=int, default=2, + help='Pool size for MaxPooling layers (default: 2)') + parser.add_argument('--dropout-rate', type=float, default=0.3, + help='Dropout rate (default: 0.3)') + parser.add_argument('--dense-num', type=int, default=2, + help='Number of dense layers (default: 2)') + + # Training parameters + parser.add_argument('--learning-rate', type=float, default=0.00001, + help='Learning rate (default: 0.00152)') + parser.add_argument('--batch-size', type=int, default=32, + help='Batch size for training (default: 32)') + parser.add_argument('--epochs', type=int, default=30, + help='Number of epochs to train (default: 30)') + parser.add_argument('--patience', type=int, default=5, + help='Patience for early stopping (default: 5)') + parser.add_argument('--class-weight', type=float, default=1.0, + help='Weight for positive class (default: 1.0)') + + # Output parameters + parser.add_argument('--output-dir', type=str, default='./model_output', + help='Directory to save model and logs (default: ./model_output)') + parser.add_argument('--model-name', type=str, default='mirbind_cnn_model', + help='Name for the saved model (default: mirbind_cnn_model)') + parser.add_argument('--log-file', type=str, default='training.log', + help='Path to the log file (default: training.log)') + + # Misc parameters + parser.add_argument('--seed', type=int, default=42, + help='Random seed for reproducibility (default: 42)') + + args = parser.parse_args() + + # Create output directory if it doesn't exist + os.makedirs(args.output_dir, exist_ok=True) + + # Set up logging + log_path = os.path.join(args.output_dir, args.log_file) + logger = setup_logger(log_path) + + # Set seeds for reproducibility + set_seeds(args.seed) + logger.info(f"Starting training with seed: {args.seed}") + + # Log all parameters + logger.info("Training with the following parameters:") + for arg in vars(args): + logger.info(f" {arg}: {getattr(args, arg)}") + + # Prepare data generators + logger.info("Preparing data generators...") + train_data_gen = TrainDataGenerator( + args.dataset_train, + args.labels_train, + dataset_size=args.dataset_size, + batch_size=args.batch_size, + validation_split=args.validation_split, + is_validation=False + ) + + val_data_gen = TrainDataGenerator( + args.dataset_train, + args.labels_train, + dataset_size=args.dataset_size, + batch_size=args.batch_size, + validation_split=args.validation_split, + is_validation=True + ) + + # Build model with specified hyperparameters + logger.info("Building model...") + model_instance = miRBind_CNN( + cnn_num=args.cnn_num, + kernel_size=args.kernel_size, + pool_size=args.pool_size, + dropout_rate=args.dropout_rate, + dense_num=args.dense_num + ) + + # Compile model + model = model_instance.compile_model(lr=args.learning_rate) + + # Log model summary + model.summary(print_fn=logger.info) + + # Prepare callbacks + logger.info("Setting up training callbacks...") + callbacks = [ + ModelCheckpoint( + filepath=os.path.join(args.output_dir, f"{args.model_name}_best.keras"), + monitor='val_auprc', + save_best_only=True, + mode='max', + verbose=1 + ), + EarlyStopping( + monitor='val_auprc', + patience=args.patience, + restore_best_weights=True, + verbose=1 + ), + CSVLogger( + os.path.join(args.output_dir, 'training_log.csv') + ) + ] + + # Class weights + class_weights = {0: 1, 1: args.class_weight} + + # Train model + logger.info("Starting model training...") + history = model.fit( + train_data_gen, + validation_data=val_data_gen, + epochs=args.epochs, + class_weight=class_weights, + callbacks=callbacks, + verbose=1 + ) + + # Plot training history + logger.info("Plotting training history...") + plot_training_history(history, args.output_dir) + + # Save final model + final_model_path = os.path.join(args.output_dir, f"{args.model_name}_final.keras") + model.save(final_model_path) + logger.info(f"Final model saved to {final_model_path}") + + # Evaluate model on validation set + logger.info("Evaluating model on validation set...") + val_metrics = model.evaluate(val_data_gen, verbose=1) + metric_names = model.metrics_names + + for name, value in zip(metric_names, val_metrics): + logger.info(f"Validation {name}: {value:.4f}") + + logger.info("Training completed successfully!") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh new file mode 100755 index 0000000..448b3bf --- /dev/null +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +train_file="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train" + +train_file_size=2516195 +model_dir="mirBind_001_long_training" +out_dir="evaluation_results/${model_dir}" + + +python train_model.py \ + --dataset-train "../${train_file}_dataset.npy" \ + --labels-train "../${train_file}_labels.npy" \ + --dataset-size $train_file_size \ + --cnn-num 4 \ + --kernel-size 9 \ + --pool-size 2 \ + --dropout-rate 0.3 \ + --dense-num 2 \ + --learning-rate 0.00008241877487855944 \ + --batch-size 32 \ + --epochs 20 \ + --patience 4 \ + --output-dir out_dir \ + --model-name mirBind_001_long_training \ + --seed 42 \ No newline at end of file diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/utils.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/utils.py new file mode 100644 index 0000000..2b2aede --- /dev/null +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/utils.py @@ -0,0 +1,29 @@ +import numpy as np +import tensorflow as tf +import random +import logging + + +def set_seeds(seed): + """Set seeds for reproducibility.""" + random.seed(seed) + np.random.seed(seed) + tf.random.set_seed(seed) + + +def setup_logger(log_file): + """Configure logging to file and console.""" + logger = logging.getLogger('mirbind_train') + logger.setLevel(logging.INFO) + + # Create file handler + file_handler = logging.FileHandler(log_file, 'w') + file_handler.setFormatter(logging.Formatter('%(asctime)s - %(message)s')) + logger.addHandler(file_handler) + + # Create console handler + console_handler = logging.StreamHandler() + console_handler.setFormatter(logging.Formatter('%(asctime)s - %(message)s')) + logger.addHandler(console_handler) + + return logger \ No newline at end of file From 05f36df4fdc88b4b8a7b67c38ec4347ea18dc8af Mon Sep 17 00:00:00 2001 From: davidcechak Date: Thu, 20 Feb 2025 16:00:05 +0100 Subject: [PATCH 12/20] Run the full hyperparameter optimisation pipeline for miRBind --- .../hyperparam_optimization_pipeline.sh | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization_pipeline.sh diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization_pipeline.sh b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization_pipeline.sh new file mode 100644 index 0000000..83ea39b --- /dev/null +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization_pipeline.sh @@ -0,0 +1,56 @@ +#!/bin/bash + + +train_file_in="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train.tsv" +test_file_in="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test.tsv" +leftout_file_in="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_leftout.tsv" + +train_file_out="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train" +test_file_out="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test" +leftout_file_out="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_leftout" + +train_file_size=2516195 +best_model_path="models/best_model.keras" +evaluation_out_dir="evaluation_results/mirBind_1902" + +CODE="../../code/machine_learning" + +mkdir -p encoded_dataset/Manakov2022_flat + +# encode datasets +python $CODE/encode/binding_2D_matrix_encoder.py --i_file $train_file_in --o_prefix $train_file_out +python $CODE/encode/binding_2D_matrix_encoder.py --i_file $test_file_in --o_prefix $test_file_out +python $CODE/encode/binding_2D_matrix_encoder.py --i_file $leftout_file_in --o_prefix $leftout_file_out + +# run hyper parameter optimisation +python hyperparam_optimization/hyperparam_optimization.py \ + --dataset-train "../${train_file_out}_dataset.npy" \ + --labels-train "../${train_file_out}_labels.npy" \ + --dataset-size $train_file_size \ + --dataset-ratio 1 \ + --batch-size 32 \ + --validation-split 0.1 \ + --n-trials 20 \ + --best-model $best_model_path \ + --log-file "hyperparam_optimization.log" + --seed 42 + --epochs 5 + +# evaluate the best model +python hyperparam_optimization/evaluate_model.py \ + --model-path $best_model_path \ + --dataset-test "../${test_file_out}_dataset.npy" \ + --labels-test "../${test_file_out}_labels.npy" \ + --batch-size 32 \ + --log-file "model_evaluation_test.log" \ + --save-plots \ + --output-dir $evaluation_out_dir + +python hyperparam_optimization/evaluate_model.py \ + --model-path $best_model_path \ + --dataset-test "../${leftout_file_out}_dataset.npy" \ + --labels-test "../${leftout_file_out}_labels.npy" \ + --batch-size 32 \ + --log-file "model_evaluation_leftout.log" \ + --save-plots \ + --output-dir $evaluation_out_dir From ffff74ccc524a4fbb201b543137580c472e873a7 Mon Sep 17 00:00:00 2001 From: davidcechak Date: Mon, 24 Feb 2025 13:07:56 +0100 Subject: [PATCH 13/20] Fix model log output paths --- .../hyperparam_optimization/train_model.py | 10 +++++----- .../hyperparam_optimization/train_model.sh | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py index 83d321a..bf7bfd0 100755 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py @@ -33,10 +33,10 @@ def plot_training_history(history, output_dir): # Plot AUPRC plt.subplot(1, 3, 2) - plt.plot(history.history['auprc']) - plt.plot(history.history['val_auprc']) + plt.plot(history.history['auc']) + plt.plot(history.history['val_auc']) plt.title('Area Under PR Curve') - plt.ylabel('AUPRC') + plt.ylabel('AUC') plt.xlabel('Epoch') plt.legend(['Train', 'Validation'], loc='upper left') @@ -162,13 +162,13 @@ def main(): callbacks = [ ModelCheckpoint( filepath=os.path.join(args.output_dir, f"{args.model_name}_best.keras"), - monitor='val_auprc', + monitor='val_auc', save_best_only=True, mode='max', verbose=1 ), EarlyStopping( - monitor='val_auprc', + monitor='val_auc', patience=args.patience, restore_best_weights=True, verbose=1 diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh index 448b3bf..9023c15 100755 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh @@ -3,8 +3,8 @@ train_file="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train" train_file_size=2516195 -model_dir="mirBind_001_long_training" -out_dir="evaluation_results/${model_dir}" +model_name="mirBind_001_long_training" +out_dir="evaluation_results/${model_name}" python train_model.py \ @@ -18,8 +18,8 @@ python train_model.py \ --dense-num 2 \ --learning-rate 0.00008241877487855944 \ --batch-size 32 \ - --epochs 20 \ - --patience 4 \ - --output-dir out_dir \ - --model-name mirBind_001_long_training \ + --epochs 70 \ + --patience 6 \ + --output-dir $out_dir \ + --model-name $model_name \ --seed 42 \ No newline at end of file From 4f9bd9c0642873722747c56de34e7058ef4dcbfd Mon Sep 17 00:00:00 2001 From: davidcechak Date: Mon, 24 Feb 2025 15:39:21 +0100 Subject: [PATCH 14/20] =?UTF-8?q?Model=20training:=20Add=20error=C2=A0hand?= =?UTF-8?q?ling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hyperparam_optimization/train_model.py | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py index bf7bfd0..b643619 100755 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py @@ -105,23 +105,20 @@ def main(): args = parser.parse_args() - # Create output directory if it doesn't exist os.makedirs(args.output_dir, exist_ok=True) + if not os.path.exists(args.output_dir): + raise RuntimeError(f"Failed to create output directory: {args.output_dir}") - # Set up logging log_path = os.path.join(args.output_dir, args.log_file) logger = setup_logger(log_path) - # Set seeds for reproducibility set_seeds(args.seed) logger.info(f"Starting training with seed: {args.seed}") - # Log all parameters logger.info("Training with the following parameters:") for arg in vars(args): logger.info(f" {arg}: {getattr(args, arg)}") - # Prepare data generators logger.info("Preparing data generators...") train_data_gen = TrainDataGenerator( args.dataset_train, @@ -141,7 +138,6 @@ def main(): is_validation=True ) - # Build model with specified hyperparameters logger.info("Building model...") model_instance = miRBind_CNN( cnn_num=args.cnn_num, @@ -151,13 +147,10 @@ def main(): dense_num=args.dense_num ) - # Compile model model = model_instance.compile_model(lr=args.learning_rate) - # Log model summary model.summary(print_fn=logger.info) - # Prepare callbacks logger.info("Setting up training callbacks...") callbacks = [ ModelCheckpoint( @@ -178,10 +171,8 @@ def main(): ) ] - # Class weights class_weights = {0: 1, 1: args.class_weight} - # Train model logger.info("Starting model training...") history = model.fit( train_data_gen, @@ -192,16 +183,17 @@ def main(): verbose=1 ) - # Plot training history - logger.info("Plotting training history...") - plot_training_history(history, args.output_dir) - - # Save final model final_model_path = os.path.join(args.output_dir, f"{args.model_name}_final.keras") - model.save(final_model_path) + try: + model.save(final_model_path) + except Exception as e: + logger.error(f"Failed to save model: {str(e)}") + raise logger.info(f"Final model saved to {final_model_path}") - # Evaluate model on validation set + logger.info("Plotting training history...") + plot_training_history(history, args.output_dir) + logger.info("Evaluating model on validation set...") val_metrics = model.evaluate(val_data_gen, verbose=1) metric_names = model.metrics_names From ebfc31538223245e24a963409cb274be1d173ddf Mon Sep 17 00:00:00 2001 From: davidcechak Date: Mon, 24 Feb 2025 17:03:15 +0100 Subject: [PATCH 15/20] miRBind: extract model compilation to utils --- .../hyperparam_optimization.py | 25 +------------------ .../hyperparam_optimization/train_model.py | 5 ++-- .../hyperparam_optimization/utils.py | 17 +++++++++++++ 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py index 1d850d1..96029fe 100644 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py @@ -10,36 +10,13 @@ import tensorflow as tf import random +from utils import set_seeds, compile_model from data_generators import TrainDataGenerator import sys sys.path.append("../../../code/machine_learning/train/CNN_miRBind_2022/") from miRBind_CNN_architecture import miRBind_CNN -def set_seeds(seed): - """Set seeds for reproducibility.""" - random.seed(seed) - np.random.seed(seed) - tf.random.set_seed(seed) - - -def compile_model(model, lr): - opt = Adam( - learning_rate=lr, - beta_1=0.9, - beta_2=0.999, - epsilon=1e-07, - amsgrad=False, - name="Adam") - - model.compile( - optimizer=opt, - loss='binary_crossentropy', - metrics=['accuracy', K.metrics.AUC(curve='PR')] - ) - return model - - def objective(trial, train_data_gen, val_data_gen, dataset_ratio, best_model_path, epochs): global best_model, best_val_auc diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py index b643619..a425ac6 100755 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py @@ -10,8 +10,7 @@ import sys import matplotlib.pyplot as plt -# Import the data generator -from utils import set_seeds, setup_logger +from utils import set_seeds, setup_logger, compile_model from data_generators import TrainDataGenerator sys.path.append("../../../code/machine_learning/train/CNN_miRBind_2022/") from miRBind_CNN_architecture import miRBind_CNN @@ -147,7 +146,7 @@ def main(): dense_num=args.dense_num ) - model = model_instance.compile_model(lr=args.learning_rate) + model = compile_model(model_instance, lr=args.learning_rate) model.summary(print_fn=logger.info) diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/utils.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/utils.py index 2b2aede..63ee76a 100644 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/utils.py +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/utils.py @@ -9,6 +9,23 @@ def set_seeds(seed): random.seed(seed) np.random.seed(seed) tf.random.set_seed(seed) + + +def compile_model(model, lr): + opt = Adam( + learning_rate=lr, + beta_1=0.9, + beta_2=0.999, + epsilon=1e-07, + amsgrad=False, + name="Adam") + + model.compile( + optimizer=opt, + loss='binary_crossentropy', + metrics=['accuracy', K.metrics.AUC(curve='PR')] + ) + return model def setup_logger(log_file): From 73fb66a3a54d9e99ddbd90e4cb3193ac39a831bf Mon Sep 17 00:00:00 2001 From: davidcechak Date: Thu, 6 Mar 2025 13:48:08 +0100 Subject: [PATCH 16/20] miRBind model: remove pycache file --- .../miRBind_CNN_architecture.cpython-38.pyc | Bin 2215 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 code/machine_learning/train/CNN_miRBind_2022/__pycache__/miRBind_CNN_architecture.cpython-38.pyc diff --git a/code/machine_learning/train/CNN_miRBind_2022/__pycache__/miRBind_CNN_architecture.cpython-38.pyc b/code/machine_learning/train/CNN_miRBind_2022/__pycache__/miRBind_CNN_architecture.cpython-38.pyc deleted file mode 100644 index 30e85dbd1e5d04b1e759e69c633442779f34f415..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2215 zcma)7O^6&t6z>0?>6zKAW-*b7_CX{~bY^$vWRPr3mZ+F45)Fn1X)0Z{Grj4bNmWhE zEIp?UdRh-2MMRIDJbClxRlRr+>*^sa2!aSgQ1HE)nau`0STp_lRn@EaUcL9e*Ec$y zz<}|@;O6jC-!Oi`$?;*s+Pp`A9H`J4th)hS@6)#Ah?Gc$(rD|Uz_ui_iJS&GF z>~HaET)}DoN=~F?rE<4eDOifujt>_m7vT}l17Z4(u|>_;rfup`|GpVJv_qRT0M4Z? z@OVYFitPHOtfa~1?d`p9gAm50NtV;Fs3^-r!qZVESt=)-g|~=cG_1-{j#vot3z;!z zf|U}svNBw#3&&*4czCwI-tR}@NXoG|-|NvT>sEZ&+gR@o1~0GohOA^_W3bWh5BdXb z`yJ3s*$$cH5>#+fFv++8H&`fO5)z?}S8y(zpf_L=z0u~hhC>f7e~6(tw17Mq=<(xb zitEeK0R}3?xD1c@1BisGpcb_cteIDPGhdpuxo>V6@5Qr*w5U@zsC&v-Gis~0b}Wo0 zZ5%kYw{OloY3~~g&N<@T+OBOa**vfoyI$?k;J}>E?wpqXk+fO6+S-L#6Kt7v^EB9+ z1+q~G`wRL1wDP#d4a^Pf^W3p5W ziXS!-k?k?%j|rtv$Ern%B*_lexdi3SG1Rvf;m?3lgbF7Tu~(oxGKmgin){R{|R%lnn%nr0Il{bPCnm zTxN?aCkGkjLwn;{Dk6`+fExT;B)!c_3gAKy62XLk?}08F?VIw`su*WEiyR)|2`lG% zSJ|LjSJj-ngqxj(aH4Dc94v_th=EzMAWM&a550z?XU^ZAyZ7|*+&rdZJ90VpPQHTV z1d`Py7n!^VJcJJl1YwyZDoD`wBuBoTBzGnxU+nlvLaQ`Mco)xaB0)?XBX|XkdqlB7 zJU2|o37(txf>jm3mrzt3`qlf&k~ Date: Thu, 6 Mar 2025 14:50:51 +0100 Subject: [PATCH 17/20] miRBind training and evaluation: refactore logging, clean bash files --- .../hyperparam_optimization/evaluate_model.py | 28 +++--------- .../hyperparam_optimization/evaluate_model.sh | 15 +++---- .../hyperparam_optimization/plots.py | 39 ++++++++++++++++- .../hyperparam_optimization/train_model.py | 43 ++----------------- .../hyperparam_optimization/train_model.sh | 18 ++++---- .../hyperparam_optimization/utils.py | 23 ++++++---- .../hyperparam_optimization_pipeline.sh | 6 ++- 7 files changed, 82 insertions(+), 90 deletions(-) diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.py index 64d6524..1df17cb 100644 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.py +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.py @@ -7,29 +7,9 @@ from data_generators import TestDataGenerator from plots import plot_roc_curve, plot_pr_curve +from utils import setup_logger -def setup_logger(log_file): - """Set up a logger to record evaluation results""" - logger = logging.getLogger('model_evaluation') - logger.setLevel(logging.INFO) - - # Create handlers - file_handler = logging.FileHandler(log_file, 'w') - console_handler = logging.StreamHandler() - - # Create formatters and add to handlers - formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') - file_handler.setFormatter(formatter) - console_handler.setFormatter(formatter) - - # Add handlers to logger - logger.addHandler(file_handler) - logger.addHandler(console_handler) - - return logger - - def evaluate_model(model, test_data, test_labels, logger, save_plots=True, output_dir='.', pred_threshold=0.5): """Evaluate model performance""" # Get predictions from prediction probabilities @@ -93,8 +73,12 @@ def main(): help='Directory to save evaluation results') args = parser.parse_args() + os.makedirs(args.output_dir, exist_ok=True) + if not os.path.exists(args.output_dir): + raise RuntimeError(f"Failed to create output directory: {args.output_dir}") + # Set up logger - logger = setup_logger(os.path.join(args.output_dir, args.log_file)) + logger = setup_logger(os.path.join(args.output_dir, args.log_file), 'model_evaluation') logger.info("Starting model evaluation") try: diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.sh b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.sh index aafac41..d35504e 100755 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.sh +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.sh @@ -3,12 +3,12 @@ test_file_out="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test" leftout_file_out="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_leftout" -# best_model_path="models/best_model.keras" -best_model_path="evaluation_results/mirBind_2002/models_tmp/best_model.keras" -# evaluation_out_dir="evaluation_results/mirBind_1902" -evaluation_out_dir="evaluation_results/mirBind_2002/models_tmp" +# set the model_name to how you named your run +timestamp=#TODO_SET_YOUR_TRAINED_MODEL'S_TIMESTAMP +model_name="mirBind_${timestamp}" +best_model_path="evaluation_results/${model_name}/${model_name}_final.keras" +evaluation_out_dir="evaluation_results/${model_name}" -# evaluate the best model python evaluate_model.py \ --model-path $best_model_path \ --dataset-test "../${test_file_out}_dataset.npy" \ @@ -25,7 +25,4 @@ python evaluate_model.py \ --batch-size 32 \ --log-file "model_evaluation_leftout.log" \ --save-plots \ - --output-dir $evaluation_out_dir - - -# hyperparam_optimization/evaluation_results/mirBind_2002/models_tmp/best_model.keras \ No newline at end of file + --output-dir $evaluation_out_dir \ No newline at end of file diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/plots.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/plots.py index aa01400..eab2d32 100644 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/plots.py +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/plots.py @@ -31,4 +31,41 @@ def plot_pr_curve(recall, precision, pr_auc, avg_precision, output_dir, logger, plt.title('Precision-Recall Curve') plt.legend(loc="lower left") plt.savefig(save_path) - logger.info(f"Saved PR curve plot to {save_path}") \ No newline at end of file + logger.info(f"Saved PR curve plot to {save_path}") + + +def plot_training_history(history, output_dir): + """Plot and save training metrics.""" + # Create a figure with 3 subplots + plt.figure(figsize=(18, 5)) + + # Plot accuracy + plt.subplot(1, 3, 1) + plt.plot(history.history['accuracy']) + plt.plot(history.history['val_accuracy']) + plt.title('Model Accuracy') + plt.ylabel('Accuracy') + plt.xlabel('Epoch') + plt.legend(['Train', 'Validation'], loc='upper left') + + # Plot AUPRC + plt.subplot(1, 3, 2) + plt.plot(history.history['auc']) + plt.plot(history.history['val_auc']) + plt.title('Area Under PR Curve') + plt.ylabel('AUC') + plt.xlabel('Epoch') + plt.legend(['Train', 'Validation'], loc='upper left') + + # Plot loss + plt.subplot(1, 3, 3) + plt.plot(history.history['loss']) + plt.plot(history.history['val_loss']) + plt.title('Model Loss') + plt.ylabel('Loss') + plt.xlabel('Epoch') + plt.legend(['Train', 'Validation'], loc='upper left') + + plt.tight_layout() + plt.savefig(os.path.join(output_dir, 'training_history.png')) + plt.close() diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py index a425ac6..d424c3e 100755 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py @@ -11,48 +11,12 @@ import matplotlib.pyplot as plt from utils import set_seeds, setup_logger, compile_model +from plots import plot_training_history from data_generators import TrainDataGenerator sys.path.append("../../../code/machine_learning/train/CNN_miRBind_2022/") from miRBind_CNN_architecture import miRBind_CNN -def plot_training_history(history, output_dir): - """Plot and save training metrics.""" - # Create a figure with 3 subplots - plt.figure(figsize=(18, 5)) - - # Plot accuracy - plt.subplot(1, 3, 1) - plt.plot(history.history['accuracy']) - plt.plot(history.history['val_accuracy']) - plt.title('Model Accuracy') - plt.ylabel('Accuracy') - plt.xlabel('Epoch') - plt.legend(['Train', 'Validation'], loc='upper left') - - # Plot AUPRC - plt.subplot(1, 3, 2) - plt.plot(history.history['auc']) - plt.plot(history.history['val_auc']) - plt.title('Area Under PR Curve') - plt.ylabel('AUC') - plt.xlabel('Epoch') - plt.legend(['Train', 'Validation'], loc='upper left') - - # Plot loss - plt.subplot(1, 3, 3) - plt.plot(history.history['loss']) - plt.plot(history.history['val_loss']) - plt.title('Model Loss') - plt.ylabel('Loss') - plt.xlabel('Epoch') - plt.legend(['Train', 'Validation'], loc='upper left') - - plt.tight_layout() - plt.savefig(os.path.join(output_dir, 'training_history.png')) - plt.close() - - def main(): parser = argparse.ArgumentParser(description='Train miRBind CNN model with specified hyperparameters') @@ -109,7 +73,7 @@ def main(): raise RuntimeError(f"Failed to create output directory: {args.output_dir}") log_path = os.path.join(args.output_dir, args.log_file) - logger = setup_logger(log_path) + logger = setup_logger(log_path, 'mirbind_train') set_seeds(args.seed) logger.info(f"Starting training with seed: {args.seed}") @@ -138,13 +102,14 @@ def main(): ) logger.info("Building model...") + model_instance = miRBind_CNN( cnn_num=args.cnn_num, kernel_size=args.kernel_size, pool_size=args.pool_size, dropout_rate=args.dropout_rate, dense_num=args.dense_num - ) + ).model model = compile_model(model_instance, lr=args.learning_rate) diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh index 9023c15..c7158d5 100755 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh @@ -1,25 +1,27 @@ #!/bin/bash -train_file="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train" - -train_file_size=2516195 -model_name="mirBind_001_long_training" +# set the model_name to a unique name for your run +timestamp=$(date +"%Y%m%d_%H%M%S") +model_name="mirBind_${timestamp}" out_dir="evaluation_results/${model_name}" +train_file="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train" +train_file_size=2516195 python train_model.py \ --dataset-train "../${train_file}_dataset.npy" \ --labels-train "../${train_file}_labels.npy" \ --dataset-size $train_file_size \ - --cnn-num 4 \ - --kernel-size 9 \ + --cnn-num 2 \ + --kernel-size 6 \ --pool-size 2 \ --dropout-rate 0.3 \ --dense-num 2 \ --learning-rate 0.00008241877487855944 \ --batch-size 32 \ - --epochs 70 \ + --epochs 100 \ --patience 6 \ --output-dir $out_dir \ --model-name $model_name \ - --seed 42 \ No newline at end of file + --seed 42 + \ No newline at end of file diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/utils.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/utils.py index 63ee76a..9e63709 100644 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/utils.py +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/utils.py @@ -2,6 +2,8 @@ import tensorflow as tf import random import logging +from tensorflow.keras.optimizers import Adam +from tensorflow import keras as K def set_seeds(seed): @@ -28,19 +30,22 @@ def compile_model(model, lr): return model -def setup_logger(log_file): - """Configure logging to file and console.""" - logger = logging.getLogger('mirbind_train') +def setup_logger(log_file, logger_name): + """Set up a logger to file and console""" + logger = logging.getLogger(logger_name) logger.setLevel(logging.INFO) - # Create file handler + # Create handlers file_handler = logging.FileHandler(log_file, 'w') - file_handler.setFormatter(logging.Formatter('%(asctime)s - %(message)s')) - logger.addHandler(file_handler) - - # Create console handler console_handler = logging.StreamHandler() - console_handler.setFormatter(logging.Formatter('%(asctime)s - %(message)s')) + + # Create formatters and add to handlers + formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') + file_handler.setFormatter(formatter) + console_handler.setFormatter(formatter) + + # Add handlers to logger + logger.addHandler(file_handler) logger.addHandler(console_handler) return logger \ No newline at end of file diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization_pipeline.sh b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization_pipeline.sh index 83ea39b..839f500 100644 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization_pipeline.sh +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization_pipeline.sh @@ -1,5 +1,9 @@ #!/bin/bash +timestamp=$(date +"%Y%m%d_%H%M%S") +model_name="mirBind_${timestamp}" +best_model_path="models/${model_name}.keras" +evaluation_out_dir="evaluation_results/${model_name}_hyperopt" train_file_in="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train.tsv" test_file_in="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test.tsv" @@ -10,8 +14,6 @@ test_file_out="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test" leftout_file_out="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_leftout" train_file_size=2516195 -best_model_path="models/best_model.keras" -evaluation_out_dir="evaluation_results/mirBind_1902" CODE="../../code/machine_learning" From 86659299994845ea9d56f19bbe9f9c01b3a9814b Mon Sep 17 00:00:00 2001 From: davidcechak Date: Mon, 10 Mar 2025 18:52:13 +0100 Subject: [PATCH 18/20] train miRBind with original params: clean bash scripts --- .../run_data_encoding.sh | 19 +++++++++------- .../run_data_encoding.sh | 22 +++++++++---------- .../run_retraining.sh | 20 ----------------- 3 files changed, 21 insertions(+), 40 deletions(-) diff --git a/analysis/miRBind_CNN_retraining_optimized/run_data_encoding.sh b/analysis/miRBind_CNN_retraining_optimized/run_data_encoding.sh index cb4d784..bf780ef 100755 --- a/analysis/miRBind_CNN_retraining_optimized/run_data_encoding.sh +++ b/analysis/miRBind_CNN_retraining_optimized/run_data_encoding.sh @@ -1,18 +1,21 @@ #!/bin/bash -DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_leftout.tsv" -# DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test.tsv" -# DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train.tsv" +TEST_DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test.tsv" +LEFTOUT_DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_leftout.tsv" +TRAIN_DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train.tsv" -DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_leftout.tsv" -# DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test" -# DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train" + +TEST_DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_test" +LEFTOUT_DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_leftout" +TRAIN_DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train" CODE="../../code/machine_learning" mkdir -p encoded_dataset/Manakov2022_flat -# encode dataset -python $CODE/encode/binding_2D_matrix_encoder.py --i_file $DATASET --o_prefix $DATASET_OUT +# encode datasets +python $CODE/encode/binding_2D_matrix_encoder.py --i_file $TEST_DATASET --o_prefix $TEST_DATASET_OUT +python $CODE/encode/binding_2D_matrix_encoder.py --i_file $LEFTOUT_DATASET --o_prefix $LEFTOUT_DATASET_OUT +python $CODE/encode/binding_2D_matrix_encoder.py --i_file $TRAIN_DATASET --o_prefix $TRAIN_DATASET_OUT diff --git a/analysis/miRBind_CNN_retraining_orig_parameters/run_data_encoding.sh b/analysis/miRBind_CNN_retraining_orig_parameters/run_data_encoding.sh index 9e524c8..bf780ef 100755 --- a/analysis/miRBind_CNN_retraining_orig_parameters/run_data_encoding.sh +++ b/analysis/miRBind_CNN_retraining_orig_parameters/run_data_encoding.sh @@ -1,23 +1,21 @@ #!/bin/bash -# train_file = "manakov/AGO2_eCLIP_Manakov2022_train.tsv" -# test_file_1 = "manakov/AGO2_eCLIP_Manakov2022_test.tsv" -# test_file_2 = "manakov/AGO2_eCLIP_Manakov2022_leftout.tsv" -DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test.tsv" -# DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train.tsv" -# DATASET="../../data/chimeric_datasets/Manakov2022/AGO2_eCLIP_Manakov2022_1_train_dataset.tsv" -# DATASET="../../data/chimeric_datasets/Manakov2022/AGO2_eCLIP_Manakov2022_1_test_dataset.tsv" +TEST_DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test.tsv" +LEFTOUT_DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_leftout.tsv" +TRAIN_DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train.tsv" -DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_test" -# DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train" -# DATASET_OUT="encoded_dataset/AGO2_eCLIP_Manakov2022_1_test" +TEST_DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_test" +LEFTOUT_DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_leftout" +TRAIN_DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train" CODE="../../code/machine_learning" mkdir -p encoded_dataset/Manakov2022_flat -# encode dataset -python $CODE/encode/binding_2D_matrix_encoder.py --i_file $DATASET --o_prefix $DATASET_OUT +# encode datasets +python $CODE/encode/binding_2D_matrix_encoder.py --i_file $TEST_DATASET --o_prefix $TEST_DATASET_OUT +python $CODE/encode/binding_2D_matrix_encoder.py --i_file $LEFTOUT_DATASET --o_prefix $LEFTOUT_DATASET_OUT +python $CODE/encode/binding_2D_matrix_encoder.py --i_file $TRAIN_DATASET --o_prefix $TRAIN_DATASET_OUT diff --git a/analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh b/analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh index 8dd5d3b..a9e5f59 100755 --- a/analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh +++ b/analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh @@ -1,23 +1,3 @@ -# #!/bin/bash - -# DATASET="../../data/chimeric_datasets/Manakov2022/AGO2_eCLIP_Manakov2022_1_train_dataset.tsv" -# MODEL="../../models/miRBind_CNN_retrained_Manakov_1_orig_parameters.keras" -# CODE="../../code/machine_learning" - -# mkdir -p encoded_dataset - -# # encode dataset -# python $CODE/encode/binding_2D_matrix_encoder.py --i_file $DATASET --o_prefix encoded_dataset/AGO2_eCLIP_Manakov2022_1_train - -# # train model -# python $CODE/train/CNN_miRBind_2022/miRBind_CNN_training_orig_parameters.py \ -# --data encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_dataset.npy \ -# --labels encoded_dataset/AGO2_eCLIP_Manakov2022_1_train_labels.npy \ -# --dataset_size 2524246 \ -# --ratio 1 \ -# --model $MODEL - - #!/bin/bash MODEL="../../models/miRBind_CNN_retrained_Manakov_1_orig_parameters.keras" From a475694fce2b3dc63805b7c3698f6cbd3aa91d77 Mon Sep 17 00:00:00 2001 From: davidcechak Date: Mon, 10 Mar 2025 23:39:11 +0100 Subject: [PATCH 19/20] miRBind optuna optimisation: add readme --- .../hyperparam_optimization/README.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/README.md diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/README.md b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/README.md new file mode 100644 index 0000000..7e0c7a8 --- /dev/null +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/README.md @@ -0,0 +1,21 @@ +# mirBind Model optimisation pipeline + +This repository contains scripts for training and evaluating a deep learning model based on variations and tuning of the miRBind architecture for miRNA-binding prediction using eCLIP data from Manakov + +1. ../encode_dataset.sh +Converts AGO2 eCLIP datasets from Manakov2022 into the 2D matrix format. +Before running the script, data needs to be place in this directory (same for test and leftout dataset): "miRBind_2.0/data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train.tsv" + +2. hyperparam_optimization.sh +Performs hyperparameter optimization for the model. Saves the best model checkpoint, architecture description, training stats, and metrics. + +3. train_model.sh +Trains the model using the optimized hyperparameters until convergence. Saves model checkpoints and training results. +Requires setting the name (timestamp) for your model + +4. evaluate_model.sh +Evaluates the trained model on test and left-out datasets. Requires setting the name (timestamp) of your trained model. Generates performance metrics and plots. +Saves results. + +../hyperparam_optimization_pipeline.sh +Orchestrates the (almost) entire workflow (except training until convergence with found hyperpara.) in a single execution, combining dataset encoding, hyperparameter optimization, and model evaluation. \ No newline at end of file From b6dddc48acff49ba8061785bfc8ad97a856eecc9 Mon Sep 17 00:00:00 2001 From: David Cechak Date: Thu, 10 Apr 2025 09:53:23 +0200 Subject: [PATCH 20/20] miRBind merge review: move generic scripts to code/machine_learning/ --- .../hyperparam_optimization/evaluate_model.sh | 4 +- .../hyperparam_optimization.py | 4 +- .../hyperparam_optimization/train_model.sh | 4 +- .../hyperparam_optimization_pipeline.sh | 4 +- .../run_data_encoding.sh | 21 ---- .../run_retraining.sh | 104 ++++++++++++++++-- .../machine_learning}/data_generators.py | 4 +- .../evaluate}/evaluate_model.py | 2 +- .../machine_learning}/plots.py | 0 .../train/CNN_miRBind_2022}/train_model.py | 2 +- .../machine_learning}/utils.py | 0 11 files changed, 109 insertions(+), 40 deletions(-) delete mode 100755 analysis/miRBind_CNN_retraining_orig_parameters/run_data_encoding.sh rename {analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization => code/machine_learning}/data_generators.py (96%) rename {analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization => code/machine_learning/evaluate}/evaluate_model.py (98%) rename {analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization => code/machine_learning}/plots.py (100%) rename {analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization => code/machine_learning/train/CNN_miRBind_2022}/train_model.py (98%) rename {analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization => code/machine_learning}/utils.py (100%) diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.sh b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.sh index d35504e..7d8438a 100755 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.sh +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.sh @@ -9,7 +9,7 @@ model_name="mirBind_${timestamp}" best_model_path="evaluation_results/${model_name}/${model_name}_final.keras" evaluation_out_dir="evaluation_results/${model_name}" -python evaluate_model.py \ +python ../../../code/machine_learning/evaluate/evaluate_model.py \ --model-path $best_model_path \ --dataset-test "../${test_file_out}_dataset.npy" \ --labels-test "../${test_file_out}_labels.npy" \ @@ -18,7 +18,7 @@ python evaluate_model.py \ --save-plots \ --output-dir $evaluation_out_dir -python evaluate_model.py \ +python ../../../code/machine_learning/evaluate/evaluate_model.py \ --model-path $best_model_path \ --dataset-test "../${leftout_file_out}_dataset.npy" \ --labels-test "../${leftout_file_out}_labels.npy" \ diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py index 96029fe..f6bd4bd 100644 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/hyperparam_optimization.py @@ -10,8 +10,8 @@ import tensorflow as tf import random -from utils import set_seeds, compile_model -from data_generators import TrainDataGenerator +from code.machine_learning.utils import set_seeds, compile_model +from code.machine_learning.data_generators import TrainDataGenerator import sys sys.path.append("../../../code/machine_learning/train/CNN_miRBind_2022/") from miRBind_CNN_architecture import miRBind_CNN diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh index c7158d5..b8d7eac 100755 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.sh @@ -8,7 +8,9 @@ out_dir="evaluation_results/${model_name}" train_file="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train" train_file_size=2516195 -python train_model.py \ +CODE="../../code/machine_learning" + +python $CODE/train/train_model.py \ --dataset-train "../${train_file}_dataset.npy" \ --labels-train "../${train_file}_labels.npy" \ --dataset-size $train_file_size \ diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization_pipeline.sh b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization_pipeline.sh index 839f500..0320f60 100644 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization_pipeline.sh +++ b/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization_pipeline.sh @@ -39,7 +39,7 @@ python hyperparam_optimization/hyperparam_optimization.py \ --epochs 5 # evaluate the best model -python hyperparam_optimization/evaluate_model.py \ +python $CODE/evaluate/evaluate_model.py \ --model-path $best_model_path \ --dataset-test "../${test_file_out}_dataset.npy" \ --labels-test "../${test_file_out}_labels.npy" \ @@ -48,7 +48,7 @@ python hyperparam_optimization/evaluate_model.py \ --save-plots \ --output-dir $evaluation_out_dir -python hyperparam_optimization/evaluate_model.py \ +python $CODE/evaluate/evaluate_model.py \ --model-path $best_model_path \ --dataset-test "../${leftout_file_out}_dataset.npy" \ --labels-test "../${leftout_file_out}_labels.npy" \ diff --git a/analysis/miRBind_CNN_retraining_orig_parameters/run_data_encoding.sh b/analysis/miRBind_CNN_retraining_orig_parameters/run_data_encoding.sh deleted file mode 100755 index bf780ef..0000000 --- a/analysis/miRBind_CNN_retraining_orig_parameters/run_data_encoding.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - - -TEST_DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_test.tsv" -LEFTOUT_DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_leftout.tsv" -TRAIN_DATASET="../../data/chimeric_datasets/Manakov2022_flat/AGO2_eCLIP_Manakov2022_train.tsv" - - -TEST_DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_test" -LEFTOUT_DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_leftout" -TRAIN_DATASET_OUT="encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train" - - -CODE="../../code/machine_learning" - -mkdir -p encoded_dataset/Manakov2022_flat - -# encode datasets -python $CODE/encode/binding_2D_matrix_encoder.py --i_file $TEST_DATASET --o_prefix $TEST_DATASET_OUT -python $CODE/encode/binding_2D_matrix_encoder.py --i_file $LEFTOUT_DATASET --o_prefix $LEFTOUT_DATASET_OUT -python $CODE/encode/binding_2D_matrix_encoder.py --i_file $TRAIN_DATASET --o_prefix $TRAIN_DATASET_OUT diff --git a/analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh b/analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh index a9e5f59..48ec8ee 100755 --- a/analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh +++ b/analysis/miRBind_CNN_retraining_orig_parameters/run_retraining.sh @@ -1,12 +1,100 @@ #!/bin/bash -MODEL="../../models/miRBind_CNN_retrained_Manakov_1_orig_parameters.keras" +# Function to display usage instructions +usage() { + echo "Usage: $0 -t -l -r -m [-c ]" + echo " -t: Test dataset TSV file (required)" + echo " -l: Leftout dataset TSV file (required)" + echo " -r: Train dataset TSV file (required)" + echo " -m: Model path (required)" + echo " -c: Code path (optional, default: ../../code/machine_learning)" + exit 1 +} + +# Default code path CODE="../../code/machine_learning" -# train model -python $CODE/train/CNN_miRBind_2022/miRBind_CNN_training_orig_parameters.py \ ---data encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_dataset.npy \ ---labels encoded_dataset/Manakov2022_flat/AGO2_eCLIP_Manakov2022_1_train_labels.npy \ ---dataset_size 2516195 \ ---ratio 1 \ ---model $MODEL \ No newline at end of file +# Parse command-line arguments +while getopts ":t:l:r:m:c:" opt; do + case ${opt} in + t ) + TEST_DATASET=$OPTARG + ;; + l ) + LEFTOUT_DATASET=$OPTARG + ;; + r ) + TRAIN_DATASET=$OPTARG + ;; + m ) + MODEL=$OPTARG + ;; + c ) + CODE=$OPTARG + ;; + \? ) + echo "Invalid option: $OPTARG" 1>&2 + usage + ;; + : ) + echo "Invalid option: $OPTARG requires an argument" 1>&2 + usage + ;; + esac +done +shift $((OPTIND -1)) + +# Validate required arguments +if [ -z "$TEST_DATASET" ] || [ -z "$LEFTOUT_DATASET" ] || [ -z "$TRAIN_DATASET" ] || [ -z "$MODEL" ]; then + echo "Error: Missing required arguments" 1>&2 + usage +fi + +# Generate output prefixes based on input file names +TEST_DATASET_OUT="encoded_dataset/$(basename "$(dirname "$TEST_DATASET")")/$(basename "$TEST_DATASET" .tsv)" +LEFTOUT_DATASET_OUT="encoded_dataset/$(basename "$(dirname "$LEFTOUT_DATASET")")/$(basename "$LEFTOUT_DATASET" .tsv)" +TRAIN_DATASET_OUT="encoded_dataset/$(basename "$(dirname "$TRAIN_DATASET")")/$(basename "$TRAIN_DATASET" .tsv)" + +# Create output directory +mkdir -p "$(dirname "$TEST_DATASET_OUT")" + +# Function to check and encode dataset +encode_dataset() { + local input_file=$1 + local output_prefix=$2 + + # Check if the dataset and labels .npy files already exist + if [ ! -f "${output_prefix}_dataset.npy" ] || [ ! -f "${output_prefix}_labels.npy" ]; then + echo "Encoding dataset: $input_file" + python "$CODE/encode/binding_2D_matrix_encoder.py" --i_file "$input_file" --o_prefix "$output_prefix" + else + echo "Encoded files for $input_file already exist. Skipping encoding." + fi +} + +# Encode datasets +encode_dataset "$TEST_DATASET" "$TEST_DATASET_OUT" +encode_dataset "$LEFTOUT_DATASET" "$LEFTOUT_DATASET_OUT" +encode_dataset "$TRAIN_DATASET" "$TRAIN_DATASET_OUT" + +# Train model (check if training dataset files exist) +TRAIN_DATASET_NPY="${TRAIN_DATASET_OUT}_dataset.npy" +TRAIN_LABELS_NPY="${TRAIN_DATASET_OUT}_labels.npy" + +# Determine dataset size (can be modified if needed) +DATASET_SIZE=$(wc -l < "$TRAIN_DATASET") + +if [ -f "$TRAIN_DATASET_NPY" ] && [ -f "$TRAIN_LABELS_NPY" ]; then + echo "Training model..." + python "$CODE/train/CNN_miRBind_2022/miRBind_CNN_training_orig_parameters.py" \ + --data "$TRAIN_DATASET_NPY" \ + --labels "$TRAIN_LABELS_NPY" \ + --dataset_size "$DATASET_SIZE" \ + --ratio 1 \ + --model "$MODEL" +else + echo "Error: Training dataset or labels file not found. Cannot proceed with training." + exit 1 +fi + +echo "Process completed." \ No newline at end of file diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/data_generators.py b/code/machine_learning/data_generators.py similarity index 96% rename from analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/data_generators.py rename to code/machine_learning/data_generators.py index 63691c6..c94a531 100644 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/data_generators.py +++ b/code/machine_learning/data_generators.py @@ -45,7 +45,7 @@ def __init__(self, data_path, labels_path, batch_size=32, dataset_size=None): if dataset_size is None: # Try to determine the dataset size by checking file properties try: - # First approach: try to load just the header to get shape and dtype + # try to load just the header to get shape and dtype with open(data_path, 'rb') as f: if f.read(6) == b'\x93NUMPY': # This is a standard numpy file, we can get info from header @@ -58,7 +58,7 @@ def __init__(self, data_path, labels_path, batch_size=32, dataset_size=None): # Not a standard numpy file, we'll try other methods raise ValueError("Not a standard numpy file") except: - # Second approach: try to infer from file size + # try to infer from file size # This assumes the files are memory-mapped in a specific format # For dataset: shape=(n, 50, 20, 1), dtype=float32 (4 bytes) # For labels: shape=(n,), dtype=float32 (4 bytes) diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.py b/code/machine_learning/evaluate/evaluate_model.py similarity index 98% rename from analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.py rename to code/machine_learning/evaluate/evaluate_model.py index 1df17cb..19799c4 100644 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/evaluate_model.py +++ b/code/machine_learning/evaluate/evaluate_model.py @@ -5,7 +5,7 @@ from tensorflow import keras as K from sklearn.metrics import precision_recall_curve, auc, roc_curve, roc_auc_score, accuracy_score, average_precision_score -from data_generators import TestDataGenerator +from code.machine_learning.data_generators import TestDataGenerator from plots import plot_roc_curve, plot_pr_curve from utils import setup_logger diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/plots.py b/code/machine_learning/plots.py similarity index 100% rename from analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/plots.py rename to code/machine_learning/plots.py diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py b/code/machine_learning/train/CNN_miRBind_2022/train_model.py similarity index 98% rename from analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py rename to code/machine_learning/train/CNN_miRBind_2022/train_model.py index d424c3e..a1160ad 100755 --- a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/train_model.py +++ b/code/machine_learning/train/CNN_miRBind_2022/train_model.py @@ -12,7 +12,7 @@ from utils import set_seeds, setup_logger, compile_model from plots import plot_training_history -from data_generators import TrainDataGenerator +from code.machine_learning.data_generators import TrainDataGenerator sys.path.append("../../../code/machine_learning/train/CNN_miRBind_2022/") from miRBind_CNN_architecture import miRBind_CNN diff --git a/analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/utils.py b/code/machine_learning/utils.py similarity index 100% rename from analysis/miRBind_CNN_retraining_optimized/hyperparam_optimization/utils.py rename to code/machine_learning/utils.py