"For this section we will use the Kaggle otto challenge.\n",
"If you want to follow, Get the data from Kaggle: https://www.kaggle.com/c/otto-group-product-classification-challenge/data"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"#### About the data"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"The Otto Group is one of the world’s biggest e-commerce companies, A consistent analysis of the performance of products is crucial. However, due to diverse global infrastructure, many identical products get classified differently.\n",
"For this competition, we have provided a dataset with 93 features for more than 200,000 products. The objective is to build a predictive model which is able to distinguish between our main product categories. \n",
"Each row corresponds to a single product. There are a total of 93 numerical features, which represent counts of different events. All features have been obfuscated and will not be defined any further.\n",
"Now lets create and train a logistic regression model."
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"---"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"# Keras\n",
"\n",
"## Deep Learning library for Theano and TensorFlow"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"Keras is a minimalist, highly modular neural networks library, written in Python and capable of running on top of either TensorFlow or Theano. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result with the least possible delay is key to doing good research.\n",
"ref: https://keras.io/"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Why this name, Keras?\n",
"\n",
"Keras (κέρας) means _horn_ in Greek. It is a reference to a literary image from ancient Greek and Latin literature, first found in the _Odyssey_, where dream spirits (_Oneiroi_, singular _Oneiros_) are divided between those who deceive men with false visions, who arrive to Earth through a gate of ivory, and those who announce a future that will come to pass, who arrive through a gate of horn. It's a play on the words κέρας (horn) / κραίνω (fulfill), and ἐλέφας (ivory) / ἐλεφαίρομαι (deceive).\n",
"\n",
"Keras was initially developed as part of the research effort of project ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating System).\n",
"\n",
">_\"Oneiroi are beyond our unravelling --who can be sure what tale they tell? Not all that men look for comes to pass. Two gates there are that give passage to fleeting Oneiroi; one is made of horn, one of ivory. The Oneiroi that pass through sawn ivory are deceitful, bearing a message that will not be fulfilled; those that come out through polished horn have truth behind them, to be accomplished for men who see them.\"_ Homer, Odyssey 19. 562 ff (Shewring translation)."
"<pre>The core data structure of Keras is a <b>model</b>, a way to organize layers. The main type of model is the <b>Sequential</b> model, a linear stack of layers.</pre>\n"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"What we did here is stacking a Fully Connected (<b>Dense</b>) layer of trainable weights from the input to the output and an <b>Activation</b> layer on top of the weights layer."
"If you need to, you can further configure your optimizer. A core principle of Keras is to make things reasonably simple, while allowing the user to be fully in control when they need to (the ultimate control being the easy extensibility of the source code).\n",
"Here we used <b>SGD</b> (stochastic gradient descent) as an optimization algorithm for our trainable weights. "
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"\"Data Sciencing\" this example a little bit more\n",
"====="
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"What we did here is nice, however in the real world it is not useable because of overfitting.\n",
"Lets try and solve it with cross validation."
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"##### Overfitting"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"In overfitting, a statistical model describes random error or noise instead of the underlying relationship. Overfitting occurs when a model is excessively complex, such as having too many parameters relative to the number of observations. \n",
"\n",
"A model that has been overfit has poor predictive performance, as it overreacts to minor fluctuations in the training data."
]
},
{
"cell_type":"markdown",
"metadata":{
"collapsed":false
},
"source":[
"\n",
"<img src =\"imgs/overfitting.png\">"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"<pre>To avoid overfitting, we will first split out data to training set and test set and test out model on the test set.\n",
"Next: we will use two of keras's callbacks <b>EarlyStopping</b> and <b>ModelCheckpoint</b></pre>"
]
},
{
"cell_type":"code",
"execution_count":13,
"metadata":{
"collapsed":false
},
"outputs":[
{
"name":"stdout",
"output_type":"stream",
"text":[
"Train on 19835 samples, validate on 3501 samples\n",
"Building a question answering system, an image classification model, a Neural Turing Machine, a word2vec embedder or any other model is just as fast. The ideas behind deep learning are simple, so why should their implementation be painful?"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"#### Theoretical Motivations for depth"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
">Much has been studied about the depth of neural nets. Is has been proven mathematically[1] and empirically that convolutional neural network benifit from depth! "
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"[1] - On the Expressive Power of Deep Learning: A Tensor Analysis - Cohen, et al 2015"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"#### Theoretical Motivations for depth"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"One much quoted theorem about neural network states that:"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
">Universal approximation theorem states[1] that a feed-forward network with a single hidden layer containing a finite number of neurons (i.e., a multilayer perceptron), can approximate continuous functions on compact subsets of $\\mathbb{R}^n$, under mild assumptions on the activation function. The theorem thus states that simple neural networks can represent a wide variety of interesting functions when given appropriate parameters; however, it does not touch upon the algorithmic learnability of those parameters."
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"[1] - Approximation Capabilities of Multilayer Feedforward Networks - Kurt Hornik 1991"