def run_lstm(): del_shared() n_in = X.shape[-1] n_hid = 20 n_out = y.shape[-1] random_state = np.random.RandomState(42) h_init = np.zeros((minibatch_size, 2 * n_hid)).astype("float32") h0 = tensor.fmatrix() l1 = lstm_fork([X_sym], [n_in], n_hid, name="l1", random_state=random_state) def step(in_t, h_tm1): h_t = lstm(in_t, h_tm1, n_hid, name="rec", random_state=random_state) return h_t h, _ = theano.scan(step, sequences=[l1], outputs_info=[h0]) h_o = slice_state(h, n_hid) pred = linear([h_o], [n_hid], n_out, name="l2", random_state=random_state) cost = ((y_sym - pred) ** 2).sum() params = list(get_params().values()) grads = tensor.grad(cost, params) learning_rate = 0.000000000001 opt = sgd(params, learning_rate) updates = opt.updates(params, grads) f = theano.function([X_sym, y_sym, h0], [cost, h], updates=updates, mode="FAST_COMPILE") f(X, y, h_init)
def test_gaussian_log_sample(): del_shared() random_state = np.random.RandomState(1999) mu = linear([X_sym], [X.shape[1]], proj_dim=100, name='mu', random_state=random_state) sigma = linear([X_sym], [X.shape[1]], proj_dim=100, name='sigma', random_state=random_state) random_state = np.random.RandomState(1999) r1 = gaussian_log_sample([mu], [sigma], name="samp1", random_state=random_state) random_state = np.random.RandomState(1999) r2 = gaussian_log_sample([mu], [sigma], name="samp2", random_state=random_state) random_state = np.random.RandomState(42) r3 = gaussian_log_sample([mu], [sigma], name="samp3", random_state=random_state) sample_function = theano.function([X_sym], [r1, r2, r3], mode="FAST_COMPILE") s_r1, s_r2, s_r3 = sample_function(X[:100]) assert_almost_equal(s_r1, s_r2) assert_raises(AssertionError, assert_almost_equal, s_r1, s_r3) ss_r1, ss_r2, ss_r3 = sample_function(X[:100]) assert_raises(AssertionError, assert_almost_equal, s_r1, ss_r1)
def test_pool2d(): random_state = np.random.RandomState(42) # 3 channel mnist X_r = np.random.randn(10, 3, 28, 28).astype("float32") X_sym = tensor.tensor4(dtype="float32") del_shared() l1 = conv2d([X_sym], [(3, 28, 28)], 5, name='l1', random_state=random_state) l2 = pool2d([l1], name='l2') # test that they can stack as well l3 = pool2d([l2], name='l3') f = theano.function([X_sym], [l1, l2, l3], mode="FAST_RUN") l1, l2, l3 = f(X_r)
def test_feedforward_theano_mix(): 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) l1_o = .999 * l1_o y_pred = softmax([l1_o], [20], proj_dim=n_classes, name='out', random_state=random_state) cost = categorical_crossentropy(y_pred, y_sym).mean() params = list(get_params().values()) grads = theano.grad(cost, params) learning_rate = 0.001 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") train_itr = minibatch_iterator([X, y], minibatch_size, axis=0) valid_itr = minibatch_iterator([X, y], minibatch_size, axis=0) X_train, y_train = next(train_itr) X_valid, y_valid = next(valid_itr) fit_function(X_train, y_train) cost_function(X_valid, y_valid)