Keras is a high-level neural networks library that is built on top of TensorFlow. It allows for easy and fast prototyping of deep learning models, making it a popular choice among researchers and developers. In this tutorial, we will provide an introduction to Keras and show you how to get started with building your first neural network using Keras.
- Installation
To use Keras, you first need to have TensorFlow installed on your system. You can install TensorFlow by running the following command:
pip install tensorflow
Once TensorFlow is installed, you can install Keras by running the following command:
pip install keras
- Importing Keras
To start using Keras in your code, you need to import it at the beginning of your script:
import keras
This will make all the Keras functions and classes available in your script.
- Building a neural network
Now that you have Keras installed and imported, you can start building your first neural network. Let’s start by creating a simple feedforward neural network with one hidden layer.
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))
model.summary()
In the code above, we first import the necessary classes from Keras. We then create a Sequential model which is a linear stack of layers. We add a Dense layer with 64 units and ‘relu’ activation function as the hidden layer, and another Dense layer with 10 units and ‘softmax’ activation function as the output layer. Finally, we print out a summary of the model.
- Compiling the model
After creating the model, we need to compile it by specifying the loss function, optimizer, and metrics to use.
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
In the code above, we compile the model with a categorical crossentropy loss function, the Adam optimizer, and accuracy as the metric to monitor during training.
- Training the model
Now that the model is compiled, we can train it on some data. In this tutorial, we will use a simple dataset from Keras called the MNIST dataset which consists of handwritten digits.
from keras.datasets import mnist
from keras.utils import to_categorical
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], -1) / 255.0
x_test = x_test.reshape(x_test.shape[0], -1) / 255.0
y_train = to_categorical(y_train, num_classes=10)
y_test = to_categorical(y_test, num_classes=10)
model.fit(x_train, y_train, batch_size=32, epochs=10, validation_data=(x_test, y_test))
In the code above, we first load the MNIST dataset and preprocess the data by reshaping and normalizing it. We also one-hot encode the labels using the to_categorical function. Finally, we train the model on the training data for 10 epochs with a batch size of 32 and validate it on the test data.
- Evaluating the model
After training the model, we can evaluate its performance on the test data.
score = model.evaluate(x_test, y_test)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
In the code above, we use the evaluate function to calculate the test loss and accuracy of the model.
Conclusion
In this tutorial, we have provided an introduction to Keras and shown you how to build a simple neural network using Keras and TensorFlow. Keras makes it easy to create and train deep learning models, and is a great tool for both beginners and experienced researchers. We encourage you to experiment with different architectures and datasets to learn more about deep learning and improve your skills.