I simplify tech for High School students. I don’t necessarily get everything correct but I do get kids interested in new areas of technology.
@antalszava I am just trying to get my brain around the difference between TensorflowJS Keras Layers Deep Learning logic (Not much different than Regular Python Tensorflow) and Quantum Machine Learning logic.
The following code is a complete webpage that runs the simple xOr Deep Learning example. It could be cut and pasted and run on any Chrome web browser.
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.3.0"> </script>
<input id="myButton123" type="button" value="xOr using Keras Layers" onclick="{
(async function myGo() {
document.getElementById('myButton123').style.backgroundColor = 'red'
const model = tf.sequential();
model.add(tf.layers.dense({inputShape: [2], units: 10, activation: 'sigmoid',}) ); // 2 inputs to 10 hidden layer nodes
model.add(tf.layers.dense({inputShape: [10], units: 1, activation: 'sigmoid',}) ); // from the 10 hidden layer nodes to 1 output layer
const myOptimizer = tf.train.sgd(0.5);
model.compile({optimizer: myOptimizer, loss: 'meanSquaredError'});
const xTrainingData = tf.tensor2d([[0,0], [0,1], [1,0], [1,1]]); // array defines shape // note should also add , 'int32'
const yTrainingTarget = tf.tensor2d([0,1,1,0], shape=[4,1]); // needs shape defined
var myFit = await model.fit(xTrainingData, yTrainingTarget, {
batchSize : 4,
epochs : 3000,
callbacks: {
onEpochEnd: async (epoch, logs) => {
document.getElementById('myDiv123').innerHTML = 'Epoch # ' + (epoch+1) + '/3000, Loss: ' + logs.loss + '<br><br>'
await tf.nextFrame(); // This improves UI responsiveness during training.
} // end onEpochEnd callback
} // end all callbacks
}) // end model.fit
const myPredictArray = await model.predict(xTrainingData).data() // just throw all the training data back in
document.getElementById('myDiv123').innerHTML += '[0,0] = ' + myPredictArray[0].toFixed(4) +'<br>'
document.getElementById('myDiv123').innerHTML += '[1,0] = ' + myPredictArray[1].toFixed(4) +'<br>'
document.getElementById('myDiv123').innerHTML += '[0,1] = ' + myPredictArray[2].toFixed(4) +'<br>'
document.getElementById('myDiv123').innerHTML += '[1,1] = ' + myPredictArray[3].toFixed(4) +'<br>'
document.getElementById('myButton123').style.backgroundColor = 'lightgray'
} )() // end Async function
}"><br><br>
<div id='myDiv123'>...</div><br>
It is fairly easy to follow the logic of this Deep Learning example:
- A button
- An asynchronous function
- Keras layer model creation
- xTrainingData, yTrainingTaget (labels)
- Fit (Training)
- Prediction
- Data output
For my students we really just have to get data in the correct format (The students would need much more data than this xOr example) and figure out the best model design for what we want to study.
Not so easy with Quantum Computing.
It is not clear to me how the Quantum computer is used to help train the model. I have this sort of working code example which I might be able to convert to train my xOr example, but I am not really sure how this program would actually be effecting the model. It looks to me like the quantum part is happening before the model is trained.
import pennylane as qml
import tensorflow as tf
import sklearn.datasets
import numpy as np
n_qubits = 1
layers = 1
data_dimension = 1
dev = qml.device("default.qubit", wires=n_qubits)
@qml.qnode(dev)
def qnode(inputs, m, c):
qml.templates.AngleEmbedding(inputs, wires=range(n_qubits))
qml.RX(m, wires=0)
qml.RZ(c, wires=0)
return [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)]
weight_shapes = {"m": 1, "c":1}
qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=n_qubits)
clayer1 = tf.keras.layers.Dense(n_qubits)
clayer2 = tf.keras.layers.Dense(data_dimension, activation="linear")
model = tf.keras.models.Sequential([qlayer])
X = np.array([-40, -10, 0, 8, 15, 22, 38], dtype=float)
Y = np.array([-40, 14, 32, 46, 59, 72, 100], dtype=float)
X1 = X.reshape((len(X), 1))
Y1 = Y.reshape((len(Y), 1))
model.compile(loss='mean_squared_error',
optimizer=tf.keras.optimizers.Adam(0.01))
model.fit(X, Y, epochs=60, batch_size=1)
model.summary()
# save model partially works
#tf.keras.models.save_model(model, './model.h5')
#print (dir(tf.keras.models.save_model))
qnn_results = model.evaluate(X, Y, verbose=2)
print ("qnn_results")
print (qnn_results)
Any ideas what the above code is doing, and suggestions for what I want to make?