In this tutorial, we will go through a simple example of building a Multi-Layer Perceptron (MLP) using Keras, a high-level neural networks API written in Python.
Overview
- What is Keras? Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano.
- What is MLP? MLP stands for Multi-Layer Perceptron, which is a class of feedforward artificial neural networks.
Setup
Before we start, make sure you have the following installed:
- Python
- TensorFlow
- Keras
You can install TensorFlow and Keras using pip:
pip install tensorflow
pip install keras
Step-by-Step Guide
- Import Libraries
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
- Create the dataset
# Generate some random data
x_train = np.random.random((1000, 20))
y_train = np.random.randint(2, size=(1000, 1))
# Normalize the data
x_train = x_train / 255.0
- Build the model
model = Sequential()
model.add(Dense(64, activation='relu', input_dim=20))
model.add(Dense(64, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
- Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
- Train the model
model.fit(x_train, y_train, epochs=10, batch_size=32)
- Evaluate the model
loss, accuracy = model.evaluate(x_train, y_train)
print(f"Loss: {loss}, Accuracy: {accuracy}")
- Make predictions
predictions = model.predict(x_train)
print(predictions)
Further Reading
For more information on Keras and MLPs, you can check out the following resources:
Conclusion
This tutorial provided a basic overview of building an MLP using Keras. You can now experiment with different architectures and datasets to improve your model's performance.
# Image for Keras
<center><img src="https://cloud-image.ullrai.com/q/Keras/" alt="Keras"/></center>