예제 #1
0
def get_mnist():
    """
    Load the MNIST data
    """
    mnist.init()
    x_train, t_train, x_test, t_test = mnist.load()
    print("Loaded MNIST data")
    return x_train, t_train, x_test, t_test
예제 #2
0
def init(digit1, digit2, num_train, num_test):
    # Download dataset
    if not os.path.isfile("mnist.pkl"):
        mnist.init()

    # Load whole dataset into memory
    x_train, t_train, x_test, t_test = mnist.load()

    # Subset training data
    if num_train > 0:
        indices1 = [
            i for i, j in enumerate(t_train)
            if ((j == digit1) or (j == digit2))
        ]
        x = x_train[indices1, :]
        y = t_train[indices1]
        y = np.cast[int](y)
        y[y == digit1] = -1
        y[y == digit2] = 1
        ind1 = np.random.choice(np.arange(y.size), num_train)
        x = x[ind1, :]
        y = y[ind1]
    else:
        x = None
        y = None

    # Subset test data
    if num_test > 0:
        indices2 = [
            i for i, j in enumerate(t_test) if ((j == digit1) or (j == digit2))
        ]
        xtest = x_test[indices2, :]
        ytest = t_test[indices2]
        ytest = np.cast[int](ytest)
        ytest[ytest == digit1] = -1
        ytest[ytest == digit2] = 1
        ind2 = np.random.choice(np.arange(ytest.size), num_test)
        xtest = xtest[ind2, :]
        ytest = ytest[ind2]
    else:
        xtest = None
        ytest = None

    # Return
    return (x, y, xtest, ytest)
예제 #3
0
def runner(params):
    backend = params.backend
    output = params.output

    print("Info: Initializing model")

    benchmark = mnist.init(backend)

    if backend == 'gpu':
        print("Info: Warming up GPU")
        benchmark.train()

    print("Info: Starting training benchmark")
    start = time.time()
    benchmark.train()
    end = time.time()
    print("Info: Finished training benchmark")

    train_time = (end - start) / EPOCHS
    print("Training time average: %s" % train_time)

    print("Info: Starting testing benchmark")
    start = time.time()
    benchmark.predict()
    end = time.time()
    print("Info: Finished testing benchmark")

    test_time = end - start

    print("Test time: %s" % str(test_time))

    data = {'benchmark': 'MNIST', 'backend': backend, 'implementation': 'Python', 'train': train_time,
            'test': test_time, 'train_size': TRAIN_SIZE, 'training_steps': EPOCHS, 'test_size': TEST_SIZE}
    print(json.dumps(data, separators=(',', ':')))

    file = open(output, "a+")
    file.write(json.dumps(data, separators=(',', ':')))
    file.write("\n")
예제 #4
0
        dataset, from a fake one, generated by a normal distribution.
    2) The generator creates digits from a normal distribution. Its output is
        given to the discriminator.
"""
import numpy as np
from sklearn import preprocessing
from matplotlib import pyplot as plt

from discriminator import Discriminator
from generator import Generator
from gan import Gan
import mnist
import params

if params.DOWNLOAD_MNIST:
    X_train, y_train, X_test, y_test = mnist.init()
else:
    X_train, y_train, X_test, y_test = mnist.load()


def get_normal_shaped_array(shape):
    """Returns a normal shaped array.

    The elements of the array follow a normal distribution and are between (0,1).

    Args:
        shape (tuple): The shape of the returned array.

    Returns:
        normal_shaped (np.array): Array with elements that are normal shaped.
    """
GRID_Y = 28

# size of the writing pad:
WIDTH = 280
HEIGHT = 280

# create the empty writing pad array to store the features (28x28 - 784) of one sample:
writing_pad = np.zeros((GRID_X, GRID_Y))

# flag to draw in the trackpad window:
drawing_active = False

# download (if not already) the MNIST samples and turn them into the numpy arrays according to this:
# https://github.com/hsjeong5/MNIST-for-Numpy/blob/master/README.md
if not os.path.exists("mnist.pkl"):
    mnist.init()

# x_train : 60,000x784 numpy array that each row contains flattened version of training images.
# t_train : 1x60,000 numpy array that each component is true label of the corresponding training images.
# x_test : 10,000x784 numpy array that each row contains flattened version of test images.
# t_test : 1x10,000 numpy array that each component is true label of the corresponding test images.
x_train, t_train, x_test, t_test = mnist.load()

# print a complete numpy array:
np.set_printoptions(threshold=np.inf)

# the x_test and x_train are grayscale images, not binary. I transform to binary:
threshold = 200
bin_x_test = 1.0 * (x_test > threshold)
bin_x_train = 1.0 * (x_train > threshold)
예제 #6
0
import mnist  # Mnist data set loader by Hyeonseok Jung (hsjeong5) https://github.com/hsjeong5/MNIST-for-Numpy
import ANN
import numpy as np

mnist.init()  # download data set and store as .pkl file

x_train, l_train, x_test, l_test = mnist.load()
n_train = 60000  # number of training examples (max: 60,000)
n_test = 10000  # number of testing examples (max: 10,000)

# Refine the data set
x_train = (
    x_train[:n_train] /
    (np.max(x_train) - np.min(x_train))).T  # Truncate to size and normalise
l_train = (l_train[:n_train]).T
x_test = (x_test[:n_test] / (np.max(x_test) - np.min(x_test))).T

layer_struct = [len(x_train), 30,
                10]  # [input_size,..hidden_layers..., output_size]

net = ANN.create(layer_struct)  # net
ANN.train(net, x_train, l_train, epochs=30, learning_rate=3, batchsize=10)

# returns output_nodes x n_test size array with all predicted examples
guess = ANN.test(net, x_test)
예제 #7
0
import numpy as np


# convert labels to one-hot encoding
def batch_make_onehot(batch_labels, nclasses):
    rslts = np.zeros(shape=[batch_labels.shape[0], nclasses])
    for i in range(batch_labels.shape[0]):
        rslts[i, :] = make_onehot(batch_labels[i], nclasses)
    return rslts


def make_onehot(label, nclasses):
    return np.eye(nclasses)[label]


dataset.init()
x_train, t_train, x_test, t_test = dataset.load()
t_train = batch_make_onehot(t_train, 10)
t_test = batch_make_onehot(t_test, 10)

np.save("data/dataset/mnist_train_data_60000x784.npy", x_train)
np.save("data/dataset/mnist_train_label_60000x10.npy", t_train)
np.save("data/dataset/mnist_test_data_10000x10.npy", x_test)
np.save("data/dataset/mnist_test_label_10000x10.npy", t_test)

# Parameters
learning_rate = 0.001
training_epochs = 5
batch_size = 100
display_step = 1