def test_rnn_correlated_mixture_density(): # graph holds information necessary to build layers from parents random_state = np.random.RandomState(1999) graph = OrderedDict() minibatch_size = 5 X_seq = np.array([bernoulli_X for i in range(minibatch_size)]) y_seq = np.array([bernoulli_y for i in range(minibatch_size)]) X_mb, X_mb_mask = make_masked_minibatch(X_seq, slice(0, minibatch_size)) y_mb, y_mb_mask = make_masked_minibatch(y_seq, slice(0, minibatch_size)) 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) n_hid = 5 train_indices = np.arange(len(X_seq)) valid_indices = np.arange(len(X_seq)) l1 = tanh_layer([X_sym], graph, 'l1', proj_dim=n_hid, random_state=random_state) h = gru_recurrent_layer([l1], X_mask_sym, n_hid, graph, 'l1_rec', random_state=random_state) rval = bernoulli_and_correlated_log_gaussian_mixture_layer( [h], graph, 'hw', proj_dim=2, n_components=3, random_state=random_state) binary, coeffs, mus, log_sigmas, corr = rval cost = bernoulli_and_correlated_log_gaussian_mixture_cost( binary, coeffs, mus, log_sigmas, corr, y_sym) cost = masked_cost(cost, y_mask_sym).mean() cost_function = theano.function([X_sym, X_mask_sym, y_sym, y_mask_sym], [cost], mode="FAST_COMPILE") checkpoint_dict = create_checkpoint_dict(locals()) epoch_results = fixed_n_epochs_trainer( cost_function, cost_function, train_indices, valid_indices, checkpoint_dict, [X_seq, y_seq], minibatch_size, list_of_minibatch_functions=[make_masked_minibatch, make_masked_minibatch], list_of_train_output_names=["train_cost"], valid_output_name="valid_cost", n_epochs=1)
def test_correlated_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([bernoulli_X, bernoulli_y], ["X", "y"], graph) n_hid = 20 minibatch_size = len(bernoulli_X) train_indices = np.arange(len(bernoulli_X)) valid_indices = np.arange(len(bernoulli_X)) l1 = tanh_layer([X_sym], graph, 'l1', proj_dim=n_hid, random_state=random_state) rval = bernoulli_and_correlated_log_gaussian_mixture_layer( [l1], graph, 'hw', proj_dim=2, n_components=3, random_state=random_state) binary, coeffs, mus, log_sigmas, corr = rval cost = bernoulli_and_correlated_log_gaussian_mixture_cost( binary, coeffs, mus, log_sigmas, corr, 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, [bernoulli_X, bernoulli_y], minibatch_size, list_of_train_output_names=["train_cost"], valid_output_name="valid_cost", n_epochs=1)
weights = get_weights_from_graph(graph) L2 = sum([(w**2).sum() for w in weights]) cost = nll + .0001 * L2 params, grads = get_params_and_grads(graph, cost) learning_rate = 1E-4 momentum = 0.95 opt = rmsprop(params, learning_rate, momentum) 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], [y_pred]) checkpoint_dict = create_checkpoint_dict(locals()) def error(*args): xargs = args[:-1] y = args[-1] final_args = xargs y_pred = predict_function(*final_args)[0] return 1 - np.mean( (np.argmax(y_pred, axis=1).ravel()) == (np.argmax(y, axis=1).ravel())) train_itr = minibatch_iterator([X, y], minibatch_size, axis=0, stop_index=60000)
def train_loop(itr): mb = next(itr) X_mb, y_mb = mb[:-1], mb[1:] cost, h = fit_function(X_mb, y_mb, train_h_init) train_h_init[:] = h[-1, :] return [cost] def valid_loop(itr): mb = next(itr) X_mb, y_mb = mb[:-1], mb[1:] cost, h = cost_function(X_mb, y_mb, valid_h_init) valid_h_init[:] = h[-1, :] return [cost] checkpoint_dict = create_checkpoint_dict(locals()) TL = TrainingLoop( train_loop, train_itr, valid_loop, valid_itr, n_epochs=100, checkpoint_every_n_epochs=1, checkpoint_dict=checkpoint_dict, skip_minimums=True, ) epoch_results = TL.run()