Sunday, May 6, 2018

How to Use the Keras Functional API for Deep Learning


How to Use the Keras Functional API for Deep Learning



The Keras Python library makes creating deep learning models fast and easy.
The sequential API allows you to create models layer-by-layer for most problems. It is limited in that it does not allow you to create models that share layers or have multiple inputs or outputs.
The functional API in Keras is an alternate way of creating models that offers a lot more flexibility, including creating more complex models.
In this tutorial, you will discover how to use the more flexible functional API in Keras to define deep learning models.
After completing this tutorial, you will know:
  • The difference between the Sequential and Functional APIs.
  • How to define simple Multilayer Perceptron, Convolutional Neural Network, and Recurrent Neural Network models using the functional API.
  • How to define more complex models with shared layers and multiple inputs and outputs.
Let’s get started.
  • Update Nov/2017: Update note about hanging dimension for input layers only affecting 1D input, thanks Joe.

Tutorial Overview

This tutorial is divided into 6 parts; they are:
  1. Keras Sequential Models
  2. Keras Functional Models
  3. Standard Network Models
  4. Shared Layers Model
  5. Multiple Input and Output Models
  6. Best Practices

1. Keras Sequential Models

As a review, Keras provides a Sequential model API.
This is a way of creating deep learning models where an instance of the Sequential class is created and model layers are created and added to it.
For example, the layers can be defined and passed to the Sequential as an array:
Layers can also be added piecewise:
The Sequential model API is great for developing deep learning models in most situations, but it also has some limitations.
For example, it is not straightforward to define models that may have multiple different input sources, produce multiple output destinations or models that re-use layers.

2. Keras Functional Models

The Keras functional API provides a more flexible way for defining models.
It specifically allows you to define multiple input or output models as well as models that share layers. More than that, it allows you to define ad hoc acyclic network graphs.
Models are defined by creating instances of layers and connecting them directly to each other in pairs, then defining a Model that specifies the layers to act as the input and output to the model.
Let’s look at the three unique aspects of Keras functional API in turn:

1. Defining Input

Unlike the Sequential model, you must create and define a standalone Input layer that specifies the shape of input data.
The input layer takes a shape argument that is a tuple that indicates the dimensionality of the input data.
When input data is one-dimensional, such as for a multilayer Perceptron, the shape must explicitly leave room for the shape of the mini-batch size used when splitting the data when training the network. Therefore, the shape tuple is always defined with a hanging last dimension when the input is one-dimensional (2,), for example:

2. Connecting Layers

The layers in the model are connected pairwise.
This is done by specifying where the input comes from when defining each new layer. A bracket notation is used, such that after the layer is created, the layer from which the input to the current layer comes from is specified.
Let’s make this clear with a short example. We can create the input layer as above, then create a hidden layer as a Dense that receives input only from the input layer.
Note the (visible) after the creation of the Dense layer that connects the input layer output as the input to the dense hidden layer.
It is this way of connecting layers piece by piece that gives the functional API its flexibility. For example, you can see how easy it would be to start defining ad hoc graphs of layers.

3. Creating the Model

After creating all of your model layers and connecting them together, you must define the model.
As with the Sequential API, the model is the thing you can summarize, fit, evaluate, and use to make predictions.
Keras provides a Model class that you can use to create a model from your created layers. It requires that you only specify the input and output layers. For example:
Now that we know all of the key pieces of the Keras functional API, let’s work through defining a suite of different models and build up some practice with it.
Each example is executable and prints the structure and creates a diagram of the graph. I recommend doing this for your own models to make it clear what exactly you have defined.
My hope is that these examples provide templates for you when you want to define your own models using the functional API in the future.

3. Standard Network Models

When getting started with the functional API, it is a good idea to see how some standard neural network models are defined.
In this section, we will look at defining a simple multilayer Perceptron, convolutional neural network, and recurrent neural network.
These examples will provide a foundation for understanding the more elaborate examples later.

Multilayer Perceptron

In this section, we define a multilayer Perceptron model for binary classification.
The model has 10 inputs, 3 hidden layers with 10, 20, and 10 neurons, and an output layer with 1 output. Rectified linear activation functions are used in each hidden layer and a sigmoid activation function is used in the output layer, for binary classification.
Running the example prints the structure of the network.
A plot of the model graph is also created and saved to file.
Multilayer Perceptron Network Graph
Multilayer Perceptron Network Graph

Convolutional Neural Network

In this section, we will define a convolutional neural network for image classification.
The model receives black and white 64×64 images as input, then has a sequence of two convolutional and pooling layers as feature extractors, followed by a fully connected layer to interpret the features and an output layer with a sigmoid activation for two-class predictions.
Running the example summarizes the model layers.
A plot of the model graph is also created and saved to file.
Convolutional Neural Network Graph
Convolutional Neural Network Graph

Recurrent Neural Network

In this section, we will define a long short-term memory recurrent neural network for sequence classification.
The model expects 100 time steps of one feature as input. The model has a single LSTM hidden layer to extract features from the sequence, followed by a fully connected layer to interpret the LSTM output, followed by an output layer for making binary predictions.
Running the example summarizes the model layers.
A plot of the model graph is also created and saved to file.
Recurrent Neural Network Graph
Recurrent Neural Network Graph

4. Shared Layers Model

Multiple layers can share the output from one layer.
For example, there may be multiple different feature extraction layers from an input, or multiple layers used to interpret the output from a feature extraction layer.
Let’s look at both of these examples.

Shared Input Layer

In this section, we define multiple convolutional layers with differently sized kernels to interpret an image input.
The model takes black and white images with the size 64×64 pixels. There are two CNN feature extraction submodels that share this input; the first has a kernel size of 4 and the second a kernel size of 8. The outputs from these feature extraction submodels are flattened into vectors and concatenated into one long vector and passed on to a fully connected layer for interpretation before a final output layer makes a binary classification.
Running the example summarizes the model layers.
A plot of the model graph is also created and saved to file.
Neural Network Graph With Shared Inputs
Neural Network Graph With Shared Inputs

Shared Feature Extraction Layer

In this section, we will two parallel submodels to interpret the output of an LSTM feature extractor for sequence classification.
The input to the model is 100 time steps of 1 feature. An LSTM layer with 10 memory cells interprets this sequence. The first interpretation model is a shallow single fully connected layer, the second is a deep 3 layer model. The output of both interpretation models are concatenated into one long vector that is passed to the output layer used to make a binary prediction.
Running the example summarizes the model layers.
A plot of the model graph is also created and saved to file.
Neural Network Graph With Shared Feature Extraction Layer
Neural Network Graph With Shared Feature Extraction Layer

5. Multiple Input and Output Models

The functional API can also be used to develop more complex models with multiple inputs, possibly with different modalities. It can also be used to develop models that produce multiple outputs.
We will look at examples of each in this section.

Multiple Input Model

We will develop an image classification model that takes two versions of the image as input, each of a different size. Specifically a black and white 64×64 version and a color 32×32 version. Separate feature extraction CNN models operate on each, then the results from both models are concatenated for interpretation and ultimate prediction.
Note that in the creation of the Model() instance, that we define the two input layers as an array. Specifically:
The complete example is listed below.
Running the example summarizes the model layers.
A plot of the model graph is also created and saved to file.
Neural Network Graph With Multiple Inputs
Neural Network Graph With Multiple Inputs

Multiple Output Model

In this section, we will develop a model that makes two different types of predictions. Given an input sequence of 100 time steps of one feature, the model will both classify the sequence and output a new sequence with the same length.
An LSTM layer interprets the input sequence and returns the hidden state for each time step. The first output model creates a stacked LSTM, interprets the features, and makes a binary prediction. The second output model uses the same output layer to make a real-valued prediction for each input time step.
Running the example summarizes the model layers.
A plot of the model graph is also created and saved to file.
Neural Network Graph With Multiple Outputs
Neural Network Graph With Multiple Outputs

6. Best Practices

In this section, I want to give you some tips to get the most out of the functional API when you are defining your own models.
  • Consistent Variable Names. Use the same variable name for the input (visible) and output layers (output) and perhaps even the hidden layers (hidden1, hidden2). It will help to connect things together correctly.
  • Review Layer Summary. Always print the model summary and review the layer outputs to ensure that the model was connected together as you expected.
  • Review Graph Plots. Always create a plot of the model graph and review it to ensure that everything was put together as you intended.
  • Name the layers. You can assign names to layers that are used when reviewing summaries and plots of the model graph. For example: Dense(1, name=’hidden1′).
  • Separate Submodels. Consider separating out the development of submodels and combine the submodels together at the end.
Do you have your own best practice tips when using the functional API?
Let me know in the comments.

Saturday, April 28, 2018

After 6 Years, GIMP 2.10 is Here With Ravishing New Looks and Tons of New Features


After 6 Years, GIMP 2.10 is Here With Ravishing New Looks and Tons of New Features

BY: Abhishek Prakash

Brief: 6 years after the release of GIMP 2.8, the major new stable release 2.10 is here. Have a look at the new look, new features and installation procedure.

Free and open source image editing application GIMP has a new major release today. GIMP 2.10 comes six years after the last major release 2.8.
It won’t be an exaggeration if I say that GIMP is the most popular image editor in Linux world and perhaps the best Adobe Photoshop alternative. The project was first started in 1996 and in the last 22 years, it has become the default image editor on almost all major Linux distributions. It is also available on Windows and macOS.

What’s new in GIMP 2.10

GIMP 2.10 has been ported to GEGL image processing engine and that’s the biggest change in this release. It brings out several new tools and improvements.

Some of the main new highlights of this release are:
  • Four new themes: Light, Gray, Dark, and System
  • Basic HiDPI support
  • GEGL is the new image processing engine providing high bit depth processing, multi-threaded and hardware accelerated pixel processing
  • Warp transform, the Unified transform and the Handle transform tools are some of the new tools
  • Many existing tools have been improved as well
  • Digital painting has been improved with canvas rotation and flipping, symmetry painting, MyPaint brush support
  • Support for OpenEXR, RGBE, WebP, HGT image formats have been added
  • Metadata viewing and editing for Exif, XMP, IPTC, and DICOM
  • Color management revamped
  • Linear color space workflow
  • Digital photography improvements with Exposure, Shadows-Highlights, High-pass, Wavelet Decompose, Panorama Projection tools
  • Usability improvements
If you want to see the GIMP 2.10 features in detail, please refer to its release note.

Install GIMP 2.10

Since GIMP 2.10 has just been released, it will be some time before your Linux distribution provides you the new version (unless you use Arch Linux). 
If you want to use it right now, you have two ways: Source Code or Flatpak.

Installing GIMP 2.10 via PPA in Ubuntu-basedLinuxdistributions

There is an unofficial PPA available that you can use to install GIMP 2.10 on Ubuntu, Linux Mint and other Ubuntu based Linux distributions right now. 
Open a terminal and use the following commands:
sudo add-apt-repository ppa:otto-kesselgulasch/gimp
sudo apt update
sudo apt install gimp
This will install GIMP 2.10. If you already have GIMP 2.8, it will be upgraded to GIMP 2.10.

Installing GIMP 2.10 with Flatpak in Ubuntu-based Linux distributions

You need to enable Flatpak support first. Use the commands below to install Flatpak in Ubuntu. 
sudo add-apt-repository ppa:alexlarsson/flatpak
sudo apt update
sudo apt install flatpak
You can refer to this page to know how to enable Flatpak support in other Linux distributions.
Once you have Fltapak support, use the command below to install GIMP 2.10:
flatpak install https://flathub.org/repo/appstream/org.gimp.GIMP.flatpakref
Once installed, if you don’t see it in the menu, you can run it using the command below:
flatpak run org.gimp.GIMP

Get GIMP 2.10 source code

Alternatively, you can always install from source code. You can download the source code from the link below:
I have been waiting for GIMP 2.10 release for some months now and I am looking forward to using its new features. How about you? Do you use GIMP? What new features you liked in GIMP 2.10?

Thursday, April 26, 2018

Misconception in artificial neural Network

Misconception In Artificial Neural Network

Many Training Algorithms Exist for Neural Networks

The learning algorithm of a neural network tries to optimize the neural network’s weights until some stopping condition has been met. This condition is typically either when the error of the network reaches an acceptable level of accuracy on the training set, when the error of the network on the validation set begins to deteriorate, or when the specified computational budget has been exhausted. The most common learning algorithm for neural networks is back-propagation, an algorithm that uses stochastic gradient descent, which was discussed earlier on in this series. Backpropagation consists of two steps:
  1. Feed-forward pass: The training dataset is passed through the network and the output from the neural network is recorded and the error of the network is calculated.
  2. Backward propagation: The error signal is passed back through the network and the weights of the neural network are optimized using gradient descent.
The are some problems with this approach. Adjusting all the weights at once can result in a significant movement of the neural network in weight space, the gradient descent algorithm is quite slow, and the gradient descent algorithm is susceptible to local minima. Local minima are a problem for specific types of neural networks including all product link neural networks. The first two problems can be addressed by using variants of gradient descent including momentum gradient descent (QuickProp), Nesterov’s Accelerated Momentum (NAG) gradient descent, the Adaptive Gradient Algorithm(AdaGrad), Resilient Propagation (RProp), and Root Mean Squared Propagation (RMSProp). As can be seen from the image below, significant improvements can be made on the classical gradient descent algorithm.
That said, these algorithms cannot overcome local minima and are also less useful when trying to optimize both the architecture and weights of a neural network concurrently. To achieve this, global optimization algorithms are needed. Two popular global optimization algorithms are Particle Swarm Optimization (PSO) and Genetic Algorithm (GA). Here is how they can be used to train neural networks.

Neural Network Vector Representation

This is done by encoding the neural network as a vector of weights, each representing the weight of a connection in the neural network. We can train neural networks using most meta-heuristic search algorithms. This technique does not work well with deep neural networks because the vectors become too large.
This diagram illustrates how a neural network can be represented in a vector notation and related to the concept of a search space or fitness landscape.

Particle Swarm Optimization

To train a neural network using a PSO, we construct a population/swarm of those neural networks. Each neural network is represented as a vector of weights and is adjusted according to its position from the global best particle and its personal best.
The fitness function is calculated as the sum-squared error of the reconstructed neural network after completing one feedforward pass of the training dataset. The main consideration with this approach is the velocity of the weight updates. This is because if the weights are adjusted too quickly, the sum-squared error of the neural networks will stagnate and no learning will occur.
This diagram shows how particles are attracted to one another in a single swarm Particle Swarm Optimization algorithm.

Genetic Algorithm

To train a neural network using a genetic algorithm, we first construct a population of the vector represented neural networks. Then, we apply the three genetic operators on that population to evolve better and better neural networks. These three operators are:
  1. Selection: Using the sum-squared error of each network calculated after one feedforward pass, we rank the population of neural networks. The top x% of the population is selected to "survive" to the next generation and be used for crossover.
  2. Crossover: The top x% of the population’s genes are allowed to cross over with one another. This process forms "offspring." In context, each offspring will represent a new neural network with weights from both of the "parent" neural networks.
  3. Mutation: This operator is required to maintain genetic diversity in the population. A small percentage of the population are selected to undergo mutation. Some of the weights in these neural networks will be adjusted randomly within a particular range.
This algorithm shows the selection, crossover, and mutation genetic operators being applied to a population of neural networks represented as vectors.
In addition to these population-based metaheuristic search algorithms, other algorithms have been used to train of neural networks including backpropagation with added momentum, differential evolutionLevenberg Marquardtsimulated annealing, and many more. Personally, I would recommend using a combination of local and global optimization algorithms to overcome the shortcomings of both.

Neural Networks Do Not Always Require a Lot of Data

Neural networks can use one of three learning strategies — namely, a supervised learning strategy, unsupervised learning strategy, or reinforcement learning strategy. Supervised learning requires at least two datasets, a training set that consists of inputs with the expected output, and a testing set that consists of inputs without the expected output. Both of these datasets must consist of labeled data, i.e. data patterns for which the target is known upfront. Unsupervised learning strategies are typically used to discover hidden structures (such as hidden Markov chains) in unlabeled data. They behave in a similar way to clustering algorithms. Reinforcement learning is based on the simple premise of rewarding neural networks for good behaviors and punishing them for bad behaviors. Because unsupervised and reinforcement learning strategies do not require that data be labeled they can be applied to under-formulated problems where the correct output is not known.

Unsupervised Learning

One of the most popular unsupervised neural network architectures is the Self-Organizing Map (also known as the Kohonen Map). Self-Organizing Maps are essentially a multi-dimensional scaling technique which constructs an approximation of the probability density function of some underlying dataset, Z, while preserving the topological structure of that dataset. This is done by mapping input vectors, zi, in the data set, Z, to weight vectors, vj, (neurons) in the feature map, V. Preserving the topological structure simply means that if two input vectors are close together in Z, then the neurons to which those input vectors map in V will also be close together.
For more information on Self-Organizing Maps and how they can be used to produce lower-dimensionality data sets click here. Another interesting application of SOMs is in coloring time series charts for stock trading. This is done to show what the market conditions are at that point in time. This website provides a detailed tutorial and code snippets for implementing the idea for improved Forex trading strategies.

Reinforcement Learning

Reinforcement learning strategies consist of three components. A policy that specifies how the neural network will make decisions, e.g. using technical and fundamental indicators. A reward function that distinguishes good from bad, e.g. making vs. losing money. And a value function which specifies the long term goal. In the context of financial markets (and gameplaying), reinforcement learning strategies are particularly useful because the neural network learns to optimize a particular quantity such as an appropriate measure of risk adjusted return.
This diagram shows how a neural network can be either negatively or positively reinforced.
PUBLISHED BY : JAYESH BAPU AHIRE