In [1]:
import tensorflow as tf
import numpy as np
import scipy as sp
from scipy import ndimage
import matplotlib.pyplot as plt
import os
import sys
import cPickle as pickle
import sklearn.preprocessing as skproc
import tfhelpers as tfh
import random as rnd
In [2]:
# Functions
def next_batch(xs, ys, batch_size):
xs_shape = np.shape(xs)
# ys_shape = np.shape(ys)
idxs = rnd.sample(range(0,xs_shape[0]), batch_size)
xs_rand = xs[idxs, :]
ys_rand = ys[idxs,:]
return xs_rand, ys_rand
def load_all_data(file_str):
curr_dir = os.getcwd()
data_file = open(os.path.join(curr_dir, file_str))
nonMnist_all = pickle.load(data_file)
# Set up the one-hot encoding...
train_ys = nonMnist_all['train_labels'].reshape(-1,1)
test_ys = nonMnist_all['test_labels'].reshape(-1,1)
enc = skproc.OneHotEncoder()
enc.fit(test_ys)
train_data = {'x': nonMnist_all['train_dataset'], 'y' : enc.transform(train_ys)}
test_data = {'x' : nonMnist_all['test_dataset'], 'y' : enc.transform(test_ys)}
return train_data, test_data
def run_nonmnist():
print 'Running nonMNIST CNN task.'
print 'Load the nonMNIST data...'
train_data, test_data = load_all_data('notMNIST_sanitized.pickle')
print 'nonMNIST batch:'
print next_batch(train_data['x'], train_data['y'], 10)
return train_data, test_data
In [3]:
train_data, test_data = load_all_data('notMNIST_sanitized.pickle')
In [8]:
xs, ys = next_batch(train_data['x'], train_data['y'], 5)
np.shape(xs)
Out[8]:
In [13]:
# TF setup: the placeholders for the input data (28x28x3 image) and output (10x1 vector)
x = tf.placeholder(tf.float32, shape=[None,784])
x_img = tf.reshape(x,[-1,28,28,1])
y_targ = tf.placeholder(tf.float32, shape=[None,10])
sess = tf.Session()
In [14]:
# conv layer 1
Wc1 = tfh.weight_variable([5,5,1,32])
bc1 = tfh.bias_variable([32])
conv1 = tfh.conv2d(x_img, Wc1) + bc1
relu1 = tf.nn.relu(conv1)
pool1 = tfh.max_pool_2x2(relu1)
In [16]:
ximg = tf.placeholder(tf.float32, shape=[None,28,28,1])
In [17]:
In [ ]: