CoolFace
Apppublic

KavyaK/DL-Models

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
dnn_main.py104 linesDownload Raw Back to root
1import pandas as pd2import matplotlib.pyplot as plt3import seaborn as sns4from sklearn.model_selection import train_test_split5import tensorflow as tf6from sklearn.metrics import classification_report, confusion_matrix, accuracy_score7 8print("---------------------- Downloading Dataset -------------------------\n")9 10dataset = pd.read_csv("denv/Models/RNN/SMSSpamCollection.txt", sep='\t', names=['label', 'message'])11 12print("----------------------  -------------------------\n")13print(dataset.head())14print("----------------------  -------------------------")15print(dataset.groupby('label').describe())16print("----------------------  -------------------------")17dataset['label'] = dataset['label'].map({'spam': 1, 'ham': 0})18X = dataset['message'].values19y = dataset['label'].values20 21print("---------------------- Train Test Split -------------------------\n")22X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)23 24# Creating a tokenizer and fitting it on the training data25tokeniser = tf.keras.preprocessing.text.Tokenizer()26tokeniser.fit_on_texts(X_train)27 28# Converting text data to sequences using the trained tokenizer29encoded_train = tokeniser.texts_to_sequences(X_train)30encoded_test = tokeniser.texts_to_sequences(X_test)31 32print(encoded_train[0:2])33print("----------------------  Padding  -------------------------\n")34max_length = 1035padded_train = tf.keras.preprocessing.sequence.pad_sequences(encoded_train, maxlen=max_length, padding='post')36padded_test = tf.keras.preprocessing.sequence.pad_sequences(encoded_test, maxlen=max_length, padding='post')37print(padded_train[0:2])38 39print("----------------------  -------------------------\n")40 41vocab_size = len(tokeniser.word_index) + 142 43# Define the model44print("---------------------- Modelling -------------------------\n")45 46model = tf.keras.models.Sequential([47    tf.keras.layers.Embedding(input_dim=vocab_size, output_dim=24, input_length=max_length),48    tf.keras.layers.Flatten(),49    tf.keras.layers.Dense(64, activation='relu'),50    tf.keras.layers.Dense(32, activation='relu'),51    tf.keras.layers.Dense(1, activation='sigmoid')52])53 54# Compile the model55model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])56 57print("----------------------  -------------------------\n")58 59# Summarize the model60print(model.summary())61 62print("----------------------  -------------------------\n")63 64early_stop = tf.keras.callbacks.EarlyStopping(monitor='accuracy', mode='min', patience=10)65 66print("----------------------  Training -------------------------\n")67 68# Fit the model69model.fit(x=padded_train,70          y=y_train,71          epochs=50,72          validation_data=(padded_test, y_test),73          callbacks=[early_stop]74          )75 76print("----------------------  -------------------------\n")77 78 79def c_report(y_true, y_pred):80    print("Classification Report")81    print(classification_report(y_true, y_pred))82    acc_sc = accuracy_score(y_true, y_pred)83    print(f"Accuracy : {str(round(acc_sc, 2) * 100)}")84    return acc_sc85 86 87def plot_confusion_matrix(y_true, y_pred):88    mtx = confusion_matrix(y_true, y_pred)89    sns.heatmap(mtx, annot=True, fmt='d', linewidths=.5, cmap="Blues", cbar=False)90    plt.ylabel('True label')91    plt.xlabel('Predicted label')92    plt.savefig("denv/Models/RNN/results/test.jpg")93 94 95preds = (model.predict(padded_test) > 0.5).astype("int32")96 97c_report(y_test, preds)98plot_confusion_matrix(y_test, preds)99 100# Save the model101model.save('DNN.h5')102 103 104