Commit 87e2f255 authored by kleswing's avatar kleswing
Browse files

MNIST notebook run

parent 372c5d01
Loading
Loading
Loading
Loading
+22 −146
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
from tensorflow.examples.tutorials.mnist import input_data
```

%% Cell type:code id: tags:

``` python
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
```

%% Output

    Extracting MNIST_data/train-images-idx3-ubyte.gz
    Extracting MNIST_data/train-labels-idx1-ubyte.gz
    Extracting MNIST_data/t10k-images-idx3-ubyte.gz
    Extracting MNIST_data/t10k-labels-idx1-ubyte.gz

%% Cell type:code id: tags:

``` python
import deepchem as dc
import tensorflow as tf
from deepchem.models.tensorgraph.layers import Layer, Input, Reshape, Flatten, Conv2D, Label, Feature
from deepchem.models.tensorgraph.layers import Dense, SoftMaxCrossEntropy, ReduceMean, SoftMax
```

%% Output

    /home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
    /home/kleswing/miniconda3/envs/j3udBScCYSIoPrpc/lib/python2.7/site-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
      "This module will be removed in 0.20.", DeprecationWarning)

%% Cell type:code id: tags:

``` python
train = dc.data.NumpyDataset(mnist.train.images, mnist.train.labels)
valid = dc.data.NumpyDataset(mnist.validation.images, mnist.validation.labels)
```

%% Cell type:code id: tags:

``` python
tg = dc.models.TensorGraph(tensorboard=True, model_dir='/tmp/mnist')
feature = Feature(shape=(None, 784))

# Images are square 28x28 (batch, height, width, channel)
make_image = Reshape(shape=(-1, 28, 28, 1), in_layers=[feature])

conv2d_1 = Conv2D(num_outputs=32, in_layers=[make_image])

conv2d_2 = Conv2D(num_outputs=64, in_layers=[conv2d_1])

flatten = Flatten(in_layers=[conv2d_2])

dense1 = Dense(out_channels=1024, activation_fn=tf.nn.relu, in_layers=[flatten])

dense2 = Dense(out_channels=10, in_layers=[dense1])

label = Label(shape=(None, 10))

smce = SoftMaxCrossEntropy(in_layers=[label, dense2])
loss = ReduceMean(in_layers=[smce])
tg.set_loss(loss)

output = SoftMax(in_layers=[dense2])
tg.add_output(output)
```

%% Cell type:code id: tags:

``` python
tg.fit(train, nb_epoch=10)
tg.save()
```

%% Output

    Exception in thread Thread-5:
    Traceback (most recent call last):
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/threading.py", line 801, in __bootstrap_inner
        self.run()
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/threading.py", line 754, in run
        self.__target(*self.__args, **self.__kwargs)
      File "/home/leswing/Documents/deepchem/deepchem/models/tensorgraph/tensor_graph.py", line 635, in _enqueue_batch
        sess.run(tg.input_queue.out_tensor, feed_dict=enq)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 767, in run
        run_metadata_ptr)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 965, in _run
        feed_dict_string, options, run_metadata)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1015, in _do_run
        target_list, options, run_metadata)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1035, in _do_call
        raise type(e)(node_def, op, message)
    CancelledError: Enqueue operation was cancelled
    	 [[Node: InputFifoQueue_12/fifo_queue_enqueue = QueueEnqueueV2[Tcomponents=[DT_FLOAT, DT_FLOAT], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](InputFifoQueue_12/fifo_queue, _recv_Feature_10_pre_q/Placeholder_0, _recv_Label_3_pre_q/Placeholder_0)]]
    
    Caused by op u'InputFifoQueue_12/fifo_queue_enqueue', defined at:
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/runpy.py", line 174, in _run_module_as_main
        "__main__", fname, loader, pkg_name)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/runpy.py", line 72, in _run_code
        exec code in run_globals
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/ipykernel_launcher.py", line 16, in <module>
        app.launch_new_instance()
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/traitlets/config/application.py", line 658, in launch_instance
        app.start()
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/ipykernel/kernelapp.py", line 477, in start
        ioloop.IOLoop.instance().start()
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/zmq/eventloop/ioloop.py", line 177, in start
        super(ZMQIOLoop, self).start()
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/tornado/ioloop.py", line 888, in start
        handler_func(fd_obj, events)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/tornado/stack_context.py", line 277, in null_wrapper
        return fn(*args, **kwargs)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
        self._handle_recv()
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
        self._run_callback(callback, msg)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
        callback(*args, **kwargs)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/tornado/stack_context.py", line 277, in null_wrapper
        return fn(*args, **kwargs)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
        return self.dispatch_shell(stream, msg)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
        handler(stream, idents, msg)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
        user_expressions, allow_stdin)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
        res = shell.run_cell(code, store_history=store_history, silent=silent)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
        return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2717, in run_cell
        interactivity=interactivity, compiler=compiler, result=result)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2821, in run_ast_nodes
        if self.run_code(code, result):
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
      File "<ipython-input-6-fa9f01cb3d14>", line 1, in <module>
        tg.fit(train, nb_epoch=1)
      File "/home/leswing/Documents/deepchem/deepchem/models/tensorgraph/tensor_graph.py", line 127, in fit
        max_checkpoints_to_keep, checkpoint_interval)
      File "/home/leswing/Documents/deepchem/deepchem/models/tensorgraph/tensor_graph.py", line 144, in fit_generator
        self.build()
      File "/home/leswing/Documents/deepchem/deepchem/models/tensorgraph/tensor_graph.py", line 374, in build
        node_layer.create_tensor(training=self._training_placeholder)
      File "/home/leswing/Documents/deepchem/deepchem/models/tensorgraph/layers.py", line 910, in create_tensor
        self.out_tensor = self.queue.enqueue(feed_dict)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/tensorflow/python/ops/data_flow_ops.py", line 322, in enqueue
        self._queue_ref, vals, name=scope)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/tensorflow/python/ops/gen_data_flow_ops.py", line 1569, in _queue_enqueue_v2
        name=name)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op
        op_def=op_def)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2327, in create_op
        original_op=self._default_original_op, op_def=op_def)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1226, in __init__
        self._traceback = _extract_stack()
    
    CancelledError (see above for traceback): Enqueue operation was cancelled
    	 [[Node: InputFifoQueue_12/fifo_queue_enqueue = QueueEnqueueV2[Tcomponents=[DT_FLOAT, DT_FLOAT], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](InputFifoQueue_12/fifo_queue, _recv_Feature_10_pre_q/Placeholder_0, _recv_Label_3_pre_q/Placeholder_0)]]
    
    
    ERROR:root:Internal Python error in the inspect module.
    Below is the traceback from this internal error.
    

    Traceback (most recent call last):
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/IPython/core/ultratb.py", line 1132, in get_records
        return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/IPython/core/ultratb.py", line 313, in wrapped
        return f(*args, **kwargs)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/IPython/core/ultratb.py", line 358, in _fixed_getinnerframes
        records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/inspect.py", line 1048, in getinnerframes
        framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/inspect.py", line 1008, in getframeinfo
        filename = getsourcefile(frame) or getfile(frame)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/inspect.py", line 453, in getsourcefile
        if hasattr(getmodule(object, filename), '__loader__'):
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/inspect.py", line 499, in getmodule
        os.path.realpath(f)] = module.__name__
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/posixpath.py", line 376, in realpath
        return abspath(path)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/posixpath.py", line 366, in abspath
        return normpath(path)
      File "/home/leswing/anaconda3/envs/deepchem27/lib/python2.7/posixpath.py", line 348, in normpath
        new_comps.append(comp)
    KeyboardInterrupt

    ---------------------------------------------------------------------------
    IndexError                                Traceback (most recent call last)
    /home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_code(self, code_obj, result)
       2896             if result is not None:
       2897                 result.error_in_exec = sys.exc_info()[1]
    -> 2898             self.showtraceback()
       2899         else:
       2900             outflag = 0
    /home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in showtraceback(self, exc_tuple, filename, tb_offset, exception_only)
       1822                     except Exception:
       1823                         stb = self.InteractiveTB.structured_traceback(etype,
    -> 1824                                             value, tb, tb_offset=tb_offset)
       1825
       1826                     self._showtraceback(etype, value, stb)
    /home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/IPython/core/ultratb.pyc in structured_traceback(self, etype, value, tb, tb_offset, number_of_lines_of_context)
       1410         self.tb = tb
       1411         return FormattedTB.structured_traceback(
    -> 1412             self, etype, value, tb, tb_offset, number_of_lines_of_context)
       1413
       1414
    /home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/IPython/core/ultratb.pyc in structured_traceback(self, etype, value, tb, tb_offset, number_of_lines_of_context)
       1318             # Verbose modes need a full traceback
       1319             return VerboseTB.structured_traceback(
    -> 1320                 self, etype, value, tb, tb_offset, number_of_lines_of_context
       1321             )
       1322         else:
    /home/leswing/anaconda3/envs/deepchem27/lib/python2.7/site-packages/IPython/core/ultratb.pyc in structured_traceback(self, etype, evalue, etb, tb_offset, number_of_lines_of_context)
       1202                 structured_traceback_parts += formatted_exception
       1203         else:
    -> 1204             structured_traceback_parts += formatted_exception[0]
       1205
       1206         return structured_traceback_parts
    IndexError: string index out of range
    Ending global_step 999: Average loss 0.0826627
    Ending global_step 1999: Average loss 0.0179067
    Ending global_step 2999: Average loss 0.0104273
    Ending global_step 3999: Average loss 0.00756823
    Ending global_step 4999: Average loss 0.00675081
    Ending global_step 5446: Average loss 0.00677046
    TIMING: model fitting took 91.544 s

%% Cell type:code id: tags:

``` python
from sklearn.metrics import roc_curve, auc
import numpy as np

print("Validation")
prediction = np.squeeze(tg.predict_on_batch(valid.X))

fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(10):
    fpr[i], tpr[i], thresh = roc_curve(valid.y[:, i], prediction[:, i])
    roc_auc[i] = auc(fpr[i], tpr[i])
    print("class %s:auc=%s" % (i, roc_auc[i]))
```

%% Output

    Validation
    class 0:auc=0.99998060547
    class 1:auc=0.999953963742
    class 2:auc=0.999864659633
    class 3:auc=0.99996714599
    class 4:auc=0.999773524087
    class 5:auc=0.999956097059
    class 6:auc=0.999952972472
    class 7:auc=0.99973442288
    class 8:auc=0.999880756822
    class 9:auc=0.999752015157

%% Cell type:code id: tags:

``` python
```

%% Cell type:code id: tags:

``` python
```