<jupyter_start><jupyter_text>Importing Libraries<jupyter_code>from __future__ import absolute_import, division, print_function, unicode_literals
%tensorflow_version 2.x
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pathlib
import os
import pandas as pd
import seaborn as sns
import sklearn.metrics as metrics
tf.__version__<jupyter_output><empty_output><jupyter_text>Mounting Google Drive with the notebook<jupyter_code>from google.colab import drive
drive.mount('/content/gdrive', force_remount=True)
root_dir = "/content/gdrive/My Drive/"
base_dir = root_dir + 'DeepLearning/MUSK/'<jupyter_output>Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly

Enter your authorization code:
··········
Mounted at /content/gdrive<jupyter_text>Loading the Data in a DataFrame<jupyter_code>data_dir = pathlib.Path(base_dir)
os.path.exists(data_dir)
for name in list(data_dir.glob('*'))[:]:
  print(name)
data = pd.read_csv(root_dir + 'DeepLearning/MUSK/musk_csv.csv')<jupyter_output><empty_output><jupyter_text>Data Exploration & Preprocessing<jupyter_code>data.head()
data.tail()
data.shape
y = data['class']
x = data.drop(['class','ID','molecule_name','conformation_name'],axis=1)
data['conformation_name'].nunique()
x.info()
sns.countplot(x='class',data=data)
plt.title("Classes count")
from sklearn.model_selection import train_test_split
np.random.seed(5)

X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=5)
sns.countplot(x=y_train)
plt.title("Classes count")
sns.countplot(x=y_test)
plt.title("Classes count")
x.describe()<jupyter_output><empty_output><jupyter_text>Model Creation<jupyter_code>model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Dense(64,input_shape=(x.shape[1],),use_bias=False))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Activation('relu'))

model.add(tf.keras.layers.Dense(64,use_bias=False))
# model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Activation('relu'))
# model.add(tf.keras.layers.Dense(64,activation='relu',input_shape=(x.shape[1],),kernel_regularizer=tf.keras.regularizers.l2(l=0.001)))
# model.add(tf.keras.layers.Dense(64,activation='relu',kernel_regularizer=tf.keras.regularizers.l2(l=0.001)))
# model.add(tf.keras.layers.Dense(64,activation='relu',kernel_regularizer=tf.keras.regularizers.l2(l=0.001)))

model.add(tf.keras.layers.Dense(1,activation='sigmoid'))
model.summary()
model.compile(optimizer=tf.keras.optimizers.Adam(),
              loss='binary_crossentropy',
              metrics=['accuracy'])
# earlyStopping = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', min_delta=0.0001, patience=6)
LRR = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_accuracy', patience=3, verbose=1, factor=0.5, min_lr=0.0001)<jupyter_output><empty_output><jupyter_text>Model Training<jupyter_code>history = model.fit(X_train,y_train,batch_size=32,epochs=100,validation_data=(X_test,y_test))
# history = model.fit(X_train,y_train,batch_size=32,epochs=100,validation_data=(X_test,y_test),callbacks=[LRR])
history1 = model.fit(X_train,y_train,batch_size=32,epochs=100,validation_data=(X_test,y_test))
def displayHistory(history,name):
  acc = history.history['accuracy']
  val_acc = history.history['val_accuracy']

  loss = history.history['loss']
  val_loss = history.history['val_loss']

  plt.figure(figsize=(8, 8))
  plt.subplot(2, 1, 1)
  plt.plot(acc, label='Training Accuracy')
  plt.plot(val_acc, label='Validation Accuracy')
  plt.legend(loc='lower right')
  plt.ylabel('Accuracy')
  plt.ylim([min(plt.ylim()),1])
  plt.title('Training and Validation Accuracy')

  plt.subplot(2, 1, 2)
  plt.plot(loss, label='Training Loss')
  plt.plot(val_loss, label='Validation Loss')
  plt.legend(loc='upper right')
  plt.ylabel('loss')
  plt.ylim([0,2.0])
  plt.title('Training and Validation Loss')
  plt.xlabel('epoch')
  plt.savefig(name)
  plt.show()
 

displayHistory(history,root_dir+'DeepLearning/MUSK/Models/model10.png')<jupyter_output><empty_output><jupyter_text>Evaluation<jupyter_code>def calculateScores(model):
  # predict probabilities for test set
  yhat_probs = model.predict(X_test, verbose=0)
  # predict crisp classes for test set
  yhat_classes = model.predict_classes(X_test, verbose=0)

  # reduce to 1d array
  yhat_probs = yhat_probs[:, 0]
  yhat_classes = yhat_classes[:, 0]

  # accuracy: (tp + tn) / (p + n)
  accuracy = metrics.accuracy_score(y_test, yhat_classes)
  print('Accuracy: %f' % accuracy)
  # precision tp / (tp + fp)
  precision = metrics.precision_score(y_test, yhat_classes)
  print('Precision: %f' % precision)
  # recall: tp / (tp + fn)
  recall = metrics.recall_score(y_test, yhat_classes)
  print('Recall: %f' % recall)
  # f1: 2 tp / (2 tp + fp + fn)
  f1 = metrics.f1_score(y_test, yhat_classes)
  print('F1 score: %f' % f1)
calculateScores(model)
model.save(root_dir + 'DeepLearning/MUSK/Models/model10.h5')
model.load_weights(root_dir + 'DeepLearning/MUSK/Models/model10.h5')<jupyter_output><empty_output><jupyter_text>Fine Tuning<jupyter_code># model.compile(optimizer=tf.keras.optimizers.Adam(),
#               loss='binary_crossentropy',
#               metrics=['accuracy'])

class myCallback(tf.keras.callbacks.Callback): 
  def on_epoch_end(self, epoch, logs={}): 
    if logs.get('val_accuracy') == 1.0:   
      print("\nReached at Validation accuracy of 1.0000")   
      self.model.stop_training = True

LRR = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_accuracy', patience=3, verbose=1, factor=0.5, min_lr=0.0001)
history2 = model.fit(X_train,y_train,batch_size=32,epochs=150,validation_data=(X_test,y_test),initial_epoch=100,callbacks=[LRR,myCallback()])
displayHistory(history2,root_dir+'DeepLearning/MUSK/Models/model10finetune.png')
calculateScores(model)
model.save(root_dir + 'DeepLearning/MUSK/Models/model10finetuned.h5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1492797213867342, 'fraction_numerical': 0.029760962482190914, 'mean_word_length': 7.19455252918288, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 1, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '499827', 'license': 'MIT', 'n_tokens_mistral': 2543, 'n_tokens_neox': 2408, 'n_words': 311, 'path': 'Deep Learning/MUSK & NON-MUSK Compounds Classification/MUSK.ipynb', 'repo_name': 'memr5/Machine-Learning-Portfolio', 'size': '6317'}