<jupyter_start><jupyter_text>Multilabel Text Classification with Recurrent Neural Networks<jupyter_code>import numpy as np
import pandas as pd
import math
import os
import json
import ast
import random
from joblib import dump, load
import tensorflow as tf
import tensorflow_addons as tfa
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV
from bpmll import bp_mll_loss
import sklearn_json as skljson
from sklearn.model_selection import train_test_split
from sklearn import metrics
import matplotlib.pyplot as plt
import sys
os.chdir('C:\\Users\\rober\\OneDrive\\Documents\\Multilabel-Text-Classification\\Deep Learning Models\\RNN Models')  ## Set working directory
                                                                                                                      ## to be 'ANN Results'
sys.path.append('../../ThresholdFunctionLearning')    ## Append path to the ThresholdFunctionLearning directory to the interpreters
                                                   ## search path
from threshold_learning import predict_test_labels_binary    ## Import the 'predict_test_labels_binary()' function from the 
from threshold_learning import predict_labels_binary         ## threshold_learning library
sys.path.append('GridSearchAid_FFNetworks')
from FFNN_gridSearch_aid import SizeLayersPows2, createModel
## Load the seperabe PCs training and test data
npzfile = np.load("../../Data/seq_trainTest_data.npz")
X_train_padded = npzfile['train_padded']
X_test_padded = npzfile['test_padded']
Y_train = npzfile['Y_train'].astype('float64')
Y_test = npzfile['Y_test'].astype('float64')
num_unique_words = npzfile['num_unique_words']<jupyter_output><empty_output><jupyter_text>Cross Entropy Models -- Traditional ("Naive") Approach<jupyter_code>## Define the LSTM RNN architecture
num_labels = Y_train.shape[1]

model_biLSTM = tf.keras.models.Sequential([
    tf.keras.layers.Embedding(num_unique_words, 32, input_length = X_train_padded.shape[1]),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(16, return_sequences = False, return_state = False)),
    #tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(num_labels, activation = 'sigmoid')
])

optim = tf.keras.optimizers.Adam(lr=0.01)
#optim = tf.keras.optimizers.Adagrad(
#    learning_rate = 0.001, initial_accumulator_value = 0.1, epsilon = 1e-07,
#    name = 'Adagrad')

#optim = tf.keras.optimizers.RMSprop(learning_rate=0.001, rho=0.9, momentum = 0.8, epsilon=1e-07,)

metric = tfa.metrics.HammingLoss(mode = 'multilabel', threshold = 0.5)
model_biLSTM.compile(loss = 'binary_crossentropy', optimizer = optim, metrics = metric)
tf.random.set_seed(123)
model_biLSTM.fit(X_train_padded, Y_train, epochs = 30, validation_data = (X_test_padded, Y_test), verbose=2)
## (CAUTION: DO NOT OVERWRITE EXISTING FILES) -- Convert training history to dataframe and write to a .json file 
outfile = "Training Histories/history_ce_RNN_lr001.npz" 
#np.savez_compressed(outfile, history_biLSTM001 = history_biLSTM001)

#model_biLSTM.save_weights('Models/biLSTM_ce_001')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09706062461726883, 'fraction_numerical': 0.017146356399265157, 'mean_word_length': 4.490756302521008, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '48528', 'license': 'MIT', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1031, 'n_words': 233, 'path': 'Deep Learning Models/RNN Models/.ipynb_checkpoints/RNN_Models-checkpoint.ipynb', 'repo_name': 'BobbyLumpkin/Multilabel-Text-Classification', 'size': '3266'}