# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (system-wide)
#     language: python
#     name: python3
# ---

# + id="_vlECuRq_4hm"
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split

# + colab={"base_uri": "https://localhost:8080/", "height": 226} id="x4hC1JeX_4hr" outputId="3f523e73-aa11-4e4b-ee69-ae05732942ec"
dataset = pd.read_csv('/content/sample_data/exoplanets_2018.csv') 
#dataset = dataset.rename(columns=header_list)
dataset.head()

# + id="RG46y94I_4hv"
pd.set_option("display.max_columns", None)

# + id="zptX-WgS_4hx"
dataset=dataset[['koi_disposition','koi_fpflag_nt','koi_fpflag_ss','koi_fpflag_co','koi_fpflag_ec','koi_depth', 'koi_model_snr']]

# + colab={"base_uri": "https://localhost:8080/"} id="PPLMQuHx_4hy" outputId="17a1412e-184d-4e9b-c288-62f1fb6d5e57"
for col in dataset.columns:
    print(col)

# + id="8A_53nBl_4hz" colab={"base_uri": "https://localhost:8080/"} outputId="c8cae344-b871-4752-fbeb-8b997075f64d"
dataset["koi_disposition"].replace({"CONFIRMED": "1", "CANDIDATE": "0", "FALSE POSITIVE": "0"}, inplace=True)
dataset.isnull().sum().sum()
dataset=dataset.dropna()  
dataset["koi_disposition"].value_counts()

# + colab={"base_uri": "https://localhost:8080/", "height": 206} id="99qyLE1w_4h0" outputId="4d849742-fae0-463b-fcba-39c469939050"
dataset.head()

# + colab={"base_uri": "https://localhost:8080/"} id="ivSf2KaX_4h2" outputId="9814edb0-d36d-4259-dde9-8fe380fb5a80"
dataset = dataset.to_numpy()

# Sepparating the label (Y) from the input features (X)
Y = dataset[:, 0]
Y = np.array(Y, dtype=int)
X = dataset[:, 1:]

# Sanity check
print(X.shape, Y.shape)

# + id="FhxV-lls_4h3" cellView="form"
#@title Hidden
# Separate data with label 0 and label 1

x_0 = X[Y == 0, :]
x_1 = X[Y == 1, :]
y_0 = Y[Y==0]
y_1 = Y[Y==1]

# Sanity check
print(x_0.shape, y_0.shape)
print(x_1.shape, y_1.shape)

# + id="CCLRDtvg_4h5" colab={"base_uri": "https://localhost:8080/"} outputId="b7898e49-f8e0-4b40-b433-4e04dccf7c27"
# Split 70% of the data for training set and 30% for testing set

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=2001, shuffle=True, stratify=Y)
# x_train_1, x_test_1, y_train_1, y_test_1 = train_test_split(x_1, y_1, test_size=0.3, random_state=2001,shuffle=True, stratify=Y)
print(X_train.shape, Y_train.shape)
print(X_test.shape, Y_test.shape)

# + id="NAoYVInq_4h6" cellView="form"
#@title Hidden
num_sample = 1500 # sample per class, total = 5000

# Take the first 5000 samples (1500 from each class) from training set for X_train and Y_train
X_train = np.concatenate((x_train_0[:num_sample, :], x_train_1[:num_sample, :]), axis=0)
Y_train = np.concatenate((y_train_0[:num_sample], y_train_1[:num_sample]), axis=0)

# Take the first 5000 samples (1500 from each class) from testing set for X_test and Y_test
X_test = np.concatenate((x_test_0[:num_sample, :], x_test_1[:num_sample, :]), axis=0)
Y_test = np.concatenate((y_test_0[:num_sample], y_test_1[:num_sample]), axis=0)

# Manual Check 
print(X_train.shape, Y_train.shape)
print(X_test.shape, Y_test.shape)

# + id="9deOUOq8_4h6"
#Save extracted datasets on the disk
# #!mkdir 'KeplerDataset'

np.savetxt('/content/sample_data/X_train_6694.txt', X_train)
np.savetxt('/content/sample_data/X_test_2870.txt', X_test)
np.savetxt('/content/sample_data/Y_train_6694.txt', Y_train)
np.savetxt('/content/sample_data/Y_test_2870.txt', Y_test)

# + id="iYkuN1MK_4h7" colab={"base_uri": "https://localhost:8080/"} cellView="code" outputId="e5f2a6e7-60b5-442e-ddde-c7d033754d54"
#@title Hidden
# Code to load the saved subset


X_train = np.loadtxt('/content/sample_data/X_train_6694.txt')
Y_train = np.loadtxt('/content/sample_data/Y_train_6694.txt')

X_test = np.loadtxt('/content/sample_data/X_test_2870.txt')
Y_test = np.loadtxt('/content/sample_data/Y_test_2870.txt')

# Check
print(X_train.shape, Y_train.shape)
print(X_test.shape, Y_test.shape)

# + id="9ICKFYk4_4h7"
#Training the Quantum Circuit in Simulator
#pip install pennylane
import pennylane as qml
from pennylane import numpy as np
from pennylane.optimize import AdamOptimizer

import tensorflow as tf
from tensorflow.keras.utils import to_categorical


# + id="3N5YVrYeKeHP"
# Set a random seed
np.random.seed(42)

# + id="zgGsQMKo_4h8"
# Define output labels as quantum state vectors
label_0 = [[1], [0]]
label_1 = [[0], [1]]


def density_matrix(state):
    """Calculates the density matrix representation of a state.

    Args:
        state (array[complex]): array representing a quantum state vector

    Returns:
        dm: (array[complex]): array representing the density matrix
    """
    return np.outer(state,  np.conj(state))

state_labels = [label_0, label_1]
dm_labels = [density_matrix(state_labels[i]) for i in range(2)]

# + id="aaqPRUy3_4h8"
from keras import backend as K


# Alpha Custom Layer
class class_weights(tf.keras.layers.Layer):
    def __init__(self):
        super(class_weights, self).__init__()
        w_init = tf.random_normal_initializer()
        self.w = tf.Variable(
            initial_value=w_init(shape=(1, 2), dtype="float32"),
            trainable=True,
        )

    def call(self, inputs):
        return (inputs * self.w)


# + id="-carE_ToZcs8"
from keras import backend as K


# Alpha Custom Layer
class class_weights(tf.keras.layers.Layer):
    def __init__(self):
        super(class_weights, self).__init__()
        w_init = tf.random_normal_initializer()
        self.w = tf.Variable(
            initial_value=w_init(shape=(1, 2), dtype="float32"),
            trainable=True,
        )

    def call(self, inputs):
        return (inputs * self.w)


# + colab={"base_uri": "https://localhost:8080/"} id="mUBE653l0Yjm" outputId="d56af406-d9b8-404b-bf17-b81aa2c6acbf"
num_fc_layer = 5 # number of layer
params_fix = np.random.uniform(size=(2, num_fc_layer, 6))
print(params_fix)

# + id="X3Ln2XAj0Yyq"
n_qubits = 2  # number of class
dev_fc = qml.device("default.qubit", wires=n_qubits)
layer_id = 0 # the layer index to be trained, it starts from zero because of Numpy convention

@qml.qnode(dev_fc)
def q_fc(params, inputs):
    """A variational quantum circuit representing the DRC.

    Args:
        params (array[float]): array of parameters
        inputs (array[float]): 1-d input vector (data sample)

    Returns:
        array[float]: 1-d output vector in the form of [alpha0*<O0>, alpha1*<O1>]
    """
    
    # layer iteration
    for l in range(len(params_fix[0])):
        # qubit iteration
        for q in range(n_qubits):
            # gate iteration
            for g in range(int(len(inputs)/3)):
              if l == layer_id: # train only the specified layer
                qml.Rot(*(params[0][3*g:3*(g+1)] * inputs[3*g:3*(g+1)] + params[1][3*g:3*(g+1)]), wires=q)
              else:
                qml.Rot(*(params_fix[0][l][3*g:3*(g+1)] * inputs[3*g:3*(g+1)] + params_fix[1][l][3*g:3*(g+1)]), wires=q)
    
    return [qml.expval(qml.Hermitian(dm_labels[i], wires=[i])) for i in range(n_qubits)]


# + id="1dpf-rjN0hyf"
n_component = 6 # number of features used

X = tf.keras.Input(shape=(n_component,), name='Input_Layer')


# Quantum Layer
q_fc_layer = qml.qnn.KerasLayer(q_fc, {"params": (2, n_component)}, output_dim=2)(X)

# Alpha Layer
alpha_layer = class_weights()(q_fc_layer)

model = tf.keras.Model(inputs=X, outputs=alpha_layer)

# + id="1RB5piW-0h8R"
opt = tf.keras.optimizers.Adam(learning_rate=0.05)
model.compile(opt, loss='mse', metrics=["accuracy"])

# + id="uPGrIIv60iIB"
filepath = "/content/sample_data/Best6_layer5(layer_id=0)_set1000_8epoch_saved-model-{epoch:02d}.hdf5"
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath, monitor='val_acc', verbose=1,
                                                save_weights_only=True, save_best_only=False, mode='auto')

# + colab={"base_uri": "https://localhost:8080/"} id="vU8-Kr6V050y" outputId="7cbb1c2e-9c99-4327-846d-7413d5569f89"
H = model.fit(X_train, to_categorical(Y_train), epochs=1, batch_size=256, initial_epoch=0,
              validation_data=(X_test, to_categorical(Y_test)), verbose=1,
              callbacks=[checkpoint])

# + colab={"base_uri": "https://localhost:8080/"} id="vM8ixM-aJmxz" outputId="b1ea6693-af61-4a6f-ff1e-92cfa3f8e2e5"
# !pwd
# !ls /content/sample_data/Best

# + id="yBjtr3Gt06BF"
model.load_weights('/content/sample_data/Best6_layer5(layer_id=0)_set1000_8epoch_saved-model-03.hdf5')

# keep that parameters and replace the initial one
params_layer_0 = model.get_weights()[0]
params_fix[:, 0, :] = params_layer_0

# + [markdown] id="S8D2OrBLJJvI"
# **2nd Layer**

# + id="PRMuEmwjI78q"
n_qubits = 2  # number of class
dev_fc = qml.device("default.qubit", wires=n_qubits)
layer_id = 1 # the layer index to be trained, it starts from zero because of Numpy convention

@qml.qnode(dev_fc)
def q_fc(params, inputs):
    """A variational quantum circuit representing the DRC.

    Args:
        params (array[float]): array of parameters
        inputs (array[float]): 1-d input vector (data sample)

    Returns:
        array[float]: 1-d output vector in the form of [alpha0*<O0>, alpha1*<O1>]
    """
    
    # layer iteration
    for l in range(len(params_fix[0])):
        # qubit iteration
        for q in range(n_qubits):
            # gate iteration
            for g in range(int(len(inputs)/3)):
              if l == layer_id: # train only the specified layer
                qml.Rot(*(params[0][3*g:3*(g+1)] * inputs[3*g:3*(g+1)] + params[1][3*g:3*(g+1)]), wires=q)
              else:
                qml.Rot(*(params_fix[0][l][3*g:3*(g+1)] * inputs[3*g:3*(g+1)] + params_fix[1][l][3*g:3*(g+1)]), wires=q)
    
    return [qml.expval(qml.Hermitian(dm_labels[i], wires=[i])) for i in range(n_qubits)]


# + id="Jcl4-trtJdrq"
n_component = 6 # number of features used

X = tf.keras.Input(shape=(n_component,), name='Input_Layer')


# Quantum Layer
q_fc_layer = qml.qnn.KerasLayer(q_fc, {"params": (2, n_component)}, output_dim=2)(X)

# Alpha Layer
alpha_layer = class_weights()(q_fc_layer)

model = tf.keras.Model(inputs=X, outputs=alpha_layer)

# + id="NIEhpBJaJd3U"
opt = tf.keras.optimizers.Adam(learning_rate=0.05)
model.compile(opt, loss='mse', metrics=["accuracy"])

# + id="1rj2LIT-Jd6o"
# Note: please put the correct directory to save the weights accordingly if you want to run the code
filepath = "/content/sample_data/Best6_layer5(layer_id=1)_3epoch_saved-model-{epoch:02d}.hdf5"
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath, monitor='val_acc', verbose=1,
                                                save_weights_only=True, save_best_only=False, mode='auto')

# + id="4sDPQDQqJd-a" colab={"base_uri": "https://localhost:8080/"} outputId="b903f9a1-1ae4-4497-e4f1-e81cf6944177"
H = model.fit(X_train, to_categorical(Y_train), epochs=1, batch_size=256, initial_epoch=0,
              validation_data=(X_test, to_categorical(Y_test)), verbose=1,
              callbacks=[checkpoint])

# + id="BnpMQPejJeB9"
# load the parameters that is considered giving the best balance between train and test accuracy
model.load_weights('/content/sample_data/Best6_layer5(layer_id=1)_3epoch_saved-model-02.hdf5')


# keep that parameters and replace the initial one
params_layer_1 = model.get_weights()[0]
params_fix[:, 1, :] = params_layer_1

# + [markdown] id="3IfcAMhiJr8n"
# **3rd Layer**

# + id="zkiS9SqUJeJR"
n_qubits = 2  # number of class
dev_fc = qml.device("default.qubit", wires=n_qubits)
layer_id = 2 # the layer index to be trained, it starts from zero because of Numpy convention

@qml.qnode(dev_fc)
def q_fc(params, inputs):
    """A variational quantum circuit representing the DRC.

    Args:
        params (array[float]): array of parameters
        inputs (array[float]): 1-d input vector (data sample)

    Returns:
        array[float]: 1-d output vector in the form of [alpha0*<O0>, alpha1*<O1>]
    """
    
    # layer iteration
    for l in range(len(params_fix[0])):
        # qubit iteration
        for q in range(n_qubits):
            # gate iteration
            for g in range(int(len(inputs)/3)):
              if l == layer_id: # train only the specified layer
                qml.Rot(*(params[0][3*g:3*(g+1)] * inputs[3*g:3*(g+1)] + params[1][3*g:3*(g+1)]), wires=q)
              else:
                qml.Rot(*(params_fix[0][l][3*g:3*(g+1)] * inputs[3*g:3*(g+1)] + params_fix[1][l][3*g:3*(g+1)]), wires=q)
    
    return [qml.expval(qml.Hermitian(dm_labels[i], wires=[i])) for i in range(n_qubits)]


# + id="1C8tPRqtJeMD"
n_component = 6 # number of features used

X = tf.keras.Input(shape=(n_component,), name='Input_Layer')


# Quantum Layer
q_fc_layer = qml.qnn.KerasLayer(q_fc, {"params": (2, n_component)}, output_dim=2)(X)

# Alpha Layer
alpha_layer = class_weights()(q_fc_layer)

model = tf.keras.Model(inputs=X, outputs=alpha_layer)

# + id="T32BcvXgJePB"
opt = tf.keras.optimizers.Adam(learning_rate=0.05)
model.compile(opt, loss='mse', metrics=["accuracy"])

# + id="xjeKolFYJeSJ"
# Note: please put the correct directory to save the weights accordingly if you want to run the code
filepath = "/content/sample_data/Best6_layer5(layer_id=2)_3epoch_saved-model-{epoch:02d}.hdf5"
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath, monitor='val_acc', verbose=1,
                                                save_weights_only=True, save_best_only=False, mode='auto')

# + id="SAm0dk1gJeVL" colab={"base_uri": "https://localhost:8080/"} outputId="f39e8f33-6ecf-46f5-cc7a-6f20651b1a31"
H = model.fit(X_train, to_categorical(Y_train), epochs=3, batch_size=256, initial_epoch=0,
              validation_data=(X_test, to_categorical(Y_test)), verbose=1,
              callbacks=[checkpoint])

# + id="w2zUR-P6JeYj"
# load the parameters that is considered giving the best balance between train and test accuracy
model.load_weights('./Model/Best6_layer5(layer_id=2)_set5000_10epoch_saved-model-08.hdf5')


# keep that parameters and replace the initial one
params_layer_2 = model.get_weights()[0]
params_fix[:, 2, :] = params_layer_2

# + [markdown] id="X_RxcJ8rJ-8g"
# **4th Layer**

# + id="QjH0FSD-Jeb0"
n_qubits = 2  # number of class
dev_fc = qml.device("default.qubit", wires=n_qubits)
layer_id = 3 # the layer index to be trained, it starts from zero because of Numpy convention

@qml.qnode(dev_fc)
def q_fc(params, inputs):
    """A variational quantum circuit representing the DRC.

    Args:
        params (array[float]): array of parameters
        inputs (array[float]): 1-d input vector (data sample)

    Returns:
        array[float]: 1-d output vector in the form of [alpha0*<O0>, alpha1*<O1>]
    """
    
    # layer iteration
    for l in range(len(params_fix[0])):
        # qubit iteration
        for q in range(n_qubits):
            # gate iteration
            for g in range(int(len(inputs)/3)):
              if l == layer_id: # train only the specified layer
                qml.Rot(*(params[0][3*g:3*(g+1)] * inputs[3*g:3*(g+1)] + params[1][3*g:3*(g+1)]), wires=q)
              else:
                qml.Rot(*(params_fix[0][l][3*g:3*(g+1)] * inputs[3*g:3*(g+1)] + params_fix[1][l][3*g:3*(g+1)]), wires=q)
    
    return [qml.expval(qml.Hermitian(dm_labels[i], wires=[i])) for i in range(n_qubits)]


# + id="Y0mlC1teKGdK"
n_component = 6 # number of features used

X = tf.keras.Input(shape=(n_component,), name='Input_Layer')


# Quantum Layer
q_fc_layer = qml.qnn.KerasLayer(q_fc, {"params": (2, n_component)}, output_dim=2)(X)

# Alpha Layer
alpha_layer = class_weights()(q_fc_layer)

model = tf.keras.Model(inputs=X, outputs=alpha_layer)

# + id="InoTB0x2KGpD"
opt = tf.keras.optimizers.Adam(learning_rate=0.05)
model.compile(opt, loss='mse', metrics=["accuracy"])

# + id="GZ3QYe55KGv2"
# Note: please put the correct directory to save the weights accordingly if you want to run the code
filepath = "./Model/Best6_layer5(layer_id=3)_set5000_10epoch_saved-model-{epoch:02d}.hdf5"
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath, monitor='val_acc', verbose=1,
                                                save_weights_only=True, save_best_only=False, mode='auto')

# + id="xRUvb7oYKGxs"
H = model.fit(X_train, to_categorical(Y_train), epochs=10, batch_size=128, initial_epoch=0,
              validation_data=(X_test, to_categorical(Y_test)), verbose=1,
              callbacks=[checkpoint])

# + id="wyzoJqVMKG0M"
# load the parameters that is considered giving the best balance between train and test accuracy
model.load_weights('./Model/Best6_layer5(layer_id=3)_set5000_10epoch_saved-model-08.hdf5')


# keep that parameters and replace the initial one
params_layer_3 = model.get_weights()[0]
params_fix[:, 3, :] = params_layer_3



# + [markdown] id="dM1OX1lTKXKU"
# **5th Layer**

# + id="4C2thMTSKG2a"
n_qubits = 2  # number of class
dev_fc = qml.device("default.qubit", wires=n_qubits)
layer_id = 4 # the layer index to be trained, it starts from zero because of Numpy convention

@qml.qnode(dev_fc)
def q_fc(params, inputs):
    """A variational quantum circuit representing the DRC.

    Args:
        params (array[float]): array of parameters
        inputs (array[float]): 1-d input vector (data sample)

    Returns:
        array[float]: 1-d output vector in the form of [alpha0*<O0>, alpha1*<O1>]
    """
    
    # layer iteration
    for l in range(len(params_fix[0])):
        # qubit iteration
        for q in range(n_qubits):
            # gate iteration
            for g in range(int(len(inputs)/3)):
              if l == layer_id: # train only the specified layer
                qml.Rot(*(params[0][3*g:3*(g+1)] * inputs[3*g:3*(g+1)] + params[1][3*g:3*(g+1)]), wires=q)
              else:
                qml.Rot(*(params_fix[0][l][3*g:3*(g+1)] * inputs[3*g:3*(g+1)] + params_fix[1][l][3*g:3*(g+1)]), wires=q)
    
    return [qml.expval(qml.Hermitian(dm_labels[i], wires=[i])) for i in range(n_qubits)]


# + id="TBx6jcxAKG4t"
n_component = 6 # number of features used

X = tf.keras.Input(shape=(n_component,), name='Input_Layer')


# Quantum Layer
q_fc_layer = qml.qnn.KerasLayer(q_fc, {"params": (2, n_component)}, output_dim=2)(X)

# Alpha Layer
alpha_layer = class_weights()(q_fc_layer)

model = tf.keras.Model(inputs=X, outputs=alpha_layer)

# + id="LFrr5qt5KG74"
opt = tf.keras.optimizers.Adam(learning_rate=0.05)
model.compile(opt, loss='mse', metrics=["accuracy"])

# + id="JlGzjF7EKG_W"
# Note: please put the correct directory to save the weights accordingly if you want to run the code
filepath = "./Model/Best6_layer5(layer_id=4)_set5000_10epoch_saved-model-{epoch:02d}.hdf5"
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath, monitor='val_acc', verbose=1,
                                                save_weights_only=True, save_best_only=False, mode='auto')

# + id="iuyfflQDKHN_"
H = model.fit(X_train, to_categorical(Y_train), epochs=10, batch_size=128, initial_epoch=0,
              validation_data=(X_test, to_categorical(Y_test)), verbose=1,
              callbacks=[checkpoint])

# + id="vadhmtfUKHRP"
# load the parameters that is considered giving the best balance between train and test accuracy
model.load_weights('./Model/Best6_layer5(layer_id=4)_set5000_10epoch_saved-model-08.hdf5')


# keep that parameters and replace the initial one
params_layer_4 = model.get_weights()[0]
params_fix[:, 4, :] = params_layer_4

# + [markdown] id="cPynKdGdKuhd"
# **Final Training**

# + id="QZkLkl31KHUH"
n_qubits = 2  # number of class
dev_fc = qml.device("default.qubit", wires=n_qubits)

@qml.qnode(dev_fc)
def q_fc(params, inputs):
    """A variational quantum circuit representing the DRC.

    Args:
        params (array[float]): array of parameters
        inputs (array[float]): 1-d input vector (data sample)

    Returns:
        array[float]: 1-d output vector in the form of [alpha0*<O0>, alpha1*<O1>]
    """
    
    # layer iteration
    for l in range(len(params[0])):
        # qubit iteration
        for q in range(n_qubits):
            # gate iteration
            for g in range(int(len(inputs)/3)):
              qml.Rot(*(params[0][l][3*g:3*(g+1)] * inputs[3*g:3*(g+1)] + params[1][l][3*g:3*(g+1)]), wires=q)
    
    return [qml.expval(qml.Hermitian(dm_labels[i], wires=[i])) for i in range(n_qubits)]


# + id="eOYdQKhaKzNl"
n_component = 6

X = tf.keras.Input(shape=(n_component,), name='Input_Layer')


# Quantum Layer
num_fc_layer = 5
q_fc_layer = qml.qnn.KerasLayer(q_fc, {"params": (2, num_fc_layer, n_component)}, output_dim=2)(X)

# Alpha Layer
alpha_layer = class_weights()(q_fc_layer)

model = tf.keras.Model(inputs=X, outputs=alpha_layer)

# + id="hEe9DbG4KzQp"
# view each of the layer's name and its specifications
model.summary()

# + id="7t5UOSPcKzTx"
model(X_train[0:3])

# set the initial weights to the params_fix that has been optimized layer by layer
# Note: please change the name of the keras layer accordingly based on the model.summary()
model.get_layer('keras_layer').set_weights([params_fix])

# + id="VrFbdrrZKzWy"
opt = tf.keras.optimizers.Adam(learning_rate=0.05)
model.compile(opt, loss='mse', metrics=["accuracy"])

# + id="Vx1gDFVFKzZ7"
# Note: please put the correct directory to save the weights accordingly if you want to run the code
filepath = "./Model/Best6_layer5(layer_id=all)_set5000_10epoch_saved-model-{epoch:02d}.hdf5"
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath, monitor='val_acc', verbose=1,
                                                save_weights_only=True, save_best_only=False, mode='auto')

# + id="hja6mSU6Kzbv"
H = model.fit(X_train, to_categorical(Y_train), epochs=10, batch_size=128, initial_epoch=0,
              validation_data=(X_test, to_categorical(Y_test)), verbose=1,
              callbacks=[checkpoint])

# + id="7Q8_pMeyKzfJ"
# load the parameters that is considered giving the best balance between train and test accuracy
model.load_weights('./Model/Best6_layer5(layer_id=all)_set5000_10epoch_saved-model-08.hdf5')


# keep the parameters as the best params, also keep the weight vector alpha
best_params = model.get_weights()[0]
alpha = model.get_weights()[1]

# + [markdown] id="vS0vPxOxLMfb"
# **Testing the Performance**

# + id="GQMr8Vt4KzhM"
best_params

# + id="_bNDu_fsKzje"
alpha 

# + id="vI1eaf9jKzlv"
# view each of the layer's name and its specifications
model.summary()

# + id="mApm_szWKHW0"
# load the parameters to the model
# Note: please change the name of the keras layer accordingly based on the model.summary()
model.get_layer('keras_layer').set_weights([best_params])
model.get_layer('class_weights').set_weights([np.array([alpha])])

# + id="rEySODFqKHaP"
from sklearn.metrics import roc_auc_score

# + id="LLEx0j2sKHdF"
#AUC for training set

Y_pred_train = model.predict(X_train)
roc_auc_score(Y_train, Y_pred_train[:, 1])

# + id="5VDEQpsnKHez"
#AUC for testing set

Y_pred_test = model.predict(X_test)
roc_auc_score(Y_test, Y_pred_test[:, 1])

StarcoderdataJupyterScriptsDedupFiltered