Пример #1
0
def test_mixture_density():
    # graph holds information necessary to build layers from parents
    random_state = np.random.RandomState(1999)
    graph = OrderedDict()
    X_sym, y_sym = add_datasets_to_graph([X, y], ["X", "y"], graph)
    n_hid = 20
    n_out = 1
    minibatch_size = len(X)
    train_indices = np.arange(len(X))
    valid_indices = np.arange(len(X))

    l1 = tanh_layer([X_sym], graph, 'l1', proj_dim=n_hid,
                    random_state=random_state)
    coeffs, mus, log_sigmas = log_gaussian_mixture_layer(
        [l1], graph, 'mdn', proj_dim=1, n_components=2,
        random_state=random_state)
    cost = log_gaussian_mixture_cost(coeffs, mus, log_sigmas, y_sym).mean()
    params, grads = get_params_and_grads(graph, cost)

    learning_rate = 1E-6
    opt = sgd(params, learning_rate)
    updates = opt.updates(params, grads)

    fit_function = theano.function([X_sym, y_sym], [cost], updates=updates,
                                   mode="FAST_COMPILE")
    cost_function = theano.function([X_sym, y_sym], [cost],
                                    mode="FAST_COMPILE")

    checkpoint_dict = create_checkpoint_dict(locals())

    epoch_results = fixed_n_epochs_trainer(
        fit_function, cost_function, train_indices, valid_indices,
        checkpoint_dict, [X, y],
        minibatch_size,
        list_of_train_output_names=["train_cost"],
        valid_output_name="valid_cost",
        n_epochs=1)
X = sine_x
y = sine_y

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

minibatch_size = len(sine_y) / 20
n_hid = 20
n_out = 1

l1 = tanh_layer([X_sym], graph, "l1", proj_dim=n_hid, random_state=random_state)
coeffs, mus, log_sigmas = log_gaussian_mixture_layer(
    [l1], graph, "mdn", proj_dim=1, n_components=24, random_state=random_state
)
cost = log_gaussian_mixture_cost(coeffs, mus, log_sigmas, y_sym).mean()
params, grads = get_params_and_grads(graph, cost)

opt = adadelta(params)
updates = opt.updates(params, grads)

fit_function = theano.function([X_sym, y_sym], [cost], updates=updates)
cost_function = theano.function([X_sym, y_sym], [cost])
predict_function = theano.function([X_sym], [coeffs, mus, log_sigmas])

checkpoint_dict = create_checkpoint_dict(locals())

train_itr = minibatch_iterator([X, y], minibatch_size, axis=0)
valid_itr = minibatch_iterator([X, y], minibatch_size, axis=0)
Пример #3
0
                                     list_of_test_values=[sine_x, sine_y])
# random state so script is deterministic
random_state = np.random.RandomState(1999)

minibatch_size = len(sine_y) / 20
n_hid = 20
n_out = 1

l1 = tanh_layer([X_sym],
                graph,
                'l1',
                proj_dim=n_hid,
                random_state=random_state)
coeffs, mus, log_sigmas = log_gaussian_mixture_layer([l1],
                                                     graph,
                                                     'mdn',
                                                     proj_dim=1,
                                                     n_components=24,
                                                     random_state=random_state)
cost = log_gaussian_mixture_cost(coeffs, mus, log_sigmas, y_sym).mean()
params, grads = get_params_and_grads(graph, cost)

opt = adadelta(params)
updates = opt.updates(params, grads)

fit_function = theano.function([X_sym, y_sym], [cost], updates=updates)
cost_function = theano.function([X_sym, y_sym], [cost])
predict_function = theano.function([X_sym], [coeffs, mus, log_sigmas])

checkpoint_dict = create_checkpoint_dict(locals())

train_itr = minibatch_iterator([X, y], minibatch_size, axis=0)