<jupyter_start><jupyter_text>How To Train CNN Classifier With TFThis notebook demonstrates how to use `genomic_benchmarks` to train a neural network classifier on one of its benchmark datasets [human_nontata_promoters](https://github.com/ML-Bioinfo-CEITEC/genomic_benchmarks/tree/main/docs/human_nontata_promoters).<jupyter_code>%load_ext autoreload
%autoreload 2<jupyter_output><empty_output><jupyter_text>Data downloadWith the function `download_dataset` downloads, we can download full-sequence form of the benchmark, splitted into train and test sets, one folder for each class.<jupyter_code>from pathlib import Path
import tensorflow as tf

from genomic_benchmarks.loc2seq import download_dataset
from genomic_benchmarks.data_check import is_downloaded, info
from genomic_benchmarks.models.tf import vectorize_layer
from genomic_benchmarks.models.tf import basic_cnn_model_v0 as model

if not is_downloaded('human_nontata_promoters'):
    download_dataset('human_nontata_promoters')
info('human_nontata_promoters', 0)<jupyter_output>Dataset `human_nontata_promoters` has 2 classes: negative, positive.

All lenghts of genomic intervals equals 251.

Totally 36131 sequences have been found, 27097 for training and 9034 for testing.<jupyter_text>TF Dataset objectTo train the model with TensorFlow, we must create a TF Dataset. Because the directory structure of our benchmarks is ready for training, we can just call `tf.keras.preprocessing.text_dataset_from_directory` function as follows.<jupyter_code>BATCH_SIZE = 64
SEQ_PATH = Path.home() / '.genomic_benchmarks' / 'human_nontata_promoters'
CLASSES = [x.stem for x in (SEQ_PATH/'train').iterdir() if x.is_dir()]

train_dset = tf.keras.preprocessing.text_dataset_from_directory(
    SEQ_PATH / 'train',
    batch_size=BATCH_SIZE,
    class_names=CLASSES)<jupyter_output>Found 27097 files belonging to 2 classes.<jupyter_text>Text vectorizationTo convert the strings to tensors, we internally use TF `TextVectorization` layer and splitting to characters.<jupyter_code>vectorize_layer.adapt(train_dset.map(lambda x, y: x))
#vectorize_layer.set_vocabulary(vocab=np.asarray(['a', 'c', 't', 'g', 'n']))
vectorize_layer.get_vocabulary()
def vectorize_text(text, label):
  text = tf.expand_dims(text, -1)
  return vectorize_layer(text)-2, label

train_ds = train_dset.map(vectorize_text)<jupyter_output><empty_output><jupyter_text>Model trainingTo get a baseline (other models can be compared to) we ship a package with [a simple CNN model](https://github.com/ML-Bioinfo-CEITEC/genomic_benchmarks/blob/main/src/genomic_benchmarks/models/tf.py). We have vectorized the dataset before training the model to speed up the process.<jupyter_code>model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              optimizer='adam',
              metrics=tf.metrics.BinaryAccuracy(threshold=0.0))
EPOCHS = 10

history = model.fit(
    train_ds,
    epochs=EPOCHS)<jupyter_output>Epoch 1/10
424/424 [==============================] - 7s 16ms/step - loss: 0.4556 - binary_accuracy: 0.7943
Epoch 2/10
424/424 [==============================] - 7s 17ms/step - loss: 0.3847 - binary_accuracy: 0.8292
Epoch 3/10
424/424 [==============================] - 7s 17ms/step - loss: 0.3633 - binary_accuracy: 0.8388
Epoch 4/10
424/424 [==============================] - 7s 17ms/step - loss: 0.3444 - binary_accuracy: 0.8463
Epoch 5/10
424/424 [==============================] - 8s 19ms/step - loss: 0.3264 - binary_accuracy: 0.8572
Epoch 6/10
424/424 [==============================] - 8s 20ms/step - loss: 0.3108 - binary_accuracy: 0.8642
Epoch 7/10
424/424 [==============================] - 8s 20ms/step - loss: 0.2954 - binary_accuracy: 0.8721
Epoch 8/10
424/424 [==============================] - 8s 19ms/step - loss: 0.2851 - binary_accuracy: 0.8784
Epoch 9/10
424/424 [==============================] - 8s 19ms/step - loss: 0.2741 - binary_accuracy: 0.8843
Epoch 10/10
424/424 [=======[...]<jupyter_text>Evaluation on the test setFinally, we can do the same pre-processing for the test set and evaluate the accuracy of our model.<jupyter_code>test_dset = tf.keras.preprocessing.text_dataset_from_directory(
    SEQ_PATH / 'test',
    batch_size=BATCH_SIZE,
    class_names=CLASSES)

test_ds =  test_dset.map(vectorize_text)
model.evaluate(test_ds)<jupyter_output>142/142 [==============================] - 1s 5ms/step - loss: 0.3401 - binary_accuracy: 0.8493
{'fraction_non_alphanumeric': 0.16198459447213412, 'fraction_numerical': 0.05935659265971908, 'mean_word_length': 5.669184290030212, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '229636', 'license': 'Apache-2.0', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1437, 'n_words': 364, 'path': 'notebooks/How_To_Train_CNN_Classifier_With_TF.ipynb', 'repo_name': 'Shirapti-nath/genomic_benchmarks', 'size': '4414'}