[PYTHON] MNIST (DCNN) with skflow

MNIST with skflow

What is skflow

An interface for using TensorFlow as easily as Scikit Learn ~~ Included in TensorFlow (TF Learn) ~~ Independent of TensorFlow 0.9, it is easier to write than the older ones below (TFLearn)

Execution environment

EC2 (AWS) g2.2xlarge instance (Oregon = US West) Python 2.7.6 TensorFlow 0.8.0 scipy 0.17.1 (required for scikit-learn) scikit-learn 0.17.1

The AWS instance was initialized using another person's AMI, but if you want to introduce it yourself, refer to the following

It looks like you don't need to build from source anymore to run TensorFlow on an EC2 GPU instance?

Source code

This time, I tried it as a reference when building a complicated network with DCNN (Deep Convolutional Neural Network), so I think that the parameters are appropriate and not very for MNIST.

mnist.py


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from sklearn import metrics
import tensorflow as tf
from tensorflow.contrib import learn as skflow
from tensorflow.contrib.learn.python.learn.datasets import mnist as source

mnist = source.load_mnist()

def max_pool_2x2(tensor_in):
    return tf.nn.max_pool(tensor_in, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = 'SAME')

def my_model(X, y):
    X = tf.reshape(X, [-1, 28, 28, 1])

    with tf.variable_scope('layer1'):
        with tf.variable_scope('conv1'):
            h_conv1 = skflow.ops.conv2d(X, n_filters = 16, filter_shape = [3, 3], bias = True, activation = tf.nn.relu)
        with tf.variable_scope('conv2'):
            h_conv2 = skflow.ops.conv2d(h_conv1, n_filters = 32, filter_shape = [3, 3], bias = True, activation = tf.nn.relu)
            h_pool1 = max_pool_2x2(h_conv2)

    with tf.variable_scope('layer2'):
        with tf.variable_scope('conv3'):
            h_conv3 = skflow.ops.conv2d(h_pool1, n_filters = 64, filter_shape = [3, 3], bias = True, activation = tf.nn.relu)
        with tf.variable_scope('conv4'):
            h_conv4 = skflow.ops.conv2d(h_conv3, n_filters = 128, filter_shape = [3, 3], bias = True, activation = tf.nn.relu)
            h_pool2 = max_pool_2x2(h_conv4)
            h_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 128])

    h_fc = skflow.ops.dnn(h_flat, [1024, 1024], activation = tf.nn.relu, dropout = 0.5)
    return skflow.models.logistic_regression(h_fc, y)

classifier = skflow.TensorFlowEstimator(model_fn = my_model, n_classes = 10, batch_size = 100, steps = 20000, learning_rate = 0.001, optimizer = 'Adam')
classifier.fit(mnist.train.images, mnist.train.labels)
score = metrics.accuracy_score(mnist.test.labels, classifier.predict(mnist.test.images))
print('Accuracy: {0:f}'.format(score))

Referenced page

skflow - mnist.py Introduction to Scikit Flow TensorFlow Python reference documentation

Recommended Posts

MNIST (DCNN) with skflow
MNIST (DCNN) with Keras (TensorFlow backend)
Think about dropouts with MNIST
Try TensorFlow MNIST with RNN
Inference works with Chainer 2.0 MNIST sample
[Python] Introduction to CNN with Pytorch MNIST