<jupyter_start><jupyter_text>PYTORCH CNN ClassifierTo run this notebook on an another benchmark, use```papermill utils/torch_cnn_classifier.ipynb torch_cnn_experiments/[DATASET NAME].ipynb -p DATASET [DATASET NAME]```<jupyter_code># DATASET = 'no_dataset'
DATASET = 'demo_human_or_worm'
VERSION = 0
BATCH_SIZE = 32
EPOCHS = 10
ITER = 999
PATIENCE = 1
print(DATASET, VERSION, BATCH_SIZE, EPOCHS, ITER, PATIENCE)<jupyter_output>demo_human_or_worm 0 32 10 999 1<jupyter_text>Config<jupyter_code># must be imported first
from comet_ml import Experiment

import os
import numpy as np
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchtext.data.utils import get_tokenizer

from genomic_benchmarks.dataset_getters.pytorch_datasets import get_dataset
from genomic_benchmarks.models.torch import CNN
from genomic_benchmarks.dataset_getters.utils import coll_factory, LetterTokenizer, build_vocab, check_seq_lengths, check_config, VARIABLE_LENGTH_DATASETS
USE_PADDING = DATASET in VARIABLE_LENGTH_DATASETS
    
config = {
    "dataset": DATASET,
    "dataset_version": VERSION,
    "epochs": EPOCHS,
    "batch_size": BATCH_SIZE,
    "use_padding": USE_PADDING,
    "force_download": False,
    "run_on_gpu": True,
    "number_of_classes": 2,
    "embedding_dim": 100,
    "patience": PATIENCE
}
checkpoint_name = config["dataset"] + "_" + str(ITER) + "_" 
print(checkpoint_name)

# check_config(config)
experiment = Experiment(project_name="genomic-cnn", api_key="<KEY>")
experiment.log_parameters(config)
experiment.set_name(checkpoint_name + "patience:" + str(config["patience"]) + "_" + "epochs:" + str(config["epochs"]))<jupyter_output>COMET WARNING: As you are running in a Jupyter environment, you will need to call `experiment.end()` when finished to ensure all metrics and code are logged before exiting.
COMET INFO: Experiment is live on comet.ml https://www.comet.ml/davidcechak/genomic-cnn/fbb010b6be0b4c4585b3cca225a3ddca<jupyter_text>Choose the dataset<jupyter_code>import torch.utils.data as data

train_dset = get_dataset(config["dataset"], 'train')
t_size = int(len(train_dset)*0.8)
v_size = len(train_dset)-t_size

new_train_dset, valid_dset = data.random_split(train_dset, [t_size, v_size])<jupyter_output><empty_output><jupyter_text>Tokenizer and vocab<jupyter_code>tokenizer = get_tokenizer(LetterTokenizer())
vocabulary = build_vocab(train_dset, tokenizer, use_padding=config["use_padding"])

print("vocab len:" ,vocabulary.__len__())
print(vocabulary.get_stoi())<jupyter_output>vocab len: 9
{'<pad>': 8, 'T': 5, 'A': 4, 'C': 3, '<eos>': 6, 'G': 2, '<bos>': 1, 'N': 7, '<unk>': 0}<jupyter_text>Dataloader and batch preparation<jupyter_code># Run on GPU or CPU
device = 'cuda' if config["run_on_gpu"] and torch.cuda.is_available() else 'cpu'
print('Using {} device'.format(device))

max_seq_len, nn_input_len = check_seq_lengths(dataset=train_dset, config=config)

# Data Loader
if(config["use_padding"]):
    collate = coll_factory(vocabulary, tokenizer, device, pad_to_length = nn_input_len)
else:
    collate = coll_factory(vocabulary, tokenizer, device, pad_to_length = None)

train_loader = DataLoader(new_train_dset, batch_size=config["batch_size"], shuffle=True, collate_fn=collate)
valid_loader = DataLoader(valid_dset, batch_size=config["batch_size"], shuffle=True, collate_fn=collate)<jupyter_output>Using cuda device
max_seq_len  200
not all sequences are of the same length<jupyter_text>Model<jupyter_code>model = CNN(
    number_of_classes=config["number_of_classes"],
    vocab_size=vocabulary.__len__(),
    embedding_dim=config["embedding_dim"],
    input_len=nn_input_len
).to(device)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>model.train(
    train_loader, 
    valid_loader, 
    epochs=config["epochs"], 
    patience = config["patience"], 
    checkpoint_name = checkpoint_name, 
    experiment = experiment
)<jupyter_output>1.7976931348623157e+308
Epoch 0<jupyter_text>Testing<jupyter_code>test_dset = get_dataset(config["dataset"], 'test')
test_loader = DataLoader(test_dset, batch_size=config["batch_size"], shuffle=True, collate_fn=collate)

acc, f1 = model.test(test_loader, experiment = experiment)
acc, f1
experiment.end()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1172316384180791, 'fraction_numerical': 0.01765536723163842, 'mean_word_length': 7.380670611439842, 'pattern_counts': {'":': 10, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '271509', 'license': 'Apache-2.0', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1548, 'n_words': 303, 'path': 'cnn_experiments/experiments/utils/torch_cnn_classifier.ipynb', 'repo_name': 'ML-Bioinfo-CEITEC/paper_FineTuning_Transformers_For_Genomic_Tasks', 'size': '4268'}