Exemplo n.º 1
0
def unit_vis(nnmodel):
    [X1, labels1] = utils.load_mnist(dataset='training',
                                     path='../examples/sda')
    x = X1
    net1 = layers.get_all_layers(nnmodel._network)[1]
    net2 = layers.get_all_layers(nnmodel._network)[2]
    res = linear_comb(net1, net2, x, 36).reshape(36, 28, 28)
    print res.shape
    displayz(res, 6, 6, 0, 28, 28, binary=True)
Exemplo n.º 2
0
def load_data(cp, train):
    log('Loading data........')
    # If 'input file' parameter not defined then assume MNIST dataset
    if cp.get('Experiment', 'inputfile') == '':
        # Get FULL dataset containing both training/testing
        # In this experiment MNIST used as a testing mechanism in order to
        # test the connection between layers, availabilty,etc. and not for
        # hyperparameter tweaking.
        [X1, labels1] = utils.load_mnist(
            dataset='training', path=MNIST_PATH)
        [X2, labels2] = utils.load_mnist(
            dataset='testing', path=MNIST_PATH)
        X = np.concatenate((X1, X2), axis=0)
        labels = np.concatenate((labels1, labels2), axis=0)
        # Will dataset be used for training? then shuffle the dataset then use
        # the same permutation for the labels.
        if train == 'train':
            p = np.random.permutation(X.shape[0])
            X = X[p].astype(np.float32) * 0.02
            labels = labels[p]
            prefix = cp.get('Experiment', 'prefix')
            num = cp.get('Experiment', 'num')
            np.save(prefix + '_' + num + 'random_perm.npy', p)
        return [X, labels]
    # If 'input file' is specified then load inputfile, our script assumes that
    # the input file will always be a numpy object
    else:
        try:
            X = np.load(cp.get('Experiment', 'inputfile'))
        except:
            log('Input file must be a saved numpy object (*.npy)')
        # Will dataset be used for training? then shuffle the dataset
        if train == 'train':
            p = np.random.permutation(X.shape[0])
            X = X[p]
            prefix = cp.get('Experiment', 'prefix')
            num = cp.get('Experiment', 'num')
            np.save(prefix + '_' + num + 'random_perm.npy', p)
        return X
    log('DONE........')
Exemplo n.º 3
0
                     filter_size=3))  # The output is of size N_obj 4 14 14
    nn.add(ReLU())  # The output is of size N_obj 4 14 14
    nn.add(MaxPool2x2())  # The output is of size N_obj 4 7 7

    nn.add(FlattenLayer())  # The output is of size N_obj 196
    nn.add(Dense(4 * 7 * 7, 32))
    nn.add(ReLU())
    nn.add(Dense(32, 1))
    return nn


nn = get_cnn()
loss = Hinge()
optimizer = SGD(nn)

train = list(load_mnist(dataset='training', path='.'))
train_images = np.array([im[1] for im in train])
train_targets = np.array([im[0] for im in train])
# We will train a 0 vs. 1 classifier
x_train = train_images[train_targets < 2][:1000]
y_train = train_targets[train_targets < 2][:1000]

y_train = y_train * 2 - 1
y_train = y_train.reshape((-1, 1))

x_train = x_train.astype('float32') / 255.0
x_train = x_train.reshape((-1, 1, 28, 28))

# It will train for about 5 minutes
num_epochs = 3
batch_size = 32
Exemplo n.º 4
0
import torchvision.datasets as dsets
from torch.autograd import Variable
from torch.utils.data import Dataset, DataLoader
from torchsummary import summary

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style

import os
os.chdir('/home/dimtsi/Dropbox/UvA/1st Semester/\
Applied Machine Learning/GitHub repo/UVA_AML18/week_3')

#==================== LOADING DATA ======================###
from dataset_utils import load_mnist
train = list(load_mnist(dataset='training', path='datasets'))
train_images = np.array([im[1] for im in train])
train_targets = np.array([im[0] for im in train])

x_train = train_images  #[train_targets < 2][:1024]
y_train = train_targets  #[train_targets < 2][:1024]
y_train = y_train.reshape((-1, 1))

test = list(load_mnist(dataset='testing', path='datasets'))
test_images = np.array([im[1] for im in test])
test_targets = np.array([im[0] for im in test])

x_test = test_images  #[test_targets < 2][:96]
y_test = test_targets  #[test_targets < 2][:96]
y_test = y_test.reshape((-1, 1))