In [1]:
import sys
import urllib

from keras.layers import Dense, Flatten, Conv2D

from keras.models import Sequential, model_from_json
from keras.datasets import mnist
from keras.utils import to_categorical

from keras.callbacks import ModelCheckpoint, TensorBoard
from keras import optimizers

import numpy as np
from PIL import Image
from io import BytesIO

from studio import fs_tracker, model_util, magics


Using TensorFlow backend.

Set up learning parameters


In [2]:
batch_size = 128
no_epochs = 10
lr = 0.01

Define model


In [3]:
model = Sequential()
model.add(Flatten(input_shape=(28, 28, 1)))
model.add(Dense(128, activation='relu'))
model.add(Dense(128, activation='relu'))

model.add(Dense(10, activation='softmax'))
model.summary()
model_str = model.to_json()


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_1 (Flatten)          (None, 784)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               100480    
_________________________________________________________________
dense_2 (Dense)              (None, 128)               16512     
_________________________________________________________________
dense_3 (Dense)              (None, 10)                1290      
=================================================================
Total params: 118,282
Trainable params: 118,282
Non-trainable params: 0
_________________________________________________________________

Load data, train model


In [7]:
%%studio_run
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 28, 28, 1)
x_test = x_test.reshape(10000, 28, 28, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

# convert class vectors to binary class matrices
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)


model = model_from_json(model_str)

model.compile(loss='categorical_crossentropy', optimizer=optimizers.SGD(lr=lr),
              metrics=['accuracy'])

checkpointer = ModelCheckpoint(
    fs_tracker.get_model_directory() +
    '/checkpoint.{epoch:02d}-{val_loss:.2f}.hdf')

tbcallback = TensorBoard(log_dir=fs_tracker.get_tensorboard_dir(),
                         histogram_freq=0,
                         write_graph=True,
                         write_images=True)

training_stats = model.fit(
    x_train, y_train, validation_data=(
        x_test,
        y_test),
    epochs=no_epochs,
    callbacks=[checkpointer, tbcallback],
    batch_size=batch_size)

history = training_stats.history

# clean up large data to prevent it from being sent back
x_train = None
y_train = None
x_test = None
y_test = None


Running studio with experiment key e7c4cb52-de57-4b3b-b079-cce33d8689fb
Experiment progress can be viewed/shared at:
https://studio-sentient.appspot.com/experiment/e7c4cb52-de57-4b3b-b079-cce33d8689fb
studio-run --capture=/Users/peter.zhokhov/.studioml/experiments/e7c4cb52-de57-4b3b-b079-cce33d8689fb/_ns:_ns --force-git --experiment=e7c4cb52-de57-4b3b-b079-cce33d8689fb _script.py
Using TensorFlow backend.
2017-11-05 17:31:24.948767: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
60000/60000 [==============================] - 1s - loss: 1.3106 - acc: 0.6652 - val_loss: 0.6301 - val_acc: 0.8464
Epoch 2/10
60000/60000 [==============================] - 1s - loss: 0.5165 - acc: 0.8639 - val_loss: 0.4114 - val_acc: 0.8919
Epoch 3/10
60000/60000 [==============================] - 1s - loss: 0.3965 - acc: 0.8892 - val_loss: 0.3508 - val_acc: 0.9033
Epoch 4/10
60000/60000 [==============================] - 1s - loss: 0.3503 - acc: 0.9008 - val_loss: 0.3173 - val_acc: 0.9116
Epoch 5/10
60000/60000 [==============================] - 1s - loss: 0.3223 - acc: 0.9080 - val_loss: 0.2988 - val_acc: 0.9178
Epoch 6/10
60000/60000 [==============================] - 1s - loss: 0.3022 - acc: 0.9135 - val_loss: 0.2794 - val_acc: 0.9216
Epoch 7/10
60000/60000 [==============================] - 1s - loss: 0.2858 - acc: 0.9180 - val_loss: 0.2678 - val_acc: 0.9237
Epoch 8/10
60000/60000 [==============================] - 1s - loss: 0.2719 - acc: 0.9223 - val_loss: 0.2555 - val_acc: 0.9287
Epoch 9/10
60000/60000 [==============================] - 1s - loss: 0.2593 - acc: 0.9262 - val_loss: 0.2459 - val_acc: 0.9301
Epoch 10/10
60000/60000 [==============================] - 1s - loss: 0.2485 - acc: 0.9291 - val_loss: 0.2347 - val_acc: 0.9333

---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
_script.py in <module>()
----> 1 get_ipython().run_cell_magic(u'studio_run', u'', u"(x_train, y_train), (x_test, y_test) = mnist.load_data()\nx_train = x_train.reshape(60000, 28, 28, 1)\nx_test = x_test.reshape(10000, 28, 28, 1)\nx_train = x_train.astype('float32')\nx_test = x_test.astype('float32')\nx_train /= 255\nx_test /= 255\n\n# convert class vectors to binary class matrices\ny_train = to_categorical(y_train, 10)\ny_test = to_categorical(y_test, 10)\n\n\nmodel = model_from_json(model_str)\n\nmodel.compile(loss='categorical_crossentropy', optimizer=optimizers.SGD(lr=lr),\n              metrics=['accuracy'])\n\ncheckpointer = ModelCheckpoint(\n    fs_tracker.get_model_directory() +\n    '/checkpoint.{epoch:02d}-{val_loss:.2f}.hdf')\n\ntbcallback = TensorBoard(log_dir=fs_tracker.get_tensorboard_dir(),\n                         histogram_freq=0,\n                         write_graph=True,\n                         write_images=True)\n\ntraining_stats = model.fit(\n    x_train, y_train, validation_data=(\n        x_test,\n        y_test),\n    epochs=no_epochs,\n    callbacks=[checkpointer, tbcallback],\n    batch_size=batch_size)\n\nhistory = training_stats.history\n\n# clean up large data to prevent it from being sent back\nx_train = None\ny_train = None\nx_test = None\ny_test = None")

/Users/peter.zhokhov/studiosent/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
   2115             magic_arg_s = self.var_expand(line, stack_depth)
   2116             with self.builtin_trap:
-> 2117                 result = fn(magic_arg_s, cell)
   2118             return result
   2119 

<decorator-gen-123> in studio_run(self, line, cell)

/Users/peter.zhokhov/studiosent/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

/Users/peter.zhokhov/dev/sentient-studio/studio/magics.pyc in studio_run(self, line, cell)
    118         self.shell.user_ns.update(new_ns)
    119 
--> 120         studiorun_out, _ = p.communicate()
    121         if p.returncode != 0:
    122             print('studio-run returned code ' + str(p.returncode))

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.pyc in communicate(self, input)
    790             elif self.stdout:
    791                 stdout = _eintr_retry_call(self.stdout.read)
--> 792                 self.stdout.close()
    793             elif self.stderr:
    794                 stderr = _eintr_retry_call(self.stderr.read)

IOError: close() called during concurrent operation on the same file object.

Visualize results


In [8]:
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

In [9]:
plt.plot(range(no_epochs), history['acc'], range(no_epochs), history['val_acc'])


Out[9]:
[<matplotlib.lines.Line2D at 0x11e6fc990>,
 <matplotlib.lines.Line2D at 0x11e6fca50>]

In [ ]: