示例#1
0
def test_convert_to_one_hot():
    fake_str_int = [[1, 5, 7, 1, 6, 0], [2, 3, 6, 0]]

    n_classes = len(set(y))
    convert_to_one_hot(y, n_classes)
    convert_to_one_hot(fake_str_int, 8)
    assert_raises(ValueError, convert_to_one_hot, X[0], n_classes)
    assert_raises(ValueError, convert_to_one_hot, X, n_classes)
示例#2
0
def test_convert_to_one_hot():
    fake_str_int = [[1, 5, 7, 1, 6, 0], [2, 3, 6, 0]]

    n_classes = len(set(y))
    convert_to_one_hot(y, n_classes)
    convert_to_one_hot(fake_str_int, 8)
    assert_raises(ValueError, convert_to_one_hot, X[0], n_classes)
    assert_raises(ValueError, convert_to_one_hot, X, n_classes)
示例#3
0
from dagbldr.optimizers import rmsprop
from dagbldr.utils import add_datasets_to_graph, get_params_and_grads
from dagbldr.utils import get_weights_from_graph
from dagbldr.utils import convert_to_one_hot
from dagbldr.utils import TrainingLoop
from dagbldr.utils import create_checkpoint_dict
from dagbldr.nodes import relu_layer, softmax_zeros_layer
from dagbldr.nodes import categorical_crossentropy

mnist = fetch_mnist()
train_indices = mnist["train_indices"]
valid_indices = mnist["valid_indices"]
X = mnist["data"]
y = mnist["target"]
n_targets = 10
y = convert_to_one_hot(y, n_targets)

# graph holds information necessary to build layers from parents
graph = OrderedDict()
X_sym, y_sym = add_datasets_to_graph([X, y], ["X", "y"], graph)
# random state so script is deterministic
random_state = np.random.RandomState(1999)

minibatch_size = 20
n_hid = 1000

l1 = relu_layer([X_sym],
                graph,
                'l1',
                proj_dim=n_hid,
                random_state=random_state)
示例#4
0
from dagbldr.utils import get_params_and_grads, make_embedding_minibatch
from dagbldr.nodes import fixed_projection_layer, embedding_layer
from dagbldr.nodes import projection_layer, linear_layer, softmax_layer
from dagbldr.nodes import softmax_zeros_layer, maxout_layer
from dagbldr.nodes import sigmoid_layer, tanh_layer, softplus_layer
from dagbldr.nodes import exp_layer, relu_layer, dropout_layer
from dagbldr.nodes import softmax_sample_layer, gaussian_sample_layer
from dagbldr.nodes import gaussian_log_sample_layer, conv2d_layer
from dagbldr.nodes import pool2d_layer

# Common between tests
digits = load_digits()
X = digits["data"]
y = digits["target"]
n_classes = len(set(y))
y = convert_to_one_hot(y, n_classes)


def run_common_layer(layer):
    random_state = np.random.RandomState(42)
    graph = OrderedDict()
    X_sym, y_sym = add_datasets_to_graph([X, y], ["X", "y"], graph)
    single_o = layer([X_sym], graph, 'single', proj_dim=5,
                     random_state=random_state)
    concat_o = layer([X_sym, y_sym], graph, 'concat', proj_dim=5,
                     random_state=random_state)
    # Check that things can be reused
    repeated_o = layer([X_sym], graph, 'single', strict=False)

    # Check that strict mode raises an error if repeated
    assert_raises(AttributeError, layer, [X_sym], graph, 'concat')
示例#5
0
import theano

from dagbldr.utils import add_datasets_to_graph, convert_to_one_hot
from dagbldr.utils import get_params_and_grads
from dagbldr.utils import early_stopping_trainer
from dagbldr.nodes import linear_layer, softmax_layer
from dagbldr.nodes import categorical_crossentropy
from dagbldr.optimizers import sgd
from dagbldr.datasets import load_digits

# Common between tests
digits = load_digits()
X = digits["data"]
y = digits["target"]
n_classes = len(set(y))
y = convert_to_one_hot(y, n_classes)


def test_feedforward_classifier():
    minibatch_size = 100
    random_state = np.random.RandomState(1999)
    graph = OrderedDict()

    X_sym, y_sym = add_datasets_to_graph([X, y], ["X", "y"], graph)

    l1_o = linear_layer([X_sym], graph, 'l1', proj_dim=20,
                        random_state=random_state)
    y_pred = softmax_layer([l1_o], graph, 'pred', n_classes,
                           random_state=random_state)

    cost = categorical_crossentropy(y_pred, y_sym).mean()
示例#6
0
f, axarr = plt.subplots(n_plot_samples, 2)
n_correct_to_show = n_plot_samples // 2
n_incorrect_to_show = n_plot_samples - n_correct_to_show

correct_ind = np.where(all_pred_y == y)[0]
incorrect_ind = np.where(all_pred_y != y)[0]
random_state.shuffle(correct_ind)
random_state.shuffle(incorrect_ind)
c = correct_ind[:n_correct_to_show]
i = incorrect_ind[:n_incorrect_to_show]

X_corr = X[c]
X_incorr = X[i]
X_stack = np.vstack((X_corr, X_incorr))
y_corr = convert_to_one_hot(y[c], n_classes)
y_incorr = convert_to_one_hot(y[i], n_classes)
y_stack = np.vstack((y_corr, y_incorr))

generated_X = gen_samples(X_stack, y_stack)
predicted_y = convert_to_one_hot(np.hstack((all_pred_y[c], all_pred_y[i])),
                                 n_classes=n_classes)

for n, (X_i, y_i, sx_i, sy_i) in enumerate(
    zip(np.dot(X_stack, pca_tf) + mean_norm, y_stack,
        generated_X, predicted_y)):
    axarr[n, 0].matshow(X_i.reshape(width, height), cmap="gray")
    axarr[n, 1].matshow(sx_i.reshape(width, height), cmap="gray")
    axarr[n, 0].axis('off')
    axarr[n, 1].axis('off')
示例#7
0
from dagbldr.datasets import minibatch_iterator
from dagbldr.utils import convert_to_one_hot
from dagbldr import get_params, del_shared
from dagbldr.nodes import linear, softmax

from dagbldr.nodes import categorical_crossentropy
from dagbldr.optimizers import sgd
from dagbldr.datasets import load_digits

# Common between tests
digits = load_digits()
X = digits["data"].astype("float32")
y = digits["target"]
n_classes = len(set(y))
y = convert_to_one_hot(y, n_classes).astype("float32")


def test_feedforward_classifier():
    del_shared()
    minibatch_size = 100
    random_state = np.random.RandomState(1999)
    X_sym = tensor.fmatrix()
    y_sym = tensor.fmatrix()

    l1_o = linear([X_sym], [X.shape[1]],
                  proj_dim=20,
                  name='l1',
                  random_state=random_state)
    y_pred = softmax([l1_o], [20],
                     proj_dim=n_classes,
示例#8
0
from dagbldr.nodes import masked_cost
import theano
import itertools
from collections import OrderedDict
import numpy as np


random_state = np.random.RandomState(1999)
graph = OrderedDict()
base_string = "cat"
true_strings = sorted(list(set(["".join(i) for i in [
    s for s in itertools.permutations(base_string)]])))
ocr = make_ocr(true_strings)
X = ocr["data"]
vocab = ocr["vocabulary"]
y = convert_to_one_hot(ocr["target"], n_classes=len(vocab)).astype(
    theano.config.floatX)
minibatch_size = mbs = 2
train_itr = minibatch_iterator([X, y], minibatch_size, make_mask=True, axis=1)
X_mb, X_mb_mask, y_mb, y_mb_mask = next(train_itr)
train_itr.reset()
valid_itr = minibatch_iterator([X, y], minibatch_size, make_mask=True, axis=1)
datasets_list = [X_mb, X_mb_mask, y_mb, y_mb_mask]
names_list = ["X", "X_mask", "y", "y_mask"]
X_sym, X_mask_sym, y_sym, y_mask_sym = add_datasets_to_graph(
    datasets_list, names_list, graph, list_of_test_values=datasets_list)

n_hid = 256
n_out = 8

h = location_attention_tanh_recurrent_layer(
    [X_sym], [y_sym], X_mask_sym, y_mask_sym, n_hid, graph, 'l1_att_rec',
示例#9
0
from dagbldr.utils import add_datasets_to_graph, get_params_and_grads
from dagbldr.utils import get_weights_from_graph
from dagbldr.utils import convert_to_one_hot
from dagbldr.utils import create_checkpoint_dict
from dagbldr.utils import TrainingLoop
from dagbldr.nodes import tanh_layer, softmax_zeros_layer
from dagbldr.nodes import categorical_crossentropy


mnist = fetch_mnist()
train_indices = mnist["train_indices"]
valid_indices = mnist["valid_indices"]
X = mnist["data"]
y = mnist["target"]
n_targets = 10
y = convert_to_one_hot(y, n_targets)

# graph holds information necessary to build layers from parents
graph = OrderedDict()
X_sym, y_sym = add_datasets_to_graph([X, y], ["X", "y"], graph)
# random state so script is deterministic
random_state = np.random.RandomState(1999)

minibatch_size = 20
n_hid = 1000

l1 = tanh_layer([X_sym], graph, 'l1', proj_dim=n_hid, random_state=random_state)
y_pred = softmax_zeros_layer([l1], graph, 'y_pred',  proj_dim=n_targets)
nll = categorical_crossentropy(y_pred, y_sym).mean()
weights = get_weights_from_graph(graph)
L2 = sum([(w ** 2).sum() for w in weights])
示例#10
0
from dagbldr.utils import get_weights
from dagbldr.utils import convert_to_one_hot
from dagbldr.utils import create_checkpoint_dict

from dagbldr.nodes import tanh, softmax_zeros
from dagbldr.nodes import categorical_crossentropy

from dagbldr.datasets import minibatch_iterator
from dagbldr.training import TrainingLoop
from dagbldr.optimizers import sgd

mnist = fetch_mnist()
X = mnist["data"].astype("float32")
y = mnist["target"]
n_targets = 10
y = convert_to_one_hot(y, n_targets).astype("float32")

X_sym = tensor.fmatrix()
y_sym = tensor.fmatrix()

# random state so script is deterministic
random_state = np.random.RandomState(1999)

minibatch_size = 20
n_hid = 500

l1 = tanh([X_sym], [X.shape[1]], proj_dim=n_hid, name='l1',
          random_state=random_state)
y_pred = softmax_zeros([l1], [n_hid], proj_dim=n_targets, name='y_pred')
nll = categorical_crossentropy(y_pred, y_sym).mean()
weights = get_weights(skip_regex=None).values()
data = mnist["data"]
target = mnist["target"]
X = data[valid_indices]
y = target[valid_indices]
n_classes = len(set(y))

# number of samples
n_plot_samples = 6
# MNIST dimensions
width = 28
height = 28
# Get random data samples
ind = np.arange(len(X))
random_state.shuffle(ind)
sample_X = X[ind[:n_plot_samples]]
sample_y = convert_to_one_hot(y[ind[:n_plot_samples]], n_classes=n_classes)


def gen_samples(X, y):
    mu, log_sig = encode_function(X)
    # No noise at test time - repeat y twice because y_pred is needed for Theano
    # But it is not used unless y_sym is all -1
    out, = decode_function(mu + np.exp(log_sig), y)
    return out

# VAE specific plotting
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

all_pred_y, = predict_function(X)
示例#12
0
data = mnist["data"]
target = mnist["target"]
X = data[valid_indices]
y = target[valid_indices]
n_classes = len(set(y))

# number of samples
n_plot_samples = 6
# MNIST dimensions
width = 28
height = 28
# Get random data samples
ind = np.arange(len(X))
random_state.shuffle(ind)
sample_X = X[ind[:n_plot_samples]]
sample_y = convert_to_one_hot(y[ind[:n_plot_samples]], n_classes=n_classes)


def gen_samples(X, y):
    mu, log_sig = encode_function(X)
    # No noise at test time - repeat y twice because y_pred is needed for Theano
    # But it is not used unless y_sym is all -1
    out, = decode_function(mu + np.exp(log_sig), y)
    return out


# VAE specific plotting
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
示例#13
0
import theano
from theano import tensor

from dagbldr.datasets import load_digits
from dagbldr.utils import convert_to_one_hot
from dagbldr.nodes import binary_crossentropy, binary_entropy
from dagbldr.nodes import categorical_crossentropy, abs_error
from dagbldr.nodes import squared_error, gaussian_error, log_gaussian_error
from dagbldr.nodes import masked_cost, gaussian_kl, gaussian_log_kl

# Common between tests
digits = load_digits()
X = digits["data"].astype("float32")
y = digits["target"]
n_classes = len(set(y))
y = convert_to_one_hot(y, n_classes).astype("float32")
X_sym = tensor.fmatrix()
y_sym = tensor.fmatrix()


def test_binary_crossentropy():
    cost = binary_crossentropy(.99 * X_sym, X_sym)
    theano.function([X_sym], cost, mode="FAST_COMPILE")


def test_binary_entropy():
    cost = binary_entropy(X_sym)
    theano.function([X_sym], cost, mode="FAST_COMPILE")


def test_categorical_crossentropy():