Permalink
Browse files

simple DNN demo for NYHackR talk

  • Loading branch information...
Jon Krohn
Jon Krohn committed Jan 30, 2017
1 parent 5d8aea0 commit 936374b5deda3c1bdcf409eef568d64e92e7e524
Showing with 387 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +292 −0 demos-for-talks/simple_dnn.ipynb
  3. +94 −0 demos-for-talks/simple_dnn.py
View
@@ -1,4 +1,5 @@
MNIST_data/
demos-for-talks/mnist/
# Byte-compiled / optimized / DLL files
__pycache__/
@@ -0,0 +1,292 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Simple Deep Neural Net for MNIST Classification"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### based on https://github.com/tflearn/tflearn/blob/master/examples/images/dnn.py"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from __future__ import division, print_function, absolute_import"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import tflearn"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"#### Data Loading and Preprocessing"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import tflearn.datasets.mnist as mnist"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading MNIST...\n",
"Succesfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.\n",
"Extracting mnist/train-images-idx3-ubyte.gz\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/jon/anaconda/lib/python3.5/gzip.py:274: VisibleDeprecationWarning: converting an array with ndim > 0 to an index will result in an error in the future\n",
" return self._buffer.read(size)\n",
"/Users/jon/anaconda/lib/python3.5/site-packages/tflearn/datasets/mnist.py:52: VisibleDeprecationWarning: converting an array with ndim > 0 to an index will result in an error in the future\n",
" data = data.reshape(num_images, rows, cols, 1)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading MNIST...\n",
"Succesfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.\n",
"Extracting mnist/train-labels-idx1-ubyte.gz\n",
"Downloading MNIST...\n",
"Succesfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.\n",
"Extracting mnist/t10k-images-idx3-ubyte.gz\n",
"Downloading MNIST...\n",
"Succesfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.\n",
"Extracting mnist/t10k-labels-idx1-ubyte.gz\n"
]
}
],
"source": [
"X, Y, testX, testY = mnist.load_data(one_hot=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Building Deep Neural Network"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"input_layer = tflearn.input_data(shape=[None, 784])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"dense1 = tflearn.fully_connected(input_layer, 64, activation='tanh', regularizer='L2', weight_decay=0.001)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"dropout1 = tflearn.dropout(dense1, 0.8)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"dense2 = tflearn.fully_connected(dropout1, 64, activation='tanh', regularizer='L2', weight_decay=0.001)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"dropout2 = tflearn.dropout(dense2, 0.8)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"softmax = tflearn.fully_connected(dropout2, 10, activation='softmax')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Regression using SGD with learning rate decay and Top-3 accuracy"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sgd = tflearn.SGD(learning_rate=0.1, lr_decay=0.96, decay_step=1000)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"top_k = tflearn.metrics.Top_k(3)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"net = tflearn.regression(softmax, optimizer=sgd, metric=top_k, loss='categorical_crossentropy')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Training"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model = tflearn.DNN(net, tensorboard_verbose=0)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Training Step: 9460 | total loss: \u001b[1m\u001b[32m0.18185\u001b[0m\u001b[0m\n",
"| SGD | epoch: 010 | loss: 0.18185 - top3: 0.9903 | val_loss: 0.13278 - val_acc: 0.9944 -- iter: 55000/55000\n",
"Training Step: 9460 | total loss: \u001b[1m\u001b[32m0.18185\u001b[0m\u001b[0m\n",
"| SGD | epoch: 010 | loss: 0.18185 - top3: 0.9903 | val_loss: 0.13278 - val_acc: 0.9944 -- iter: 55000/55000\n",
"--\n"
]
}
],
"source": [
"model.fit(X, Y, n_epoch=10, validation_set=(testX, testY), show_metric=True, run_id='dense_model')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"celltoolbar": "Raw Cell Format",
"kernelspec": {
"display_name": "Python [conda root]",
"language": "python",
"name": "conda-root-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
@@ -0,0 +1,94 @@
# coding: utf-8
# # Simple Deep Neural Net for MNIST Classification
# #### based on https://github.com/tflearn/tflearn/blob/master/examples/images/dnn.py
# In[1]:
from __future__ import division, print_function, absolute_import
# In[2]:
import tflearn
# #### Data Loading and Preprocessing
# In[3]:
import tflearn.datasets.mnist as mnist
# In[4]:
X, Y, testX, testY = mnist.load_data(one_hot=True)
# #### Building Deep Neural Network
# In[5]:
input_layer = tflearn.input_data(shape=[None, 784])
# In[6]:
dense1 = tflearn.fully_connected(input_layer, 64, activation='tanh', regularizer='L2', weight_decay=0.001)
# In[7]:
dropout1 = tflearn.dropout(dense1, 0.8)
# In[8]:
dense2 = tflearn.fully_connected(dropout1, 64, activation='tanh', regularizer='L2', weight_decay=0.001)
# In[9]:
dropout2 = tflearn.dropout(dense2, 0.8)
# In[10]:
softmax = tflearn.fully_connected(dropout2, 10, activation='softmax')
# #### Regression using SGD with learning rate decay and Top-3 accuracy
# In[11]:
sgd = tflearn.SGD(learning_rate=0.1, lr_decay=0.96, decay_step=1000)
# In[12]:
top_k = tflearn.metrics.Top_k(3)
# In[13]:
net = tflearn.regression(softmax, optimizer=sgd, metric=top_k, loss='categorical_crossentropy')
# #### Training
# In[14]:
model = tflearn.DNN(net, tensorboard_verbose=0)
# In[16]:
model.fit(X, Y, n_epoch=10, validation_set=(testX, testY), show_metric=True, run_id='dense_model')
# In[ ]:

0 comments on commit 936374b

Please sign in to comment.