Last active
August 27, 2016 21:59
-
-
Save ilblackdragon/55b7a515b67ad59f53f1 to your computer and use it in GitHub Desktop.
Scikit Flow - Digits example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from sklearn import datasets, cross_validation, metrics | |
import tensorflow as tf | |
from tensorflow.contrib import layers | |
from tensorflow.contrib import learn | |
random.seed(42) | |
# Load dataset and split it into train / test subsets. | |
digits = datasets.load_digits() | |
X = digits.images | |
y = digits.target | |
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, | |
test_size=0.2, random_state=42) | |
# TensorFlow model using Scikit Flow ops | |
def conv_model(features, target): | |
target = tf.one_hot(target, 10, 1.0, 0.0) | |
features = tf.expand_dims(features, 3) | |
features = tf.reduce_max(layers.conv2d(features, 12, [3, 3]), [1, 2]) | |
features = tf.reshape(features, [-1, 12]) | |
prediction, loss = learn.models.logistic_regression(features, target) | |
train_op = layers.optimize_loss(loss, | |
tf.contrib.framework.get_global_step(), optimizer='SGD', | |
learning_rate=0.01) | |
return tf.argmax(prediction, dimension=1), loss, train_op | |
# Create a classifier, train and predict. | |
classifier = learn.Estimator(model_fn=conv_model) | |
classifier.fit(X_train, y_train, steps=1000, batch_size=128) | |
score = metrics.accuracy_score(classifier.predict(X_test), y_test) | |
print('Accuracy: %f' % score) |
@Waffleboy You are right - I'm always flipping y_hat and y_pred arguments.
I just updated code, but previous line 21 meant 2d convolution over input image (e.g. computes weighted average for each square of size 3x3) and then find max across dimensions 1 and 2 - so will be left with [batch_size,12] where for each example in the batch 12 numbers are maximum values of the average from before.
You can think of it as running 12 object detector (or 3x3 objects) across the image and then using their maximum value to determine if object was present or not.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, thanks for this! :)
if im not wrong, line 30 should be:
score = metrics.accuracy_score( y_test,classifier.predict(X_test))
Also, can you explain line 21? :)