From 6a580de6c50f49dcbdd55ccf3450ffa3529189ab Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sat, 13 Jul 2024 16:58:34 +0700 Subject: [PATCH] Create neural_network_quantum_with_explainability.py --- ...ral_network_quantum_with_explainability.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 ai_engine/neural_network_quantum_with_explainability.py diff --git a/ai_engine/neural_network_quantum_with_explainability.py b/ai_engine/neural_network_quantum_with_explainability.py new file mode 100644 index 0000000..b24e78a --- /dev/null +++ b/ai_engine/neural_network_quantum_with_explainability.py @@ -0,0 +1,26 @@ +import tensorflow as tf +from qiskit import QuantumCircuit, execute +from lime import lime_tabular + +class QuantumNeuralNetworkWithExplainability: + def __init__(self, input_shape, num_classes): + self.model = tf.keras.Sequential([ + tf.keras.layers.Dense(64, activation='relu', input_shape=input_shape), + tf.keras.layers.Dense(32, activation='relu'), + tf.keras.layers.Dense(num_classes, activation='softmax') + ]) + self.quantum_circuit = QuantumCircuit(5, 5) + self.explainer = lime_tabular.LimeTabularExplainer(self.model, num_features=10) + + def train(self, X_train, y_train, epochs=10, batch_size=32): + self.model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, validation_split=0.2) + self.quantum_circuit.barrier() + job = execute(self.quantum_circuit, backend='qasm_simulator', shots=1024) + result = job.result() + self.model.set_weights(result.get_statevector()) + + def predict(self, X_test): + return self.model.predict(X_test) + + def explain(self, X_test): + return self.explainer.explain_instance(X_test, num_features=10)