Build an AI Neural Network with Keras
Introduction to Keras
What is Keras and Keras.js
Keras is a high-level neural network API written in Python, running on top of TensorFlow. It simplifies building deep learning models by providing an intuitive interface.
This guide will walk you through:
Installing Keras with Python.
Building, training, and testing an XOR logic neural network with Keras in Python.
Train an XOR Neural Network Using Keras with Python
Step 1: Install Dependencies
To install Keras and TensorFlow, run the following command:
pip install tensorflow kerasStep 2: Code Breakdown for XOR Model with Keras (Python)
Import Required Libraries:
import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Densenumpy: For handling data arrays.tensorflowandkeras: Core frameworks for building the model.Sequential: A linear stack of layers for easy model construction.Dense: Fully connected layer for neural networks.
Define XOR Dataset:
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32)
y = np.array([[0], [1], [1], [0]], dtype=np.float32)XOR inputs (
X) represent the logic gate conditions.XOR outputs (
y) are the expected results.
Build the Model:
model = Sequential([
Dense(4, activation='relu', input_shape=(2,)),
Dense(1, activation='sigmoid')
])The input layer takes two values.
The hidden layer has 4 neurons with
ReLUactivation.The output layer has 1 neuron with a
sigmoidactivation function to predict binary values.
Compile and Train the Model:
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(X, y, epochs=500, verbose=0)The model is optimized using
adam.binary_crossentropyis used as the loss function.The model is trained for 500 epochs for better accuracy.
Test the Model:
predictions = np.round(model.predict(X))
print("Predictions:", predictions)Step 3: Full Code for XOR Model with Keras (Python)
import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32)
y = np.array([[0], [1], [1], [0]], dtype=np.float32)
model = Sequential([
Dense(4, activation='relu', input_shape=(2,)),
Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(X, y, epochs=500, verbose=0)
predictions = np.round(model.predict(X))
print("Predictions:", predictions)Expected Output:
Predictions: [[0.]
[1.]
[1.]
[0.]]Keras.js is a JavaScript library that allows you to run and train Keras models directly in Node.js or a web browser. In this guide, you'll learn:
How to train an XOR neural network with Keras.js in Node.js and JavaScript.
A step-by-step breakdown of each code block.
Full code samples with expected outputs.
Saving a Keras Model in ONNX Format
To save a Keras model in ONNX format, you can use the keras2onnx library to convert your Keras model to ONNX and then save it.
Here is a breakdown of the process:
1. Install Necessary Libraries:
keras2onnx: This library facilitates the conversion from Keras to ONNX.
pip install keras2onnxonnx: This is the core ONNX library for working with the ONNX format.
pip install onnx2. Convert to ONNX:
Use the
keras2onnx.convert_kerasfunction to convert the Keras model to ONNX.
import keras2onnx
# Specify target ONNX opset version
onnx_model = keras2onnx.convert_keras(model, target_opset=12) target_opset: This parameter specifies the target ONNX opset (operator set) version. Choose a version that is compatible with your ONNX runtime and target hardware.
4. Save the ONNX Model:
Save the ONNX model to a file using the
onnx.save()function.
import onnx
# Replace with your desired filename
onnx.save(onnx_model, "your_model.onnx") Train an XOR Neural Network Using Keras.js
For Node.js:
npm install keras-jsFor web browsers, include the library in your HTML file:
<script src="https://cdn.jsdelivr.net/npm/keras-js"></script>Step 2: Training an XOR Neural Network with Keras.js in Node.js
Create a file called xor_kerasjs_node.js and follow these steps:
Step 2.1: Import Dependencies and Initialize Model
const KerasJS = require('keras-js');
// Initialize the model
const model = new KerasJS.Model({
filepath: './xor_model.bin',
gpu: true // Enable GPU for performance boost
});Step 2.2: Prepare the XOR Dataset
const trainData = {
inputs: new Float32Array([
0, 0,
0, 1,
1, 0,
1, 1
]),
labels: new Float32Array([0, 1, 1, 0])
};This dataset represents XOR logic gate inputs and corresponding outputs.
Step 2.3: Train the Model
async function trainModel() {
await model.ready();
for (let epoch = 0; epoch < 500; epoch++) {
await model.fit({
inputs: trainData.inputs,
labels: trainData.labels,
epochs: 1
});
console.log(`Epoch ${epoch + 1} complete.`);
}
}This loop iterates through 500 epochs to train the model progressively.
Step 2.4: Test the Model
async function testModel() {
const inputData = { 'input': trainData.inputs };
const outputData = await model.predict(inputData);
console.log('Predictions:', outputData['output']);
}This section performs inference using the trained XOR model.
Step 2.5: Run Training and Testing
(async function () {
await trainModel();
await testModel();
})();This self-invoking function executes both training and testing.
Step 2.6: Run the File
Execute the file with:
node xor_kerasjs_node.jsExpected Output:
Epoch 1 complete.
Epoch 2 complete.
...
Epoch 500 complete.
Predictions: [0, 1, 1, 0]Step 3: Training an XOR Neural Network with Keras.js in the Browser
Step 3.1: Create an HTML File
Create index.html with the following content:
<!DOCTYPE html>
<html>
<head>
<title>XOR Model with Keras.js</title>
<script src="https://cdn.jsdelivr.net/npm/keras-js"></script>
<script defer src="script.js"></script>
</head>
<body>
<h1>XOR Neural Network with Keras.js in the Browser</h1>
<p>Check the console for results.</p>
</body>
</html>Step 3.2: Create a JavaScript File
Create script.js with the following content:
async function runModel() {
const model = new KerasJS.Model({
filepath: './xor_model.bin',
gpu: true
});
await model.ready();
const trainData = {
inputs: new Float32Array([0, 0, 0, 1, 1, 0, 1, 1]),
labels: new Float32Array([0, 1, 1, 0])
};
// Training Loop
for (let epoch = 0; epoch < 500; epoch++) {
await model.fit({
inputs: trainData.inputs,
labels: trainData.labels,
epochs: 1
});
console.log(`Epoch ${epoch + 1} complete.`);
}
const outputData = await model.predict({ 'input': trainData.inputs });
console.log('Predictions:', outputData['output']);
}
runModel();Step 3.3: Start a Local Server
To run the web application locally:
npx http-serverVisit
http://localhost:8080
in your browser and check the console for predictions.
Expected Output:
Epoch 1 complete.
Epoch 2 complete.
...
Epoch 500 complete.
Predictions: [0, 1, 1, 0]Conclusion
In this guide, you have learned how to:
Install Keras with Python.
Train and test an XOR logic neural network using Keras.
Successfully run your model predictions with accurate results.
Installed Keras.js for Node.js and the browser.
Built and trained an XOR neural network using Keras.js.
Understood each code block through a step-by-step breakdown.


