def test_conv3d(self): # TH input shape: (samples, input_depth, conv_dim1, conv_dim2, conv_dim3) # TF input shape: (samples, conv_dim1, conv_dim2, conv_dim3, input_depth) # TH kernel shape: (depth, input_depth, x, y, z) # TF kernel shape: (x, y, z, input_depth, depth) # test in dim_ordering = th for input_shape in [(2, 3, 4, 5, 4), (2, 3, 5, 4, 6)]: for kernel_shape in [(4, 3, 2, 2, 2), (4, 3, 3, 2, 4)]: xval = np.random.random(input_shape) xth = KTH.variable(xval) xtf = KTF.variable(xval) kernel_val = np.random.random(kernel_shape) - 0.5 kernel_th = KTH.variable(convert_kernel(kernel_val, dim_ordering='th')) kernel_tf = KTF.variable(kernel_val) zth = KTH.eval(KTH.conv3d(xth, kernel_th, dim_ordering='th')) ztf = KTF.eval(KTF.conv3d(xtf, kernel_tf, dim_ordering='th')) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05) # test in dim_ordering = tf input_shape = (1, 2, 2, 2, 1) kernel_shape = (2, 2, 2, 1, 1) xval = np.random.random(input_shape) xth = KTH.variable(xval) xtf = KTF.variable(xval) kernel_val = np.random.random(kernel_shape) - 0.5 kernel_th = KTH.variable(convert_kernel(kernel_val, dim_ordering='tf')) kernel_tf = KTF.variable(kernel_val) zth = KTH.eval(KTH.conv3d(xth, kernel_th, dim_ordering='tf')) ztf = KTF.eval(KTF.conv3d(xtf, kernel_tf, dim_ordering='tf')) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05)
def check_single_tensor_operation(function_name, input_shape, **kwargs): val = np.random.random(input_shape) - 0.5 xth = KTH.variable(val) xtf = KTF.variable(val) zth = KTH.eval(getattr(KTH, function_name)(xth, **kwargs)) ztf = KTF.eval(getattr(KTF, function_name)(xtf, **kwargs)) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05)
def test_random_normal(self): mean = 0. std = 1. rand = KTF.eval(KTF.random_normal((1000, 1000), mean=mean, std=std)) assert(rand.shape == (1000, 1000)) assert(np.abs(np.mean(rand) - mean) < 0.01) assert(np.abs(np.std(rand) - std) < 0.01) rand = KTH.eval(KTH.random_normal((1000, 1000), mean=mean, std=std)) assert(rand.shape == (1000, 1000)) assert(np.abs(np.mean(rand) - mean) < 0.01) assert(np.abs(np.std(rand) - std) < 0.01)
def test_conv2d(self): # TH kernel shape: (depth, input_depth, rows, cols) # TF kernel shape: (rows, cols, input_depth, depth) for input_shape in [(2, 3, 4, 5), (2, 3, 5, 6)]: for kernel_shape in [(4, 3, 2, 2), (4, 3, 3, 4)]: xval = np.random.random(input_shape) xth = KTH.variable(xval) xtf = KTF.variable(xval) kernel_val = np.random.random(kernel_shape) - 0.5 kernel_th = KTH.variable(convert_kernel(kernel_val, dim_ordering='th')) kernel_tf = KTF.variable(kernel_val) zth = KTH.eval(KTH.conv2d(xth, kernel_th, dim_ordering='th')) ztf = KTF.eval(KTF.conv2d(xtf, kernel_tf, dim_ordering='th')) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05) input_shape = (1, 6, 5, 3) kernel_shape = (3, 3, 3, 2) xval = np.random.random(input_shape) xth = KTH.variable(xval) xtf = KTF.variable(xval) kernel_val = np.random.random(kernel_shape) - 0.5 kernel_th = KTH.variable(convert_kernel(kernel_val, dim_ordering='tf')) kernel_tf = KTF.variable(kernel_val) zth = KTH.eval(KTH.conv2d(xth, kernel_th, dim_ordering='tf')) ztf = KTF.eval(KTF.conv2d(xtf, kernel_tf, dim_ordering='tf')) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05)
def test_random_binomial(self): p = 0.5 rand = KTF.eval(KTF.random_binomial((1000, 1000), p)) assert(rand.shape == (1000, 1000)) assert(np.abs(np.mean(rand) - p) < 0.01) assert(np.max(rand) == 1) assert(np.min(rand) == 0) rand = KTH.eval(KTH.random_binomial((1000, 1000), p)) assert(rand.shape == (1000, 1000)) assert(np.abs(np.mean(rand) - p) < 0.01) assert(np.max(rand) == 1) assert(np.min(rand) == 0)
def test_random_uniform(self): min = -1. max = 1. rand = KTF.eval(KTF.random_uniform((1000, 1000), min, max)) assert(rand.shape == (1000, 1000)) assert(np.abs(np.mean(rand)) < 0.01) assert(np.max(rand) <= max) assert(np.min(rand) >= min) rand = KTH.eval(KTH.random_uniform((1000, 1000), min, max)) assert(rand.shape == (1000, 1000)) assert(np.abs(np.mean(rand)) < 0.01) assert(np.max(rand) <= max) assert(np.min(rand) >= min)
def check_two_tensor_operation(function_name, x_input_shape, y_input_shape, **kwargs): xval = np.random.random(x_input_shape) - 0.5 xth = KTH.variable(xval) xtf = KTF.variable(xval) yval = np.random.random(y_input_shape) - 0.5 yth = KTH.variable(yval) ytf = KTF.variable(yval) zth = KTH.eval(getattr(KTH, function_name)(xth, yth, **kwargs)) ztf = KTF.eval(getattr(KTF, function_name)(xtf, ytf, **kwargs)) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05)
def test_shape_operations(self): # concatenate xval = np.random.random((4, 3)) xth = KTH.variable(xval) xtf = KTF.variable(xval) yval = np.random.random((4, 2)) yth = KTH.variable(yval) ytf = KTF.variable(yval) zth = KTH.eval(KTH.concatenate([xth, yth], axis=-1)) ztf = KTF.eval(KTF.concatenate([xtf, ytf], axis=-1)) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05) check_single_tensor_operation('reshape', (4, 2), shape=(8, 1)) check_single_tensor_operation('permute_dimensions', (4, 2, 3), pattern=(2, 0, 1)) #check_single_tensor_operation('repeat', (4, 1), n=3) #check_single_tensor_operation('flatten', (4, 1)) check_single_tensor_operation('expand_dims', (4, 3), dim=-1) check_single_tensor_operation('expand_dims', (4, 3, 2), dim=1)
def check_composed_tensor_operations(first_function_name, first_function_args, second_function_name, second_function_args, input_shape): ''' Creates a random tensor t0 with shape input_shape and compute t1 = first_function_name(t0, **first_function_args) t2 = second_function_name(t1, **second_function_args) with both Theano and TensorFlow backends and ensures the answers match. ''' val = np.random.random(input_shape) - 0.5 xth = KTH.variable(val) xtf = KTF.variable(val) yth = getattr(KTH, first_function_name)(xth, **first_function_args) ytf = getattr(KTF, first_function_name)(xtf, **first_function_args) zth = KTH.eval(getattr(KTH, second_function_name)(yth, **second_function_args)) ztf = KTF.eval(getattr(KTF, second_function_name)(ytf, **second_function_args)) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05)
def test_nn_operations(self): check_single_tensor_operation('relu', (4, 2), alpha=0.1, max_value=0.5) check_single_tensor_operation('softmax', (4, 10)) # check_single_tensor_operation('softplus', (4, 10)) check_single_tensor_operation('elu', (4, 10), alpha=0.5) check_single_tensor_operation('sigmoid', (4, 2)) # check_single_tensor_operation('hard_sigmoid', (4, 2)) check_single_tensor_operation('tanh', (4, 2)) # dropout val = np.random.random((100, 100)) xth = KTH.variable(val) xtf = KTF.variable(val) zth = KTH.eval(KTH.dropout(xth, level=0.2)) ztf = KTF.eval(KTF.dropout(xtf, level=0.2)) assert zth.shape == ztf.shape # dropout patterns are different, only check mean assert np.abs(zth.mean() - ztf.mean()) < 0.05 '''