Introduction to Python Deep Learning with Keras
Two of the top numerical platforms in Python that provide the basis for Deep Learning research and development are Theano and TensorFlow.
Both are very powerful libraries, but both can be difficult to use directly for creating deep learning models.
In this post, you will discover the Keras Python library that provides a clean and convenient way to create a range of deep learning models on top of Theano or TensorFlow.
Let’s get started.
Update Oct/2016: Updated examples for Keras 1.1.0, Theano 0.8.2 and TensorFlow 0.10.0.
What is Keras?
Keras is a minimalist Python library for deep learning that can run on top of Theano or TensorFlow.
It was developed to make implementing deep learning models as fast and easy as possible for research and development.
It runs on Python 2.7 or 3.5 and can seamlessly execute on GPUs and CPUs given the underlying frameworks. It is released under the permissive MIT license.
Keras was developed and maintained by François Chollet, a Google engineer using four guiding principles:
- Modularity: A model can be understood as a sequence or a graph alone. All the concerns of a deep learning model are discrete components that can be combined in arbitrary ways.
- Minimalism: The library provides just enough to achieve an outcome, no frills and maximizing readability.
- Extensibility: New components are intentionally easy to add and use within the framework, intended for researchers to trial and explore new ideas.
- Python: No separate model files with custom file formats. Everything is native Python.
How to Install Keras
Keras is relatively straightforward to install if you already have a working Python and SciPy environment.
You must also have an installation of Theano or TensorFlow on your system already.
You can see installation instructions for both platforms here:
Keras can be installed easily using PyPI, as follows:
1 | sudo pip install keras |
At the time of writing, the most recent version of Keras is version 1.1.0. You can check your version of Keras on the command line using the following snippet:
You can check your version of Keras on the command line using the following snippet:
1 | python -c "import keras; print keras.__version__" |
Running the above script you will see:
1 | 1.1.0 |
You can upgrade your installation of Keras using the same method:
1 | sudo pip install --upgrade keras |
Theano and TensorFlow Backends for Keras
Assuming you have both Theano and TensorFlow installed, you can configure the backend used by Keras.
The easiest way is by adding or editing the Keras configuration file in your home directory:
1 | ~/.keras/keras.json |
Which has the format:
123456 | { "image_dim_ordering": "tf", "epsilon": 1e-07, "floatx": "float32", "backend": "tensorflow"} |
In this configuration file you can change the “backend” property from “tensorflow” (the default) to “theano“. Keras will then use the configuration the next time it is run.
You can confirm the backend used by Keras using the following snippet on the command line:
1 | python -c "from keras import backend; print backend._BACKEND" |
Running this with default configuration you will see:
12 | Using TensorFlow backend.tensorflow |
You can also specify the backend to use by Keras on the command line by specifying the KERAS_BACKEND environment variable, as follows:
1 | KERAS_BACKEND=theano python -c "from keras import backend; print(backend._BACKEND)" |
Running this example prints:
12 | Using Theano backend.theano |
Build Deep Learning Models with Keras
The focus of Keras is the idea of a model.
The main type of model is called a Sequence which is a linear stack of layers.
You create a sequence and add layers to it in the order that you wish for the computation to be performed.
Once defined, you compile the model which makes use of the underlying framework to optimize the computation to be performed by your model. In this you can specify the loss function and the optimizer to be used.
Once compiled, the model must be fit to data. This can be done one batch of data at a time or by firing off the entire model training regime. This is where all the compute happens.
Once trained, you can use your model to make predictions on new data.
We can summarize the construction of deep learning models in Keras as follows:
- Define your model. Create a sequence and add layers.
- Compile your model. Specify loss functions and optimizers.
- Fit your model. Execute the model using data.
- Make predictions. Use the model to generate predictions on new data.
Need help with Deep Learning in Python?
Take my free 2-week email course and discover MLPs, CNNs and LSTMs (with code).
Click to sign-up now and also get a free PDF Ebook version of the course.
Keras Resources
The list below provides some additional resources that you can use to learn more about Keras.
Are you looking for a good Deep Learning tutorial to get started, take a look at:
Summary
In this post, you discovered the Keras Python library for deep learning research and development.
You discovered that Keras is designed for minimalism and modularity allowing you to very quickly define deep learning models and run them on top of a Theano or TensorFlow backend.
Do you have any questions about Keras or this post? Ask your questions in the comments and I will do my best to answer them.
Frustrated With Your Progress In Deep Learning?
What If You Could Develop A Network in Minutes
…with just a few lines of Python
It covers self-study tutorials and end-to-end projects on topics like:
Multilayer Perceptrons, Convolutional Nets and Recurrent Neural Nets, and more…
Finally Bring Deep Learning To
Your Own Projects
Skip the Academics. Just Results.
相關推薦
Introduction to Python Deep Learning with Keras
Tweet Share Share Google Plus Two of the top numerical platforms in Python that provide the basi
How to Use Metrics for Deep Learning with Keras in Python
Tweet Share Share Google Plus The Keras library provides a way to calculate and report on a suit
(轉) Learning Deep Learning with Keras
trees create pda sse caffe latex .py encode you Learning Deep Learning with Keras Piotr Migda? - blog Projects Articles Publications Res
Image Augmentation for Deep Learning With Keras
Tweet Share Share Google Plus Data preparation is required when working with neural network and
9 Ways to Get Help with Deep Learning in Keras
Tweet Share Share Google Plus Keras is a Python deep learning library that can use the efficient
[Deep-Learning-with-Python]基於Keras的房價預測
預測房價:迴歸問題 迴歸問題預測結果為連續值,而不是離散的類別。 波士頓房價資料集 通過20世紀70年代波士頓郊區房價資料集,預測平均房價;資料集的特徵包括犯罪率、稅率等資訊。資料集只有506條記錄,劃分成404的訓練集和102的測試集。每個記錄的特徵
[Deep-Learning-with-Python]神經網絡的數學基礎
val 描述 優化算法 初始化 訓練數據 eight data 一個數 NPU 理解深度學習需要熟悉一些簡單的數學概念:Tensors(張量)、Tensor operations 張量操作、differentiation微分、gradient descent 梯度下降等等。
[Deep-Learning-with-Python]GAN圖片生成
gen 優秀 img 人工 process trick inpu generator type GAN 由Goodfellow等人於2014年引入的生成對抗網絡(GAN)是用於學習圖像潛在空間的VAE的替代方案。它們通過強制生成的圖像在統計上幾乎與真實圖像幾乎無法區分,從而
Python深度學習(Deep Learning with Python) 中文版+英文版+原始碼
Keras作者、谷歌大腦François Chollet最新撰寫的深度學習Python教程實戰書籍(2017年12月出版)介紹深入學習使用Python語言和強大Keras庫,詳實新穎。PDF高清中文版+英文版+原始碼,這本書讓你通過直觀的解釋和例項學習深度學習,不得不看。 下載地址:https://www.
11 Deep Learning With Python Libraries and Frameworks
TensorFlow is an open-source library for numerical computation in which it uses data flow graphs. The Google Brain Team researchers developed this with the
We'd Like To See Deep Learning Hand To Hand With Deep Feeling
Hardly could anyone find a point at the Heidelberg Laurate Forum space-time without hearing about Artificial Intelligence, Machine Learning and Deep Learni
How to do Deep Learning on Graphs with Graph Convolutional Networks
Observe that the weights (the values) in each row of the adjacency matrix have been divided by the degree of the node corresponding to the row. We apply th
Introduction to Random Forest Algorithm with Python
Using Neural Networks:Define the model :# Build a neural network :NN_model = Sequential()NN_model.add(Dense(128, input_dim = 68, activation='relu'))NN_mode
Introduction to Time Series Forecasting With Python
I believe my books offer thousands of dollars of education for tens of dollars each. They are months if not years of experience distilled into a few hundre
Time Series Prediction With Deep Learning in Keras
Tweet Share Share Google Plus Time Series prediction is a difficult problem both to frame and to
[Deep-Learning-with-Python]計算機視覺中的深度學習
包括: - 理解卷積神經網路 - 使用資料增強緩解過擬合 - 使用預訓練卷積網路做特徵提取 - 微調預訓練網路模型 - 視覺化卷積網路學習結果以及分類決策過程 介紹卷積神經網路,convnets,深度學習在計算機視覺方面廣泛應用的一個網路模型。 卷
Deep Learning with Python 系列筆記(六):深度學習實踐進階
Keras functional API keras中常用的Sequential 模型,通常假設網路只有一個輸入和一個輸出,並且這些網路層之間是一種線性的堆疊。如下: 事實上,這種結構太過普通,我們需要把許多內容和實際操作都通過一個Sequential模型實現
[Deep-Learning-with-Python] 文字序列中的深度學習
將文字資料處理成有用的資料表示 迴圈神經網路 使用1D卷積處理序列資料 深度學習模型可以處理文字序列、時間序列、一般性序列資料等等。處理序列資料的兩個基本深度學習演算法是迴圈神經網路和1D卷積(2D卷積的一維模式)。 文字資料 文字是最廣泛的序列資料形
Deep learning with python 學習筆記(一)
一、前饋神經網路 第三章: 編寫前饋神經網路的程式碼:#Layer Neural Network for Regression import autograd.numpy as np import autograd.numpy.random as npr from aut
paper reading----Xception: Deep Learning with Depthwise Separable Convolutions
module 之間 pap AD lin reg arch dual pooling 背景以及問題描述: Inception-style models的基本單元是Inception module。Inception model是Inception mod