def __call__(self, X): XY = X.dot(X.T) x2 = at.sum(X**2, axis=1).dimshuffle(0, "x") X2e = at.repeat(x2, X.shape[0], axis=1) H = X2e + X2e.T - 2.0 * XY V = at.sort(H.flatten()) length = V.shape[0] # median distance m = at.switch( at.eq((length % 2), 0), # if even vector at.mean(V[((length // 2) - 1):((length // 2) + 1)]), # if odd vector V[length // 2], ) h = 0.5 * m / at.log(floatX(H.shape[0]) + floatX(1)) # RBF Kxy = at.exp(-H / h / 2.0) # Derivative dxkxy = -at.dot(Kxy, X) sumkxy = at.sum(Kxy, axis=-1, keepdims=True) dxkxy = at.add(dxkxy, at.mul(X, sumkxy)) / h return Kxy, dxkxy
def test_GpuCrossentropySoftmaxArgmax1HotWithBias(): # This is basic test for GpuCrossentropySoftmaxArgmax1HotWithBias # We check that we loop when their is too much threads n_in = 1000 batch_size = 4097 n_out = 1250 if not isinstance(mode_with_gpu, aesara.compile.DebugMode): n_in = 4098 n_out = 4099 y = tt.lvector("y") b = tt.fvector("b") # we precompute the dot with big shape before to allow the test of # GpuCrossentropySoftmax1HotWithBiasDx to don't fail with the error # (the launch timed out and was terminated) on GPU card not # powerful enough. We need the big shape to check for corner # case. dot_result = tt.fmatrix("dot_result") # Seed numpy.random with config.unittests.rseed utt.seed_rng() xx = np.asarray(np.random.rand(batch_size, n_in), dtype=np.float32) yy = np.ones((batch_size, ), dtype="int32") b_values = np.zeros((n_out, ), dtype="float32") W_values = np.asarray(np.random.rand(n_in, n_out), dtype="float32") dot_value = np.asarray(np.dot(xx, W_values), dtype="float32") del W_values p_y_given_x = tt.nnet.softmax(dot_result + b) y_pred = tt.argmax(p_y_given_x, axis=-1) loss = -tt.mean(tt.log(p_y_given_x)[tt.arange(y.shape[0]), y]) dW = tt.grad(loss, dot_result) classify = aesara.function(inputs=[y, b, dot_result], outputs=[loss, y_pred, dW], mode=mode_without_gpu) classify_gpu = aesara.function(inputs=[y, b, dot_result], outputs=[loss, y_pred, dW], mode=mode_with_gpu) assert any([ isinstance(node.op, tt.nnet.CrossentropySoftmaxArgmax1HotWithBias) for node in classify.maker.fgraph.toposort() ]) assert any([ isinstance(node.op, GpuCrossentropySoftmaxArgmax1HotWithBias) for node in classify_gpu.maker.fgraph.toposort() ]) out = classify(yy, b_values, dot_value) gout = classify_gpu(yy, b_values, dot_value) assert len(out) == len(gout) == 3 utt.assert_allclose(out[0], gout[0]) utt.assert_allclose(out[2], gout[2], atol=3e-6) utt.assert_allclose(out[1], gout[1])
def setup_gpu_op(self, activations, labels, input_length, compute_grad=True): gpu_ctc_cost = gpu_ctc(activations, labels, input_length) outputs = [gpu_ctc_cost] if compute_grad: # Symbolic gradient of CTC cost gpu_ctc_grad = tt.grad(tt.mean(gpu_ctc_cost), activations) outputs += [gpu_ctc_grad] return aesara.function([], outputs, mode=mode_with_gpu)
def run_ctc(self, activations, labels, input_length, expected_costs, expected_grads): # Create symbolic variables t_activations = aesara.shared(activations, name="activations") t_activation_times = aesara.shared(input_length, name="activation_times") t_labels = aesara.shared(labels, name="labels") t_cost = ctc(t_activations, t_labels, t_activation_times) # Symbolic gradient of CTC cost t_grad = tt.grad(tt.mean(t_cost), t_activations) # Compile symbolic functions train = aesara.function([], [t_cost, t_grad]) cost, grad = train() utt.assert_allclose(expected_grads / cost.shape[0], grad) utt.assert_allclose(expected_costs, cost) self.check_grads_disabled(t_activations, t_labels, t_activation_times)
def get_moment(cls, rv, *sim_inputs): # Take the mean of 10 draws multiple_sim = rv.owner.op(*sim_inputs, size=at.concatenate([[10], rv.shape])) return at.mean(multiple_sim, axis=0)
import time import numpy as np import aesara from aesara import tensor as tt from aesara.ifelse import ifelse a, b = tt.scalars("a", "b") x, y = tt.matrices("x", "y") z_switch = tt.switch(tt.lt(a, b), tt.mean(x), tt.mean(y)) z_lazy = ifelse(tt.lt(a, b), tt.mean(x), tt.mean(y)) f_switch = aesara.function([a, b, x, y], z_switch) f_lazyifelse = aesara.function([a, b, x, y], z_lazy) val1 = 0.0 val2 = 1.0 big_mat1 = np.ones((10000, 1000)) big_mat2 = np.ones((10000, 1000)) n_times = 10 tic = time.clock() for i in range(n_times): f_switch(val1, val2, big_mat1, big_mat2) print("time spent evaluating both values %f sec" % (time.clock() - tic)) tic = time.clock()