def test_xcov_derivative_cc2tensor(): from neon.backends.cc2 import GPU, GPUTensor be = GPU(rng_seed=0) np.random.seed(0) n = 10 k = 8 (k1, k2) = (3, 5) a = np.array(np.random.randn(k, n), dtype='float32', order='C') s = np.zeros_like(a) acc = xcc(a[:k1], a[k1:]) # k1 x k2 c1 = a[k1:] - a[k1:].mean(1, keepdims=True) # k2 x n c2 = a[:k1] - a[:k1].mean(1, keepdims=True) # k1 x n s[:k1] = acc.dot(c1) / n s[k1:] = acc.T.dot(c2) / n outputs = GPUTensor(a.copy()) tempbuf1 = be.empty((k1, n)) tempbuf2 = be.empty((k2, n)) tempbuf3 = be.empty((k1, k2)) tempbuf4 = be.empty(outputs.shape) temp = [tempbuf1, tempbuf2, tempbuf3, tempbuf4] my_result = xcov_cost_derivative(be, outputs, [], temp, k1) expected_result = GPUTensor(s) assert_tensor_near_equal(expected_result, my_result)
def test_cudanet_negative(self): self.layer.positive(self.inputs) self.layer.negative(self.inputs) target = np.array([0.50274211, 0.50407821], dtype='float32') assert_tensor_near_equal(self.layer.p_hid_minus.asnumpyarray()[:, 0], target)
def test_xcov_derivative_cc2tensor(): from neon.backends.cc2 import GPU, GPUTensor be = GPU(rng_seed=0) np.random.seed(0) n = 10 k = 8 (k1, k2) = (3, 5) a = np.array(np.random.randn(k, n), dtype='float32', order='C') s = np.zeros_like(a) acc = xcc(a[:k1], a[k1:]) # k1 x k2 c1 = a[k1:] - a[k1:].mean(1, keepdims=True) # k2 x n c2 = a[:k1] - a[:k1].mean(1, keepdims=True) # k1 x n s[:k1] = acc.dot(c1)/n s[k1:] = acc.T.dot(c2)/n outputs = GPUTensor(a.copy()) tempbuf1 = be.empty((k1, n)) tempbuf2 = be.empty((k2, n)) tempbuf3 = be.empty((k1, k2)) tempbuf4 = be.empty(outputs.shape) temp = [tempbuf1, tempbuf2, tempbuf3, tempbuf4] my_result = xcov_cost_derivative(be, outputs, [], temp, k1) expected_result = GPUTensor(s) assert_tensor_near_equal(expected_result, my_result)
def test_softmax_cputensor(): sftmx = Softmax() inputs = np.array([0, 1, -2]).reshape((3, 1)) be = CPU(rng_seed=0) temp = be.zeros((3, 1)) outputs = np.exp(inputs-1) / np.sum(np.exp(inputs-1)) sftmx.apply_function(be, CPUTensor(inputs), temp) assert_tensor_near_equal(CPUTensor(outputs), temp)
def test_softmax_cputensor(): sftmx = Softmax() inputs = np.array([0, 1, -2]).reshape((3, 1)) be = CPU(rng_seed=0) temp = be.zeros((3, 1)) outputs = np.exp(inputs - 1) / np.sum(np.exp(inputs - 1)) sftmx.apply_function(be, CPUTensor(inputs), temp) assert_tensor_near_equal(CPUTensor(outputs), temp)
def test_logistic_cputensor(): lgstc = Logistic() inputs = np.array([0, 1, -2]).reshape((3, 1)) be = CPU(rng_seed=0) temp = be.zeros((3, 1)) outputs = 1.0 / (1.0 + np.exp(-inputs)) lgstc.apply_function(be, CPUTensor(inputs), temp) assert_tensor_near_equal(CPUTensor(outputs), temp)
def test_logistic_derivative_cputensor(): lgstc = Logistic() inputs = np.array([0, 1, -2]).reshape((3, 1)) be = CPU(rng_seed=0) outputs = 1.0 / (1.0 + np.exp(-inputs)) outputs = outputs * (1.0 - outputs) temp = be.zeros(inputs.shape) lgstc.apply_derivative(be, CPUTensor(inputs), temp) assert_tensor_near_equal(CPUTensor(outputs), temp)
def test_cross_entropy_derivative_cputensor(): be = CPU(rng_seed=0) outputs = CPUTensor([0.5, 0.9, 0.1, 0.0001]) targets = CPUTensor([0.5, 0.99, 0.01, 0.2]) temp = [be.zeros(outputs.shape), be.zeros(outputs.shape)] expected_result = ((outputs.asnumpyarray() - targets.asnumpyarray()) / (outputs.asnumpyarray() * (1 - outputs.asnumpyarray()))) assert_tensor_near_equal( expected_result, cross_entropy_derivative(be, outputs, targets, temp))
def test_tanh_cc2tensor(): tntest = Tanh() from neon.backends.cc2 import GPU, GPUTensor inputs = np.array([0, 1, -2]).reshape((3, 1)) outputs = GPUTensor([true_tanh(0), true_tanh(1), true_tanh(-2)]) be = GPU(rng_seed=0) temp = be.zeros((3, 1)) tntest.apply_function(be, GPUTensor(inputs), temp) assert_tensor_near_equal(outputs, temp)
def test_tanh_cputensor(): tntest = Tanh() be = CPU(rng_seed=0) CPUTensor([0, 1, -2]) inputs = np.array([0, 1, -2]) temp = be.zeros(inputs.shape) outputs = np.array([true_tanh(0), true_tanh(1), true_tanh(-2)]) tntest.apply_function(be, CPUTensor(inputs), temp) assert_tensor_near_equal(CPUTensor(outputs), temp)
def test_tanh_derivative_cputensor(): tntest = Tanh() inputs = np.array([0, 1, -2]) be = CPU(rng_seed=0) outputs = np.array( [1 - true_tanh(0)**2, 1 - true_tanh(1)**2, 1 - true_tanh(-2)**2]) temp = be.zeros(inputs.shape) tntest.apply_derivative(be, CPUTensor(inputs), temp) assert_tensor_near_equal(CPUTensor(outputs), temp)
def test_logistic_cc2tensor(): lgstc = Logistic() from neon.backends.cc2 import GPU, GPUTensor inputs = np.array([0, 1, -2]).reshape((3, 1)) outputs = 1.0 / (1.0 + np.exp(-inputs)) be = GPU(rng_seed=0) temp = be.zeros((3, 1)) lgstc.apply_function(be, GPUTensor(inputs), temp) assert_tensor_near_equal(GPUTensor(outputs), temp)
def test_softmax_cc2tensor(): sftmx = Softmax() from neon.backends.cc2 import GPU, GPUTensor inputs = np.array([0, 1, -2]).reshape((3, 1)) outputs = np.exp(inputs) / np.sum(np.exp(inputs)) be = GPU(rng_seed=0) temp = be.zeros((3, 1)) sftmx.apply_function(be, GPUTensor(inputs), temp) assert_tensor_near_equal(GPUTensor(outputs), temp)
def test_tanh_derivative_cc2tensor(): tntest = Tanh() from neon.backends.cc2 import GPU, GPUTensor inputs = np.array([0, 1, -2], dtype='float32').reshape((3, 1)) be = GPU(rng_seed=0) outputs = GPUTensor( [1 - true_tanh(0)**2, 1 - true_tanh(1)**2, 1 - true_tanh(-2)**2]) temp = be.zeros(inputs.shape) tntest.apply_derivative(be, GPUTensor(inputs, dtype='float32'), temp) assert_tensor_near_equal(outputs, temp, tolerance=1e-5)
def test_tanh_derivative_cputensor(): tntest = Tanh() inputs = np.array([0, 1, -2]) be = CPU(rng_seed=0) outputs = np.array([1 - true_tanh(0) ** 2, 1 - true_tanh(1) ** 2, 1 - true_tanh(-2) ** 2]) temp = be.zeros(inputs.shape) tntest.apply_derivative(be, CPUTensor(inputs), temp) assert_tensor_near_equal(CPUTensor(outputs), temp)
def test_lrgnorm(self): tsr = self.be.array([[-1, 0], [1, 3]]) rpow = 1. / 5 # -> sum([[1, 0], [1, 243]], axis=0)**rpow -> rpow([2, 243]) assert_tensor_equal(self.be.norm(tsr, order=5, axis=0), CPUTensor([[2**rpow, 243**rpow]])) # -> sum([[1, 0], [1, 243]], axis=1)**rpow -> rpow([1, 244]) # 244**.2 == ~3.002465 hence the near_equal test assert_tensor_near_equal(self.be.norm(tsr, order=5, axis=1), CPUTensor([1**rpow, 244**rpow]), 1e-6)
def test_cross_entropy_derivative_cputensor(): be = CPU(rng_seed=0) outputs = CPUTensor([0.5, 0.9, 0.1, 0.0001]) targets = CPUTensor([0.5, 0.99, 0.01, 0.2]) temp = [be.zeros(outputs.shape), be.zeros(outputs.shape)] expected_result = ((outputs.asnumpyarray() - targets.asnumpyarray()) / (outputs.asnumpyarray() * (1 - outputs.asnumpyarray()))) assert_tensor_near_equal(expected_result, cross_entropy_derivative(be, outputs, targets, temp))
def test_negnorm(self): tsr = self.be.array([[-1, -2], [1, 3]]) rpow = -1. / 3 # -> sum([[1, .125], [1, .037037]], axis=0)**rpow -> rpow([2, .162037]) assert_tensor_equal(self.be.norm(tsr, order=-3, axis=0), CPUTensor([[2**rpow, .162037037037**rpow]])) # -> sum([[1, .125], [1, .037037]], axis=1)**rpow -> # rpow([1.125, 1.037037]) assert_tensor_near_equal(self.be.norm(tsr, order=-3, axis=1), CPUTensor([1.125**rpow, 1.037037**rpow]), 1e-6)
def test_cross_entropy_cputensor(): be = CPU(rng_seed=0) outputs = CPUTensor([0.5, 0.9, 0.1, 0.0001]) targets = CPUTensor([0.5, 0.99, 0.01, 0.2]) temp = [be.zeros(outputs.shape), be.zeros(outputs.shape)] expected_result = np.sum( (-targets.asnumpyarray()) * np.log(outputs.asnumpyarray()) - (1 - targets.asnumpyarray()) * np.log(1 - outputs.asnumpyarray()), keepdims=True) assert_tensor_near_equal(expected_result, cross_entropy(be, outputs, targets, temp))
def test_softmax_derivative_cputensor(): sftmx = Softmax() inputs = np.array([0, 1, -2]).reshape((3, 1)) be = CPU(rng_seed=0) outputs = np.exp(inputs) / np.sum(np.exp(inputs)) errmat = np.ones(inputs.shape) a = np.einsum('ij,ji->i', errmat.T, outputs) outputs = outputs * (errmat - a[np.newaxis, :]) temp = be.zeros(inputs.shape) sftmx.apply_derivative(be, CPUTensor(inputs), temp) assert_tensor_near_equal(CPUTensor(outputs), temp)
def test_tanh_derivative_cc2tensor(): tntest = Tanh() from neon.backends.cc2 import GPU, GPUTensor inputs = np.array([0, 1, -2], dtype='float32').reshape((3, 1)) be = GPU(rng_seed=0) outputs = GPUTensor([1 - true_tanh(0) ** 2, 1 - true_tanh(1) ** 2, 1 - true_tanh(-2) ** 2]) temp = be.zeros(inputs.shape) tntest.apply_derivative(be, GPUTensor(inputs, dtype='float32'), temp) assert_tensor_near_equal(outputs, temp, tolerance=1e-5)
def test_cross_entropy_cputensor(): be = CPU(rng_seed=0) outputs = CPUTensor([0.5, 0.9, 0.1, 0.0001]) targets = CPUTensor([0.5, 0.99, 0.01, 0.2]) temp = [be.zeros(outputs.shape), be.zeros(outputs.shape)] expected_result = np.sum((- targets.asnumpyarray()) * np.log(outputs.asnumpyarray()) - (1 - targets.asnumpyarray()) * np.log(1 - outputs.asnumpyarray()), keepdims=True) assert_tensor_near_equal(expected_result, cross_entropy(be, outputs, targets, temp))
def test_softmax_derivative_cc2tensor(): sftmx = Softmax() from neon.backends.cc2 import GPU, GPUTensor inputs = np.array([0, 1, -2]).reshape((3, 1)) outputs = np.exp(inputs) / np.sum(np.exp(inputs)) errmat = np.ones(inputs.shape) a = np.einsum('ij,ji->i', errmat.T, outputs) outputs = outputs * (errmat - a[np.newaxis, :]) be = GPU(rng_seed=0) temp = be.zeros(inputs.shape) sftmx.apply_derivative(be, GPUTensor(inputs), temp) assert_tensor_near_equal(GPUTensor(outputs), temp)
def test_negnorm(self): tsr = self.be.array([[-1, -2], [1, 3]]) rpow = -1. / 3 # -> sum([[1, .125], [1, .037037]], axis=0)**rpow -> rpow([2, .162037]) out = self.be.empty((1, 2)) assert_tensor_equal(self.be.norm(tsr, order=-3, axis=0, out=out), self.gpt([[2**rpow, .162037037037**rpow]])) # -> sum([[1, .125], [1, .037037]], axis=1)**rpow -> # rpow([1.125, 1.037037]) out = self.be.empty((2, 1)) assert_tensor_near_equal(self.be.norm(tsr, order=-3, axis=1, out=out), self.gpt([1.125**rpow, 1.037037**rpow]), 1e-6)
def test_lrgnorm(self): tsr = self.be.array([[-1, 0], [1, 3]]) rpow = 1. / 5 # -> sum([[1, 0], [1, 243]], axis=0)**rpow -> rpow([2, 243]) out = self.be.empty((1, 2)) assert_tensor_equal(self.be.norm(tsr, order=5, axis=0, out=out), self.gpt([[2**rpow, 243**rpow]])) # -> sum([[1, 0], [1, 243]], axis=1)**rpow -> rpow([1, 244]) # 244**.2 == ~3.002465 hence the near_equal test out = self.be.empty((2, 1)) assert_tensor_near_equal(self.be.norm(tsr, order=5, axis=1, out=out), self.gpt([1**rpow, 244**rpow]), 1e-6)
def test_cross_entropy_cc2tensor(): from neon.backends.cc2 import GPU, GPUTensor be = GPU(rng_seed=0) # to ensure cublas_init() is called. outputs = GPUTensor([0.5, 0.9, 0.1, 0.0001]) targets = GPUTensor([0.5, 0.99, 0.01, 0.2]) temp = [be.zeros(outputs.shape), be.zeros(outputs.shape)] expected_result = np.sum((- targets.asnumpyarray()) * np.log(outputs.asnumpyarray()) - (1 - targets.asnumpyarray()) * np.log(1 - outputs.asnumpyarray()), keepdims=True) assert_tensor_near_equal(expected_result, cross_entropy(be, outputs, targets, temp), tolerance=1e-6)
def test_cross_entropy_limits(): be = CPU(rng_seed=0) outputs = CPUTensor([0.5, 1.0, 0.0, 0.0001]) targets = CPUTensor([0.5, 0.0, 1.0, 0.2]) eps = 2**-23 temp = [be.zeros(outputs.shape), be.zeros(outputs.shape)] expected_result = np.sum( (-targets.asnumpyarray()) * np.log(outputs.asnumpyarray() + eps) - (1 - targets.asnumpyarray()) * np.log(1 - outputs.asnumpyarray() + eps), keepdims=True) assert_tensor_near_equal(expected_result, cross_entropy(be, outputs, targets, temp, eps))
def test_cross_entropy_cc2tensor(): from neon.backends.cc2 import GPU, GPUTensor be = GPU(rng_seed=0) # to ensure cublas_init() is called. outputs = GPUTensor([0.5, 0.9, 0.1, 0.0001]) targets = GPUTensor([0.5, 0.99, 0.01, 0.2]) temp = [be.zeros(outputs.shape), be.zeros(outputs.shape)] expected_result = np.sum( (-targets.asnumpyarray()) * np.log(outputs.asnumpyarray()) - (1 - targets.asnumpyarray()) * np.log(1 - outputs.asnumpyarray()), keepdims=True) assert_tensor_near_equal(expected_result, cross_entropy(be, outputs, targets, temp), tolerance=1e-6)
def test_xcov_cputensor(): np.random.seed(0) n = 10 k = 8 (k1, k2) = (3, 5) a = np.array(np.random.randn(k, n) * 10, dtype='float32', order='C') acc = xcc(a[:k1], a[k1:]) expected_result = 0.5 * (acc**2.).sum() be = CPU(rng_seed=0) outputs = CPUTensor(a.copy()) tempbuf1 = be.empty((k1, n)) tempbuf2 = be.empty((k2, n)) tempbuf3 = be.empty((k1, k2)) tempbuf4 = be.empty(outputs.shape) temp = [tempbuf1, tempbuf2, tempbuf3, tempbuf4] my_result = xcov_cost(be, outputs, [], temp, k1) assert_tensor_near_equal(expected_result, my_result)
def test_xcov_cputensor(): np.random.seed(0) n = 10 k = 8 (k1, k2) = (3, 5) a = np.array(np.random.randn(k, n)*10, dtype='float32', order='C') acc = xcc(a[:k1], a[k1:]) expected_result = 0.5 * (acc**2.).sum() be = CPU(rng_seed=0) outputs = CPUTensor(a.copy()) tempbuf1 = be.empty((k1, n)) tempbuf2 = be.empty((k2, n)) tempbuf3 = be.empty((k1, k2)) tempbuf4 = be.empty(outputs.shape) temp = [tempbuf1, tempbuf2, tempbuf3, tempbuf4] my_result = xcov_cost(be, outputs, [], temp, k1) assert_tensor_near_equal(expected_result, my_result)
def test_xcov_cc2tensor(): np.random.seed(0) n = 10 k = 8 (k1, k2) = (3, 5) a = np.array(np.random.randn(k, n) * 10, dtype='float32', order='C') acc = xcc(a[:k1], a[k1:]) expected_result = 0.5 * (acc**2.).sum() from neon.backends.cc2 import GPU, GPUTensor be = GPU(rng_seed=0) # to ensure cublas_init() is called. outputs = GPUTensor(a.copy()) tempbuf1 = be.empty((k1, n)) tempbuf2 = be.empty((k2, n)) tempbuf3 = be.empty((k1, k2)) tempbuf4 = be.empty(outputs.shape) temp = [tempbuf1, tempbuf2, tempbuf3, tempbuf4] my_result = xcov_cost(be, outputs, [], temp, k1) assert_tensor_near_equal(expected_result, my_result, tolerance=1e-3)
def test_xcov_cc2tensor(): np.random.seed(0) n = 10 k = 8 (k1, k2) = (3, 5) a = np.array(np.random.randn(k, n)*10, dtype='float32', order='C') acc = xcc(a[:k1], a[k1:]) expected_result = 0.5 * (acc**2.).sum() from neon.backends.cc2 import GPU, GPUTensor be = GPU(rng_seed=0) # to ensure cublas_init() is called. outputs = GPUTensor(a.copy()) tempbuf1 = be.empty((k1, n)) tempbuf2 = be.empty((k2, n)) tempbuf3 = be.empty((k1, k2)) tempbuf4 = be.empty(outputs.shape) temp = [tempbuf1, tempbuf2, tempbuf3, tempbuf4] my_result = xcov_cost(be, outputs, [], temp, k1) assert_tensor_near_equal(expected_result, my_result, tolerance=1e-3)
def test_cudanet_positive(self): self.layer.positive(self.inputs) target = np.array([0.50541031, 0.50804842], dtype='float32') assert_tensor_near_equal(self.layer.p_hid_plus.asnumpyarray()[:, 0], target)
def test_cudanet_cost(self): self.layer.positive(self.inputs) self.layer.negative(self.inputs) thecost = self.cost.apply_function(self.inputs) target = 106.588943481 assert_tensor_near_equal(thecost, target)