In this post, let us see how to build a deep learning model using Keras. If you haven't installed Tensorflow and Keras, I will show the simple way to install these two modules.
1. Installing Tensorflow and Keras
- Open Anaconda Navigator. Under Environments, create new environment in Python 3.6.
Create a new environment in Anaconda- Python 3.6 |
Created a new environment |
- Open terminal and pip install Tensorflow and Keras
Open Terminal |
Use the appropriate option for your computer (From Tensorflow website) |
- pip install tensorflow
install Tensorflow |
- then, pip install keras
install Keras |
- Now install Jupyter Notebook on this environment and launch
- import tensorflow and keras
Mission successful! |
2. Building Model using Keras
2.1 Four steps
Keras is simple to use. There are four steps in building a neural network model in Keras.
- Define the architecture
- Compile
- Fit
- Predict
In the following picture, I have shown these four steps (basic code is taken from here).
As you can see, first we have imported libraries.
a) Define the architecture
b) Compile
- Loss function,
- Optimizer and
- Metrics
b1) Loss function
In case of Loss function, commonly used are the
- mean_squared_error: commonly used for regression tasks
- mean_absolute_percentage_error
- categorical_crossentropy (Used for classification problems where y is one-hot-encoded, if not we can use to_categorical option to one-hot-encode y)
For more loss functions, refer this page.
b2) Optimizer
Commonly used optimizers are
- 'sgd' (Stochastic gradient descent)
- 'adam' (adam stands for adaptive moment estimation)
We can tune the learning rates, decay, momentum etc for better performance.
b3) Metrics
And in case metrics to measure the performance of the model, commonly used is the 'accuracy'
c) Fit
model.fit(X, target, validation_split=0.3, epochs=30, callbacks=[early_stopping_monitor])
d) Predict
Summary
In this post, we have seen
- how to install Tensorflow and Keras
- Four steps of basic model building in Keras
- Some important concepts/hyperparameters in those four steps
3. References
- https://keras.io/ has all the resources to learn Keras.
- For installing Tensorflow and Keras, refer https://towardsdatascience.com/python-environment-setup-for-deep-learning-on-windows-10-c373786e36d1
- Also you may refer this page for installing Keras
- Excellent blogpost series on training neural networks https://towardsdatascience.com/how-do-we-train-neural-networks-edd985562b73