In [1]:
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
from tensorflow.python import debug as tf_debug


WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/datasets/base.py:198: retry (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Use the retry module or similar alternatives.

In [2]:
epoch = 10
batch_size = 32
mnist = input_data.read_data_sets('', one_hot = True)
sess = tf.InteractiveSession()
    
def feed_dict(train):
    x, y = mnist.train.next_batch(batch_size)
    return {X: x, Y: y}
        
def convolutionize(x, conv_w, h = 1):
    return tf.nn.conv2d(input = x, filter = conv_w, strides = [1, h, h, 1], padding = 'SAME')

def pooling(wx):
    return tf.nn.max_pool(wx, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = 'SAME')
    
with tf.name_scope("input"):
    X = tf.placeholder(tf.float32, [None,28,28,1], name="x-input")
    Y = tf.placeholder(tf.float32, [None, 10], name="y-input")
    
with tf.name_scope("conv_1"):
    with tf.name_scope("weights"):
        w1 = tf.Variable(tf.random_normal([3, 3, 1, 16], stddev = 0.5))
    with tf.name_scope("biases"):
        b1 = tf.Variable(tf.zeros(shape = [16]))
    with tf.name_scope("activate"):
        conv1 = pooling(tf.nn.sigmoid(convolutionize(X, w1) + b1))
    
with tf.name_scope("conv_2"):
    with tf.name_scope("weights"):
        w2 = tf.Variable(tf.random_normal([3, 3, 16, 8], stddev = 0.5))
    with tf.name_scope("biases"):
        b2 = tf.Variable(tf.zeros(shape = [8]))
    with tf.name_scope("activate"):
        conv2 = pooling(tf.nn.sigmoid(convolutionize(conv1, w2) + b2))
            
with tf.name_scope("conv_3"):
    with tf.name_scope("weights"):
        w3 = tf.Variable(tf.random_normal([3, 3, 8, 8], stddev = 0.5))
    with tf.name_scope("biases"):
        b3 = tf.Variable(tf.zeros(shape = [8]))
    with tf.name_scope("activate"):
        conv3 = pooling(tf.nn.sigmoid(convolutionize(conv2, w3) + b3))
        conv3 = tf.reshape(conv3, [-1, 128])
            
with tf.name_scope("logits"):
    with tf.name_scope("weights"):
        w4 = tf.Variable(tf.random_normal([128, 10], stddev = 0.5))
    with tf.name_scope("biases"):
        b4 = tf.Variable(tf.zeros(shape = [10]))
    with tf.name_scope("activate"):
        logits = tf.matmul(conv3, w4) + b4
            
with tf.name_scope("cross_entropy"):
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = logits, labels = Y))
        
with tf.name_scope("train"):
    optimizer = tf.train.AdamOptimizer(1e-3).minimize(cost)
        
with tf.name_scope("accuracy"):
    with tf.name_scope("correct_prediction"):
        correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(logits, 1))
    with tf.name_scope("accuracy"):
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
            
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter('./logs', sess.graph)


WARNING:tensorflow:From <ipython-input-2-5257623b169d>:3: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting train-images-idx3-ubyte.gz
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting train-labels-idx1-ubyte.gz
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.one_hot on tensors.
Extracting t10k-images-idx3-ubyte.gz
Extracting t10k-labels-idx1-ubyte.gz
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From <ipython-input-2-5257623b169d>:54: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:

Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.

See tf.nn.softmax_cross_entropy_with_logits_v2.

Open your terminal and execute

tensorboard --logdir=./logs --port 6006 --debugger_port 6064

Open first tensorboard before run blocks below!


In [3]:
sess = tf_debug.TensorBoardDebugWrapperSession(sess,'localhost:6064')

In [4]:
for i in range(epoch):
    xs, ys = mnist.train.next_batch(batch_size)
    xs = xs.reshape((-1, 28, 28, 1))
    sess.run(optimizer, feed_dict = {X : xs, Y : ys})


39 ops no flops stats due to incomplete shapes.
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-4-567439d3b213> in <module>()
      2     xs, ys = mnist.train.next_batch(batch_size)
      3     xs = xs.reshape((-1, 28, 28, 1))
----> 4     sess.run(optimizer, feed_dict = {X : xs, Y : ys})

/usr/local/lib/python3.5/dist-packages/tensorflow/python/debug/wrappers/grpc_wrapper.py in run(self, fetches, feed_dict, options, run_metadata, callable_runner, callable_runner_args)
    230         run_metadata=run_metadata,
    231         callable_runner=callable_runner,
--> 232         callable_runner_args=callable_runner_args)

/usr/local/lib/python3.5/dist-packages/tensorflow/python/debug/wrappers/framework.py in run(self, fetches, feed_dict, options, run_metadata, callable_runner, callable_runner_args)
    491                                    feed_dict=feed_dict,
    492                                    options=decorated_run_options,
--> 493                                    run_metadata=run_metadata)
    494       except errors.OpError as op_error:
    495         if self._pass_through_operrors:

/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    903     try:
    904       result = self._run(None, fetches, feed_dict, options_ptr,
--> 905                          run_metadata_ptr)
    906       if run_metadata:
    907         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1138     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1139       results = self._do_run(handle, final_targets, final_fetches,
-> 1140                              feed_dict_tensor, options, run_metadata)
   1141     else:
   1142       results = []

/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1319     if handle is None:
   1320       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1321                            run_metadata)
   1322     else:
   1323       return self._do_call(_prun_fn, handle, feeds, fetches)

/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1325   def _do_call(self, fn, *args):
   1326     try:
-> 1327       return fn(*args)
   1328     except errors.OpError as e:
   1329       message = compat.as_text(e.message)

/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1310       self._extend_graph()
   1311       return self._call_tf_sessionrun(
-> 1312           options, feed_dict, fetch_list, target_list, run_metadata)
   1313 
   1314     def _prun_fn(handle, feed_dict, fetch_list):

/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1418         return tf_session.TF_Run(
   1419             self._session, options, feed_dict, fetch_list, target_list,
-> 1420             status, run_metadata)
   1421 
   1422   def _call_tf_sessionprun(self, handle, feed_dict, fetch_list):

KeyboardInterrupt: 

In [ ]: