Bladeren bron

change project layout

aymericdamien 9 jaren geleden
bovenliggende
commit
4fbed84d52

+ 30 - 0
LICENSE

@@ -0,0 +1,30 @@
+The MIT License (MIT)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+All contributions by Aymeric Damien:
+Copyright (c) 2015, Aymeric Damien.
+All rights reserved.
+
+All other contributions:
+Copyright (c) 2015, the respective contributors.
+All rights reserved.
+
+Each contributor holds copyright over their respective contributions.
+The project versioning (Git) records all such contribution source information.

basic_operations.py → examples/1 - Introduction/basic_operations.py


helloworld.py → examples/1 - Introduction/helloworld.py


+ 144 - 0
examples/2 - Basic Classifiers/input_data.py

@@ -0,0 +1,144 @@
+"""Functions for downloading and reading MNIST data."""
+from __future__ import print_function
+import gzip
+import os
+import urllib
+import numpy
+SOURCE_URL = 'http://yann.lecun.com/exdb/mnist/'
+def maybe_download(filename, work_directory):
+  """Download the data from Yann's website, unless it's already here."""
+  if not os.path.exists(work_directory):
+    os.mkdir(work_directory)
+  filepath = os.path.join(work_directory, filename)
+  if not os.path.exists(filepath):
+    filepath, _ = urllib.urlretrieve(SOURCE_URL + filename, filepath)
+    statinfo = os.stat(filepath)
+    print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.')
+  return filepath
+def _read32(bytestream):
+  dt = numpy.dtype(numpy.uint32).newbyteorder('>')
+  return numpy.frombuffer(bytestream.read(4), dtype=dt)
+def extract_images(filename):
+  """Extract the images into a 4D uint8 numpy array [index, y, x, depth]."""
+  print('Extracting', filename)
+  with gzip.open(filename) as bytestream:
+    magic = _read32(bytestream)
+    if magic != 2051:
+      raise ValueError(
+          'Invalid magic number %d in MNIST image file: %s' %
+          (magic, filename))
+    num_images = _read32(bytestream)
+    rows = _read32(bytestream)
+    cols = _read32(bytestream)
+    buf = bytestream.read(rows * cols * num_images)
+    data = numpy.frombuffer(buf, dtype=numpy.uint8)
+    data = data.reshape(num_images, rows, cols, 1)
+    return data
+def dense_to_one_hot(labels_dense, num_classes=10):
+  """Convert class labels from scalars to one-hot vectors."""
+  num_labels = labels_dense.shape[0]
+  index_offset = numpy.arange(num_labels) * num_classes
+  labels_one_hot = numpy.zeros((num_labels, num_classes))
+  labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
+  return labels_one_hot
+def extract_labels(filename, one_hot=False):
+  """Extract the labels into a 1D uint8 numpy array [index]."""
+  print('Extracting', filename)
+  with gzip.open(filename) as bytestream:
+    magic = _read32(bytestream)
+    if magic != 2049:
+      raise ValueError(
+          'Invalid magic number %d in MNIST label file: %s' %
+          (magic, filename))
+    num_items = _read32(bytestream)
+    buf = bytestream.read(num_items)
+    labels = numpy.frombuffer(buf, dtype=numpy.uint8)
+    if one_hot:
+      return dense_to_one_hot(labels)
+    return labels
+class DataSet(object):
+  def __init__(self, images, labels, fake_data=False):
+    if fake_data:
+      self._num_examples = 10000
+    else:
+      assert images.shape[0] == labels.shape[0], (
+          "images.shape: %s labels.shape: %s" % (images.shape,
+                                                 labels.shape))
+      self._num_examples = images.shape[0]
+      # Convert shape from [num examples, rows, columns, depth]
+      # to [num examples, rows*columns] (assuming depth == 1)
+      assert images.shape[3] == 1
+      images = images.reshape(images.shape[0],
+                              images.shape[1] * images.shape[2])
+      # Convert from [0, 255] -> [0.0, 1.0].
+      images = images.astype(numpy.float32)
+      images = numpy.multiply(images, 1.0 / 255.0)
+    self._images = images
+    self._labels = labels
+    self._epochs_completed = 0
+    self._index_in_epoch = 0
+  @property
+  def images(self):
+    return self._images
+  @property
+  def labels(self):
+    return self._labels
+  @property
+  def num_examples(self):
+    return self._num_examples
+  @property
+  def epochs_completed(self):
+    return self._epochs_completed
+  def next_batch(self, batch_size, fake_data=False):
+    """Return the next `batch_size` examples from this data set."""
+    if fake_data:
+      fake_image = [1.0 for _ in xrange(784)]
+      fake_label = 0
+      return [fake_image for _ in xrange(batch_size)], [
+          fake_label for _ in xrange(batch_size)]
+    start = self._index_in_epoch
+    self._index_in_epoch += batch_size
+    if self._index_in_epoch > self._num_examples:
+      # Finished epoch
+      self._epochs_completed += 1
+      # Shuffle the data
+      perm = numpy.arange(self._num_examples)
+      numpy.random.shuffle(perm)
+      self._images = self._images[perm]
+      self._labels = self._labels[perm]
+      # Start next epoch
+      start = 0
+      self._index_in_epoch = batch_size
+      assert batch_size <= self._num_examples
+    end = self._index_in_epoch
+    return self._images[start:end], self._labels[start:end]
+def read_data_sets(train_dir, fake_data=False, one_hot=False):
+  class DataSets(object):
+    pass
+  data_sets = DataSets()
+  if fake_data:
+    data_sets.train = DataSet([], [], fake_data=True)
+    data_sets.validation = DataSet([], [], fake_data=True)
+    data_sets.test = DataSet([], [], fake_data=True)
+    return data_sets
+  TRAIN_IMAGES = 'train-images-idx3-ubyte.gz'
+  TRAIN_LABELS = 'train-labels-idx1-ubyte.gz'
+  TEST_IMAGES = 't10k-images-idx3-ubyte.gz'
+  TEST_LABELS = 't10k-labels-idx1-ubyte.gz'
+  VALIDATION_SIZE = 5000
+  local_file = maybe_download(TRAIN_IMAGES, train_dir)
+  train_images = extract_images(local_file)
+  local_file = maybe_download(TRAIN_LABELS, train_dir)
+  train_labels = extract_labels(local_file, one_hot=one_hot)
+  local_file = maybe_download(TEST_IMAGES, train_dir)
+  test_images = extract_images(local_file)
+  local_file = maybe_download(TEST_LABELS, train_dir)
+  test_labels = extract_labels(local_file, one_hot=one_hot)
+  validation_images = train_images[:VALIDATION_SIZE]
+  validation_labels = train_labels[:VALIDATION_SIZE]
+  train_images = train_images[VALIDATION_SIZE:]
+  train_labels = train_labels[VALIDATION_SIZE:]
+  data_sets.train = DataSet(train_images, train_labels)
+  data_sets.validation = DataSet(validation_images, validation_labels)
+  data_sets.test = DataSet(test_images, test_labels)
+  return data_sets

linear_regression.py → examples/2 - Basic Classifiers/linear_regression.py


logistic_regression.py → examples/2 - Basic Classifiers/logistic_regression.py


nearest_neighbor.py → examples/2 - Basic Classifiers/nearest_neighbor.py


alexnet.py → examples/3 - Neural Networks/alexnet.py


convolutional_network.py → examples/3 - Neural Networks/convolutional_network.py


+ 144 - 0
examples/3 - Neural Networks/input_data.py

@@ -0,0 +1,144 @@
+"""Functions for downloading and reading MNIST data."""
+from __future__ import print_function
+import gzip
+import os
+import urllib
+import numpy
+SOURCE_URL = 'http://yann.lecun.com/exdb/mnist/'
+def maybe_download(filename, work_directory):
+  """Download the data from Yann's website, unless it's already here."""
+  if not os.path.exists(work_directory):
+    os.mkdir(work_directory)
+  filepath = os.path.join(work_directory, filename)
+  if not os.path.exists(filepath):
+    filepath, _ = urllib.urlretrieve(SOURCE_URL + filename, filepath)
+    statinfo = os.stat(filepath)
+    print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.')
+  return filepath
+def _read32(bytestream):
+  dt = numpy.dtype(numpy.uint32).newbyteorder('>')
+  return numpy.frombuffer(bytestream.read(4), dtype=dt)
+def extract_images(filename):
+  """Extract the images into a 4D uint8 numpy array [index, y, x, depth]."""
+  print('Extracting', filename)
+  with gzip.open(filename) as bytestream:
+    magic = _read32(bytestream)
+    if magic != 2051:
+      raise ValueError(
+          'Invalid magic number %d in MNIST image file: %s' %
+          (magic, filename))
+    num_images = _read32(bytestream)
+    rows = _read32(bytestream)
+    cols = _read32(bytestream)
+    buf = bytestream.read(rows * cols * num_images)
+    data = numpy.frombuffer(buf, dtype=numpy.uint8)
+    data = data.reshape(num_images, rows, cols, 1)
+    return data
+def dense_to_one_hot(labels_dense, num_classes=10):
+  """Convert class labels from scalars to one-hot vectors."""
+  num_labels = labels_dense.shape[0]
+  index_offset = numpy.arange(num_labels) * num_classes
+  labels_one_hot = numpy.zeros((num_labels, num_classes))
+  labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
+  return labels_one_hot
+def extract_labels(filename, one_hot=False):
+  """Extract the labels into a 1D uint8 numpy array [index]."""
+  print('Extracting', filename)
+  with gzip.open(filename) as bytestream:
+    magic = _read32(bytestream)
+    if magic != 2049:
+      raise ValueError(
+          'Invalid magic number %d in MNIST label file: %s' %
+          (magic, filename))
+    num_items = _read32(bytestream)
+    buf = bytestream.read(num_items)
+    labels = numpy.frombuffer(buf, dtype=numpy.uint8)
+    if one_hot:
+      return dense_to_one_hot(labels)
+    return labels
+class DataSet(object):
+  def __init__(self, images, labels, fake_data=False):
+    if fake_data:
+      self._num_examples = 10000
+    else:
+      assert images.shape[0] == labels.shape[0], (
+          "images.shape: %s labels.shape: %s" % (images.shape,
+                                                 labels.shape))
+      self._num_examples = images.shape[0]
+      # Convert shape from [num examples, rows, columns, depth]
+      # to [num examples, rows*columns] (assuming depth == 1)
+      assert images.shape[3] == 1
+      images = images.reshape(images.shape[0],
+                              images.shape[1] * images.shape[2])
+      # Convert from [0, 255] -> [0.0, 1.0].
+      images = images.astype(numpy.float32)
+      images = numpy.multiply(images, 1.0 / 255.0)
+    self._images = images
+    self._labels = labels
+    self._epochs_completed = 0
+    self._index_in_epoch = 0
+  @property
+  def images(self):
+    return self._images
+  @property
+  def labels(self):
+    return self._labels
+  @property
+  def num_examples(self):
+    return self._num_examples
+  @property
+  def epochs_completed(self):
+    return self._epochs_completed
+  def next_batch(self, batch_size, fake_data=False):
+    """Return the next `batch_size` examples from this data set."""
+    if fake_data:
+      fake_image = [1.0 for _ in xrange(784)]
+      fake_label = 0
+      return [fake_image for _ in xrange(batch_size)], [
+          fake_label for _ in xrange(batch_size)]
+    start = self._index_in_epoch
+    self._index_in_epoch += batch_size
+    if self._index_in_epoch > self._num_examples:
+      # Finished epoch
+      self._epochs_completed += 1
+      # Shuffle the data
+      perm = numpy.arange(self._num_examples)
+      numpy.random.shuffle(perm)
+      self._images = self._images[perm]
+      self._labels = self._labels[perm]
+      # Start next epoch
+      start = 0
+      self._index_in_epoch = batch_size
+      assert batch_size <= self._num_examples
+    end = self._index_in_epoch
+    return self._images[start:end], self._labels[start:end]
+def read_data_sets(train_dir, fake_data=False, one_hot=False):
+  class DataSets(object):
+    pass
+  data_sets = DataSets()
+  if fake_data:
+    data_sets.train = DataSet([], [], fake_data=True)
+    data_sets.validation = DataSet([], [], fake_data=True)
+    data_sets.test = DataSet([], [], fake_data=True)
+    return data_sets
+  TRAIN_IMAGES = 'train-images-idx3-ubyte.gz'
+  TRAIN_LABELS = 'train-labels-idx1-ubyte.gz'
+  TEST_IMAGES = 't10k-images-idx3-ubyte.gz'
+  TEST_LABELS = 't10k-labels-idx1-ubyte.gz'
+  VALIDATION_SIZE = 5000
+  local_file = maybe_download(TRAIN_IMAGES, train_dir)
+  train_images = extract_images(local_file)
+  local_file = maybe_download(TRAIN_LABELS, train_dir)
+  train_labels = extract_labels(local_file, one_hot=one_hot)
+  local_file = maybe_download(TEST_IMAGES, train_dir)
+  test_images = extract_images(local_file)
+  local_file = maybe_download(TEST_LABELS, train_dir)
+  test_labels = extract_labels(local_file, one_hot=one_hot)
+  validation_images = train_images[:VALIDATION_SIZE]
+  validation_labels = train_labels[:VALIDATION_SIZE]
+  train_images = train_images[VALIDATION_SIZE:]
+  train_labels = train_labels[VALIDATION_SIZE:]
+  data_sets.train = DataSet(train_images, train_labels)
+  data_sets.validation = DataSet(validation_images, validation_labels)
+  data_sets.test = DataSet(test_images, test_labels)
+  return data_sets

multilayer_perceptron.py → examples/3 - Neural Networks/multilayer_perceptron.py


recurrent_network.py → examples/3 - Neural Networks/recurrent_network.py


+ 85 - 0
examples/4 - Multi GPU/multigpu_basics.py

@@ -0,0 +1,85 @@
+#Multi GPU Basic example
+'''
+This tutorial requires your machine to have 2 GPUs
+"/cpu:0": The CPU of your machine.
+"/gpu:0": The first GPU of your machine
+"/gpu:1": The second GPU of your machine
+'''
+
+import numpy as np
+import tensorflow as tf
+import datetime
+
+#Processing Units logs
+log_device_placement = True
+
+#num of multiplications to perform
+n = 10
+
+'''
+Example: compute A^n + B^n on 2 GPUs
+Results on 8 cores with 2 GTX-980:
+ * Single GPU computation time: 0:00:11.277449
+ * Multi GPU computation time: 0:00:07.131701
+'''
+#Create random large matrix
+A = np.random.rand(1e4, 1e4).astype('float32')
+B = np.random.rand(1e4, 1e4).astype('float32')
+
+# Creates a graph to store results
+c1 = []
+c2 = []
+
+def matpow(M, n):
+    if n < 1: #Abstract cases where n < 1
+        return M
+    else:
+        return tf.matmul(M, matpow(M, n-1))
+
+'''
+Single GPU computing
+'''
+with tf.device('/gpu:0'):
+    a = tf.constant(A)
+    b = tf.constant(B)
+    #compute A^n and B^n and store results in c1
+    c1.append(matpow(a, n))
+    c1.append(matpow(b, n))
+
+with tf.device('/cpu:0'):
+  sum = tf.add_n(c1) #Addition of all elements in c1, i.e. A^n + B^n
+
+t1_1 = datetime.datetime.now()
+with tf.Session(config=tf.ConfigProto(log_device_placement=log_device_placement)) as sess:
+    # Runs the op.
+    sess.run(sum)
+t2_1 = datetime.datetime.now()
+
+
+'''
+Multi GPU computing
+'''
+#GPU:0 computes A^n
+with tf.device('/gpu:0'):
+    #compute A^n and store result in c2
+    a = tf.constant(A)
+    c2.append(matpow(a, n))
+
+#GPU:1 computes B^n
+with tf.device('/gpu:1'):
+    #compute B^n and store result in c2
+    b = tf.constant(B)
+    c2.append(matpow(b, n))
+
+with tf.device('/cpu:0'):
+  sum = tf.add_n(c2) #Addition of all elements in c2, i.e. A^n + B^n
+
+t1_2 = datetime.datetime.now()
+with tf.Session(config=tf.ConfigProto(log_device_placement=log_device_placement)) as sess:
+    # Runs the op.
+    sess.run(sum)
+t2_2 = datetime.datetime.now()
+
+
+print "Single GPU computation time: " + str(t2_1-t1_1)
+print "Multi GPU computation time: " + str(t2_2-t1_2)

+ 230 - 0
notebooks/1 - Introduction/basic_operations.ipynb

@@ -0,0 +1,230 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "#Basic Operations example using TensorFlow library.\n",
+    "#Author: Aymeric Damien\n",
+    "#Project: https://github.com/aymericdamien/TensorFlow-Examples/"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "import tensorflow as tf"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Basic constant operations\n",
+    "# The value returned by the constructor represents the output\n",
+    "# of the Constant op.\n",
+    "a = tf.constant(2)\n",
+    "b = tf.constant(3)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "a=2, b=3\n",
+      "Addition with constants: 5\n",
+      "Multiplication with constants: 6\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Launch the default graph.\n",
+    "with tf.Session() as sess:\n",
+    "    print \"a=2, b=3\"\n",
+    "    print \"Addition with constants: %i\" % sess.run(a+b)\n",
+    "    print \"Multiplication with constants: %i\" % sess.run(a*b)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Basic Operations with variable as graph input\n",
+    "# The value returned by the constructor represents the output\n",
+    "# of the Variable op. (define as input when running session)\n",
+    "# tf Graph input\n",
+    "a = tf.placeholder(tf.types.int16)\n",
+    "b = tf.placeholder(tf.types.int16)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Define some operations\n",
+    "add = tf.add(a, b)\n",
+    "mul = tf.mul(a, b)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Addition with variables: 5\n",
+      "Multiplication with variables: 6\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Launch the default graph.\n",
+    "with tf.Session() as sess:\n",
+    "    # Run every operation with variable input\n",
+    "    print \"Addition with variables: %i\" % sess.run(add, feed_dict={a: 2, b: 3})\n",
+    "    print \"Multiplication with variables: %i\" % sess.run(mul, feed_dict={a: 2, b: 3})"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 8,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# ----------------\n",
+    "# More in details:\n",
+    "# Matrix Multiplication from TensorFlow official tutorial\n",
+    "\n",
+    "# Create a Constant op that produces a 1x2 matrix.  The op is\n",
+    "# added as a node to the default graph.\n",
+    "#\n",
+    "# The value returned by the constructor represents the output\n",
+    "# of the Constant op.\n",
+    "matrix1 = tf.constant([[3., 3.]])"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Create another Constant that produces a 2x1 matrix.\n",
+    "matrix2 = tf.constant([[2.],[2.]])"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.\n",
+    "# The returned value, 'product', represents the result of the matrix\n",
+    "# multiplication.\n",
+    "product = tf.matmul(matrix1, matrix2)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 11,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[[ 12.]]\n"
+     ]
+    }
+   ],
+   "source": [
+    "# To run the matmul op we call the session 'run()' method, passing 'product'\n",
+    "# which represents the output of the matmul op.  This indicates to the call\n",
+    "# that we want to get the output of the matmul op back.\n",
+    "#\n",
+    "# All inputs needed by the op are run automatically by the session.  They\n",
+    "# typically are run in parallel.\n",
+    "#\n",
+    "# The call 'run(product)' thus causes the execution of threes ops in the\n",
+    "# graph: the two constants and matmul.\n",
+    "#\n",
+    "# The output of the op is returned in 'result' as a numpy `ndarray` object.\n",
+    "with tf.Session() as sess:\n",
+    "    result = sess.run(product)\n",
+    "    print result\n",
+    "    # ==> [[ 12.]]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "IPython (Python 2.7)",
+   "language": "python",
+   "name": "python2"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 2
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython2",
+   "version": "2.7.8"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}

+ 96 - 0
notebooks/1 - Introduction/helloworld.ipynb

@@ -0,0 +1,96 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [],
+   "source": [
+    "import tensorflow as tf"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "#Simple hello world using TensorFlow\n",
+    "\n",
+    "# Create a Constant op\n",
+    "# The op is added as a node to the default graph.\n",
+    "#\n",
+    "# The value returned by the constructor represents the output\n",
+    "# of the Constant op.\n",
+    "\n",
+    "hello = tf.constant('Hello, TensorFlow!')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Start tf session\n",
+    "sess = tf.Session()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Hello, TensorFlow!\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Run graph\n",
+    "print sess.run(hello)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "IPython (Python 2.7)",
+   "language": "python",
+   "name": "python2"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 2
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython2",
+   "version": "2.7.8"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}

File diff suppressed because it is too large
+ 260 - 0
notebooks/2 - Basic Classifiers/linear_regression.ipynb


+ 234 - 0
notebooks/2 - Basic Classifiers/logistic_regression.ipynb

@@ -0,0 +1,234 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# A logistic regression learning algorithm example using TensorFlow library.\n",
+    "# This example is using the MNIST database of handwritten digits (http://yann.lecun.com/exdb/mnist/)\n",
+    "\n",
+    "# Author: Aymeric Damien\n",
+    "# Project: https://github.com/aymericdamien/TensorFlow-Examples/"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Extracting /tmp/data/train-images-idx3-ubyte.gz\n",
+      "Extracting /tmp/data/train-labels-idx1-ubyte.gz\n",
+      "Extracting /tmp/data/t10k-images-idx3-ubyte.gz\n",
+      "Extracting /tmp/data/t10k-labels-idx1-ubyte.gz\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Import MINST data\n",
+    "import input_data\n",
+    "mnist = input_data.read_data_sets(\"/tmp/data/\", one_hot=True)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "import tensorflow as tf"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Parameters\n",
+    "learning_rate = 0.01\n",
+    "training_epochs = 25\n",
+    "batch_size = 100\n",
+    "display_step = 1"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 8,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# tf Graph Input\n",
+    "x = tf.placeholder(\"float\", [None, 784]) # mnist data image of shape 28*28=784\n",
+    "y = tf.placeholder(\"float\", [None, 10]) # 0-9 digits recognition => 10 classes"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Create model\n",
+    "\n",
+    "# Set model weights\n",
+    "W = tf.Variable(tf.zeros([784, 10]))\n",
+    "b = tf.Variable(tf.zeros([10]))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Construct model\n",
+    "activation = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 11,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Minimize error using cross entropy\n",
+    "cost = -tf.reduce_sum(y*tf.log(activation)) # Cross entropy\n",
+    "optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) # Gradient Descent"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 12,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Initializing the variables\n",
+    "init = tf.initialize_all_variables()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 13,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Epoch: 0001 cost= 29.860479714\n",
+      "Epoch: 0002 cost= 22.080549484\n",
+      "Epoch: 0003 cost= 21.237104595\n",
+      "Epoch: 0004 cost= 20.460196280\n",
+      "Epoch: 0005 cost= 20.185128237\n",
+      "Epoch: 0006 cost= 19.940297202\n",
+      "Epoch: 0007 cost= 19.645111119\n",
+      "Epoch: 0008 cost= 19.507218031\n",
+      "Epoch: 0009 cost= 19.389794492\n",
+      "Epoch: 0010 cost= 19.177005816\n",
+      "Epoch: 0011 cost= 19.082493615\n",
+      "Epoch: 0012 cost= 19.072873598\n",
+      "Epoch: 0013 cost= 18.938005402\n",
+      "Epoch: 0014 cost= 18.891806430\n",
+      "Epoch: 0015 cost= 18.839480221\n",
+      "Epoch: 0016 cost= 18.769349510\n",
+      "Epoch: 0017 cost= 18.590865587\n",
+      "Epoch: 0018 cost= 18.623413677\n",
+      "Epoch: 0019 cost= 18.546149085\n",
+      "Epoch: 0020 cost= 18.432274895\n",
+      "Epoch: 0021 cost= 18.358189004\n",
+      "Epoch: 0022 cost= 18.380014628\n",
+      "Epoch: 0023 cost= 18.499993471\n",
+      "Epoch: 0024 cost= 18.386477311\n",
+      "Epoch: 0025 cost= 18.258080609\n",
+      "Optimization Finished!\n",
+      "Accuracy: 0.9048\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Launch the graph\n",
+    "with tf.Session() as sess:\n",
+    "    sess.run(init)\n",
+    "\n",
+    "    # Training cycle\n",
+    "    for epoch in range(training_epochs):\n",
+    "        avg_cost = 0.\n",
+    "        total_batch = int(mnist.train.num_examples/batch_size)\n",
+    "        # Loop over all batches\n",
+    "        for i in range(total_batch):\n",
+    "            batch_xs, batch_ys = mnist.train.next_batch(batch_size)\n",
+    "            # Fit training using batch data\n",
+    "            sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys})\n",
+    "            # Compute average loss\n",
+    "            avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})/total_batch\n",
+    "        # Display logs per epoch step\n",
+    "        if epoch % display_step == 0:\n",
+    "            print \"Epoch:\", '%04d' % (epoch+1), \"cost=\", \"{:.9f}\".format(avg_cost)\n",
+    "\n",
+    "    print \"Optimization Finished!\"\n",
+    "\n",
+    "    # Test model\n",
+    "    correct_prediction = tf.equal(tf.argmax(activation, 1), tf.argmax(y, 1))\n",
+    "    # Calculate accuracy\n",
+    "    accuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\n",
+    "    print \"Accuracy:\", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "IPython (Python 2.7)",
+   "language": "python",
+   "name": "python2"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 2
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython2",
+   "version": "2.7.8"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}

+ 387 - 0
notebooks/2 - Basic Classifiers/nearest_neighbor.ipynb

@@ -0,0 +1,387 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# A nearest neighbor learning algorithm example using TensorFlow library.\n",
+    "# This example is using the MNIST database of handwritten digits (http://yann.lecun.com/exdb/mnist/)\n",
+    "\n",
+    "# Author: Aymeric Damien\n",
+    "# Project: https://github.com/aymericdamien/TensorFlow-Examples/"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "import numpy as np\n",
+    "import tensorflow as tf"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Extracting /tmp/data/train-images-idx3-ubyte.gz\n",
+      "Extracting /tmp/data/train-labels-idx1-ubyte.gz\n",
+      "Extracting /tmp/data/t10k-images-idx3-ubyte.gz\n",
+      "Extracting /tmp/data/t10k-labels-idx1-ubyte.gz\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Import MINST data\n",
+    "import input_data\n",
+    "mnist = input_data.read_data_sets(\"/tmp/data/\", one_hot=True)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# In this example, we limit mnist data\n",
+    "Xtr, Ytr = mnist.train.next_batch(5000) #5000 for training (nn candidates)\n",
+    "Xte, Yte = mnist.test.next_batch(200) #200 for testing"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Reshape images to 1D\n",
+    "Xtr = np.reshape(Xtr, newshape=(-1, 28*28))\n",
+    "Xte = np.reshape(Xte, newshape=(-1, 28*28))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# tf Graph Input\n",
+    "xtr = tf.placeholder(\"float\", [None, 784])\n",
+    "xte = tf.placeholder(\"float\", [784])"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 8,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Nearest Neighbor calculation using L1 Distance\n",
+    "# Calculate L1 Distance\n",
+    "distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.neg(xte))), reduction_indices=1)\n",
+    "# Predict: Get min distance index (Nearest neighbor)\n",
+    "pred = tf.arg_min(distance, 0)\n",
+    "\n",
+    "accuracy = 0."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Initializing the variables\n",
+    "init = tf.initialize_all_variables()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Test 0 Prediction: 7 True Class: 7\n",
+      "Test 1 Prediction: 2 True Class: 2\n",
+      "Test 2 Prediction: 1 True Class: 1\n",
+      "Test 3 Prediction: 0 True Class: 0\n",
+      "Test 4 Prediction: 4 True Class: 4\n",
+      "Test 5 Prediction: 1 True Class: 1\n",
+      "Test 6 Prediction: 4 True Class: 4\n",
+      "Test 7 Prediction: 9 True Class: 9\n",
+      "Test 8 Prediction: 8 True Class: 5\n",
+      "Test 9 Prediction: 9 True Class: 9\n",
+      "Test 10 Prediction: 0 True Class: 0\n",
+      "Test 11 Prediction: 0 True Class: 6\n",
+      "Test 12 Prediction: 9 True Class: 9\n",
+      "Test 13 Prediction: 0 True Class: 0\n",
+      "Test 14 Prediction: 1 True Class: 1\n",
+      "Test 15 Prediction: 5 True Class: 5\n",
+      "Test 16 Prediction: 4 True Class: 9\n",
+      "Test 17 Prediction: 7 True Class: 7\n",
+      "Test 18 Prediction: 3 True Class: 3\n",
+      "Test 19 Prediction: 4 True Class: 4\n",
+      "Test 20 Prediction: 9 True Class: 9\n",
+      "Test 21 Prediction: 6 True Class: 6\n",
+      "Test 22 Prediction: 6 True Class: 6\n",
+      "Test 23 Prediction: 5 True Class: 5\n",
+      "Test 24 Prediction: 4 True Class: 4\n",
+      "Test 25 Prediction: 0 True Class: 0\n",
+      "Test 26 Prediction: 7 True Class: 7\n",
+      "Test 27 Prediction: 4 True Class: 4\n",
+      "Test 28 Prediction: 0 True Class: 0\n",
+      "Test 29 Prediction: 1 True Class: 1\n",
+      "Test 30 Prediction: 3 True Class: 3\n",
+      "Test 31 Prediction: 1 True Class: 1\n",
+      "Test 32 Prediction: 3 True Class: 3\n",
+      "Test 33 Prediction: 4 True Class: 4\n",
+      "Test 34 Prediction: 7 True Class: 7\n",
+      "Test 35 Prediction: 2 True Class: 2\n",
+      "Test 36 Prediction: 7 True Class: 7\n",
+      "Test 37 Prediction: 1 True Class: 1\n",
+      "Test 38 Prediction: 2 True Class: 2\n",
+      "Test 39 Prediction: 1 True Class: 1\n",
+      "Test 40 Prediction: 1 True Class: 1\n",
+      "Test 41 Prediction: 7 True Class: 7\n",
+      "Test 42 Prediction: 4 True Class: 4\n",
+      "Test 43 Prediction: 1 True Class: 2\n",
+      "Test 44 Prediction: 3 True Class: 3\n",
+      "Test 45 Prediction: 5 True Class: 5\n",
+      "Test 46 Prediction: 1 True Class: 1\n",
+      "Test 47 Prediction: 2 True Class: 2\n",
+      "Test 48 Prediction: 4 True Class: 4\n",
+      "Test 49 Prediction: 4 True Class: 4\n",
+      "Test 50 Prediction: 6 True Class: 6\n",
+      "Test 51 Prediction: 3 True Class: 3\n",
+      "Test 52 Prediction: 5 True Class: 5\n",
+      "Test 53 Prediction: 5 True Class: 5\n",
+      "Test 54 Prediction: 6 True Class: 6\n",
+      "Test 55 Prediction: 0 True Class: 0\n",
+      "Test 56 Prediction: 4 True Class: 4\n",
+      "Test 57 Prediction: 1 True Class: 1\n",
+      "Test 58 Prediction: 9 True Class: 9\n",
+      "Test 59 Prediction: 5 True Class: 5\n",
+      "Test 60 Prediction: 7 True Class: 7\n",
+      "Test 61 Prediction: 8 True Class: 8\n",
+      "Test 62 Prediction: 9 True Class: 9\n",
+      "Test 63 Prediction: 3 True Class: 3\n",
+      "Test 64 Prediction: 7 True Class: 7\n",
+      "Test 65 Prediction: 4 True Class: 4\n",
+      "Test 66 Prediction: 6 True Class: 6\n",
+      "Test 67 Prediction: 4 True Class: 4\n",
+      "Test 68 Prediction: 3 True Class: 3\n",
+      "Test 69 Prediction: 0 True Class: 0\n",
+      "Test 70 Prediction: 7 True Class: 7\n",
+      "Test 71 Prediction: 0 True Class: 0\n",
+      "Test 72 Prediction: 2 True Class: 2\n",
+      "Test 73 Prediction: 7 True Class: 9\n",
+      "Test 74 Prediction: 1 True Class: 1\n",
+      "Test 75 Prediction: 7 True Class: 7\n",
+      "Test 76 Prediction: 3 True Class: 3\n",
+      "Test 77 Prediction: 7 True Class: 2\n",
+      "Test 78 Prediction: 9 True Class: 9\n",
+      "Test 79 Prediction: 7 True Class: 7\n",
+      "Test 80 Prediction: 7 True Class: 7\n",
+      "Test 81 Prediction: 6 True Class: 6\n",
+      "Test 82 Prediction: 2 True Class: 2\n",
+      "Test 83 Prediction: 7 True Class: 7\n",
+      "Test 84 Prediction: 8 True Class: 8\n",
+      "Test 85 Prediction: 4 True Class: 4\n",
+      "Test 86 Prediction: 7 True Class: 7\n",
+      "Test 87 Prediction: 3 True Class: 3\n",
+      "Test 88 Prediction: 6 True Class: 6\n",
+      "Test 89 Prediction: 1 True Class: 1\n",
+      "Test 90 Prediction: 3 True Class: 3\n",
+      "Test 91 Prediction: 6 True Class: 6\n",
+      "Test 92 Prediction: 9 True Class: 9\n",
+      "Test 93 Prediction: 3 True Class: 3\n",
+      "Test 94 Prediction: 1 True Class: 1\n",
+      "Test 95 Prediction: 4 True Class: 4\n",
+      "Test 96 Prediction: 1 True Class: 1\n",
+      "Test 97 Prediction: 7 True Class: 7\n",
+      "Test 98 Prediction: 6 True Class: 6\n",
+      "Test 99 Prediction: 9 True Class: 9\n",
+      "Test 100 Prediction: 6 True Class: 6\n",
+      "Test 101 Prediction: 0 True Class: 0\n",
+      "Test 102 Prediction: 5 True Class: 5\n",
+      "Test 103 Prediction: 4 True Class: 4\n",
+      "Test 104 Prediction: 9 True Class: 9\n",
+      "Test 105 Prediction: 9 True Class: 9\n",
+      "Test 106 Prediction: 2 True Class: 2\n",
+      "Test 107 Prediction: 1 True Class: 1\n",
+      "Test 108 Prediction: 9 True Class: 9\n",
+      "Test 109 Prediction: 4 True Class: 4\n",
+      "Test 110 Prediction: 8 True Class: 8\n",
+      "Test 111 Prediction: 7 True Class: 7\n",
+      "Test 112 Prediction: 3 True Class: 3\n",
+      "Test 113 Prediction: 9 True Class: 9\n",
+      "Test 114 Prediction: 7 True Class: 7\n",
+      "Test 115 Prediction: 9 True Class: 4\n",
+      "Test 116 Prediction: 9 True Class: 4\n",
+      "Test 117 Prediction: 4 True Class: 4\n",
+      "Test 118 Prediction: 9 True Class: 9\n",
+      "Test 119 Prediction: 7 True Class: 2\n",
+      "Test 120 Prediction: 5 True Class: 5\n",
+      "Test 121 Prediction: 4 True Class: 4\n",
+      "Test 122 Prediction: 7 True Class: 7\n",
+      "Test 123 Prediction: 6 True Class: 6\n",
+      "Test 124 Prediction: 7 True Class: 7\n",
+      "Test 125 Prediction: 9 True Class: 9\n",
+      "Test 126 Prediction: 0 True Class: 0\n",
+      "Test 127 Prediction: 5 True Class: 5\n",
+      "Test 128 Prediction: 8 True Class: 8\n",
+      "Test 129 Prediction: 5 True Class: 5\n",
+      "Test 130 Prediction: 6 True Class: 6\n",
+      "Test 131 Prediction: 6 True Class: 6\n",
+      "Test 132 Prediction: 5 True Class: 5\n",
+      "Test 133 Prediction: 7 True Class: 7\n",
+      "Test 134 Prediction: 8 True Class: 8\n",
+      "Test 135 Prediction: 1 True Class: 1\n",
+      "Test 136 Prediction: 0 True Class: 0\n",
+      "Test 137 Prediction: 1 True Class: 1\n",
+      "Test 138 Prediction: 6 True Class: 6\n",
+      "Test 139 Prediction: 4 True Class: 4\n",
+      "Test 140 Prediction: 6 True Class: 6\n",
+      "Test 141 Prediction: 7 True Class: 7\n",
+      "Test 142 Prediction: 2 True Class: 3\n",
+      "Test 143 Prediction: 1 True Class: 1\n",
+      "Test 144 Prediction: 7 True Class: 7\n",
+      "Test 145 Prediction: 1 True Class: 1\n",
+      "Test 146 Prediction: 8 True Class: 8\n",
+      "Test 147 Prediction: 2 True Class: 2\n",
+      "Test 148 Prediction: 0 True Class: 0\n",
+      "Test 149 Prediction: 1 True Class: 2\n",
+      "Test 150 Prediction: 9 True Class: 9\n",
+      "Test 151 Prediction: 9 True Class: 9\n",
+      "Test 152 Prediction: 5 True Class: 5\n",
+      "Test 153 Prediction: 5 True Class: 5\n",
+      "Test 154 Prediction: 1 True Class: 1\n",
+      "Test 155 Prediction: 5 True Class: 5\n",
+      "Test 156 Prediction: 6 True Class: 6\n",
+      "Test 157 Prediction: 0 True Class: 0\n",
+      "Test 158 Prediction: 3 True Class: 3\n",
+      "Test 159 Prediction: 4 True Class: 4\n",
+      "Test 160 Prediction: 4 True Class: 4\n",
+      "Test 161 Prediction: 6 True Class: 6\n",
+      "Test 162 Prediction: 5 True Class: 5\n",
+      "Test 163 Prediction: 4 True Class: 4\n",
+      "Test 164 Prediction: 6 True Class: 6\n",
+      "Test 165 Prediction: 5 True Class: 5\n",
+      "Test 166 Prediction: 4 True Class: 4\n",
+      "Test 167 Prediction: 5 True Class: 5\n",
+      "Test 168 Prediction: 1 True Class: 1\n",
+      "Test 169 Prediction: 4 True Class: 4\n",
+      "Test 170 Prediction: 9 True Class: 4\n",
+      "Test 171 Prediction: 7 True Class: 7\n",
+      "Test 172 Prediction: 2 True Class: 2\n",
+      "Test 173 Prediction: 3 True Class: 3\n",
+      "Test 174 Prediction: 2 True Class: 2\n",
+      "Test 175 Prediction: 1 True Class: 7\n",
+      "Test 176 Prediction: 1 True Class: 1\n",
+      "Test 177 Prediction: 8 True Class: 8\n",
+      "Test 178 Prediction: 1 True Class: 1\n",
+      "Test 179 Prediction: 8 True Class: 8\n",
+      "Test 180 Prediction: 1 True Class: 1\n",
+      "Test 181 Prediction: 8 True Class: 8\n",
+      "Test 182 Prediction: 5 True Class: 5\n",
+      "Test 183 Prediction: 0 True Class: 0\n",
+      "Test 184 Prediction: 2 True Class: 8\n",
+      "Test 185 Prediction: 9 True Class: 9\n",
+      "Test 186 Prediction: 2 True Class: 2\n",
+      "Test 187 Prediction: 5 True Class: 5\n",
+      "Test 188 Prediction: 0 True Class: 0\n",
+      "Test 189 Prediction: 1 True Class: 1\n",
+      "Test 190 Prediction: 1 True Class: 1\n",
+      "Test 191 Prediction: 1 True Class: 1\n",
+      "Test 192 Prediction: 0 True Class: 0\n",
+      "Test 193 Prediction: 4 True Class: 9\n",
+      "Test 194 Prediction: 0 True Class: 0\n",
+      "Test 195 Prediction: 1 True Class: 3\n",
+      "Test 196 Prediction: 1 True Class: 1\n",
+      "Test 197 Prediction: 6 True Class: 6\n",
+      "Test 198 Prediction: 4 True Class: 4\n",
+      "Test 199 Prediction: 2 True Class: 2\n",
+      "Done!\n",
+      "Accuracy: 0.92\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Launch the graph\n",
+    "with tf.Session() as sess:\n",
+    "    sess.run(init)\n",
+    "\n",
+    "    # loop over test data\n",
+    "    for i in range(len(Xte)):\n",
+    "        # Get nearest neighbor\n",
+    "        nn_index = sess.run(pred, feed_dict={xtr: Xtr, xte: Xte[i,:]})\n",
+    "        # Get nearest neighbor class label and compare it to its true label\n",
+    "        print \"Test\", i, \"Prediction:\", np.argmax(Ytr[nn_index]), \"True Class:\", np.argmax(Yte[i])\n",
+    "        # Calculate accuracy\n",
+    "        if np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]):\n",
+    "            accuracy += 1./len(Xte)\n",
+    "    print \"Done!\"\n",
+    "    print \"Accuracy:\", accuracy"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "IPython (Python 2.7)",
+   "language": "python",
+   "name": "python2"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 2
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython2",
+   "version": "2.7.8"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}

+ 260 - 0
notebooks/3 - Neural Networks/multilayer_perceptron.ipynb

@@ -0,0 +1,260 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# A Multilayer Perceptron implementation example using TensorFlow library.\n",
+    "# This example is using the MNIST database of handwritten digits (http://yann.lecun.com/exdb/mnist/)\n",
+    "\n",
+    "# Author: Aymeric Damien\n",
+    "# Project: https://github.com/aymericdamien/TensorFlow-Examples/"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Extracting /tmp/data/train-images-idx3-ubyte.gz\n",
+      "Extracting /tmp/data/train-labels-idx1-ubyte.gz\n",
+      "Extracting /tmp/data/t10k-images-idx3-ubyte.gz\n",
+      "Extracting /tmp/data/t10k-labels-idx1-ubyte.gz\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Import MINST data\n",
+    "import input_data\n",
+    "mnist = input_data.read_data_sets(\"/tmp/data/\", one_hot=True)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "import tensorflow as tf"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Parameters\n",
+    "learning_rate = 0.001\n",
+    "training_epochs = 15\n",
+    "batch_size = 100\n",
+    "display_step = 1"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Network Parameters\n",
+    "n_hidden_1 = 256 # 1st layer num features\n",
+    "n_hidden_2 = 256 # 2nd layer num features\n",
+    "n_input = 784 # MNIST data input (img shape: 28*28)\n",
+    "n_classes = 10 # MNIST total classes (0-9 digits)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# tf Graph input\n",
+    "x = tf.placeholder(\"float\", [None, n_input])\n",
+    "y = tf.placeholder(\"float\", [None, n_classes])"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Create model\n",
+    "def multilayer_perceptron(_X, _weights, _biases):\n",
+    "    layer_1 = tf.nn.relu(tf.add(tf.matmul(_X, _weights['h1']), _biases['b1'])) #Hidden layer with RELU activation\n",
+    "    layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1, _weights['h2']), _biases['b2'])) #Hidden layer with RELU activation\n",
+    "    return tf.matmul(layer_2, weights['out']) + biases['out']"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 8,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Store layers weight & bias\n",
+    "weights = {\n",
+    "    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),\n",
+    "    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),\n",
+    "    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))\n",
+    "}\n",
+    "biases = {\n",
+    "    'b1': tf.Variable(tf.random_normal([n_hidden_1])),\n",
+    "    'b2': tf.Variable(tf.random_normal([n_hidden_2])),\n",
+    "    'out': tf.Variable(tf.random_normal([n_classes]))\n",
+    "}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Construct model\n",
+    "pred = multilayer_perceptron(x, weights, biases)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Define loss and optimizer\n",
+    "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y)) # Softmax loss\n",
+    "optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) # Adam Optimizer"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 11,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": [
+    "# Initializing the variables\n",
+    "init = tf.initialize_all_variables()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 12,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Epoch: 0001 cost= 160.113980416\n",
+      "Epoch: 0002 cost= 38.665780694\n",
+      "Epoch: 0003 cost= 24.118004577\n",
+      "Epoch: 0004 cost= 16.440921303\n",
+      "Epoch: 0005 cost= 11.689460141\n",
+      "Epoch: 0006 cost= 8.469423468\n",
+      "Epoch: 0007 cost= 6.223237230\n",
+      "Epoch: 0008 cost= 4.560174118\n",
+      "Epoch: 0009 cost= 3.250516910\n",
+      "Epoch: 0010 cost= 2.359658795\n",
+      "Epoch: 0011 cost= 1.694081847\n",
+      "Epoch: 0012 cost= 1.167997509\n",
+      "Epoch: 0013 cost= 0.872986831\n",
+      "Epoch: 0014 cost= 0.630616366\n",
+      "Epoch: 0015 cost= 0.487381571\n",
+      "Optimization Finished!\n",
+      "Accuracy: 0.9462\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Launch the graph\n",
+    "with tf.Session() as sess:\n",
+    "    sess.run(init)\n",
+    "\n",
+    "    # Training cycle\n",
+    "    for epoch in range(training_epochs):\n",
+    "        avg_cost = 0.\n",
+    "        total_batch = int(mnist.train.num_examples/batch_size)\n",
+    "        # Loop over all batches\n",
+    "        for i in range(total_batch):\n",
+    "            batch_xs, batch_ys = mnist.train.next_batch(batch_size)\n",
+    "            # Fit training using batch data\n",
+    "            sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys})\n",
+    "            # Compute average loss\n",
+    "            avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})/total_batch\n",
+    "        # Display logs per epoch step\n",
+    "        if epoch % display_step == 0:\n",
+    "            print \"Epoch:\", '%04d' % (epoch+1), \"cost=\", \"{:.9f}\".format(avg_cost)\n",
+    "\n",
+    "    print \"Optimization Finished!\"\n",
+    "\n",
+    "    # Test model\n",
+    "    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))\n",
+    "    # Calculate accuracy\n",
+    "    accuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\n",
+    "    print \"Accuracy:\", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "IPython (Python 2.7)",
+   "language": "python",
+   "name": "python2"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 2
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython2",
+   "version": "2.7.8"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}

+ 0 - 0
tensorflow_setup.md