def test_strided_elementwise_inplace(): from brainstorm.handlers import PyCudaHandler _h = PyCudaHandler() rdm = np.random.RandomState(1345) def get_rdm_array(shape, dims): if dims == 2: return rdm.randn(shape[0],shape[1]) elif dims == 3: return rdm.randn(shape[0],shape[1], shape[2]) else: return rdm.randn(shape[0],shape[1], shape[2], shape[3]) for dims in range(2,5): for i in range(10): shape = rdm.randint(1,17,dims) a1 = np.float32(get_rdm_array(shape, dims)) a2 = np.float32(get_rdm_array(shape, dims)) a3 = np.float32(get_rdm_array(shape, dims)) a = np.vstack([a1,a2,a3]) original_shape = a.shape a = a.reshape([int(original_shape[0]/3)] + list(original_shape[1:])+[3]) b = np.zeros_like(a, dtype=np.float32) A = _h.create_from_numpy(a) _h.strided_elementwise_inplace(A, 1,'logistic') _h.strided_elementwise_inplace(A, 0,'tanh') outputs = _h.get_numpy_copy(A).reshape(original_shape) c1 = np.tanh(a1) c2 = 1./(1.+np.exp(a2)) c3 = a3 c = np.vstack([c1,c2,c3]) passed = np.allclose(outputs, c) assert passed
def test_conv2d_forward_batch_pycuda(): from brainstorm.handlers import PyCudaHandler _h = PyCudaHandler() for input_shape in ((3, 3), (5, 4), (4, 9)): for nr_images in (1, 4): for nr_input_maps in (1, 3): for nr_filters in (1, 3): for kernel_shape in ((1, 1), (2, 2), (3, 2)): for stride in ((1, 1), (2, 2), (1, 2)): for padding in (0, 1): inputs = np.random.rand( nr_images, input_shape[0], input_shape[1], nr_input_maps).astype(dtype) weights = np.random.rand( nr_filters, kernel_shape[0], kernel_shape[1], nr_input_maps).astype( dtype) bias = np.zeros(nr_filters).astype(dtype) output_height = \ (input_shape[0] + 2 * padding - kernel_shape[0]) / stride[0] + 1 output_width = \ (input_shape[1] + 2 * padding - kernel_shape[1]) / stride[1] + 1 outputs = np.zeros((nr_images, output_height, output_width, nr_filters), dtype=dtype) true_outputs = np.zeros_like(outputs) _conv2d_forward_batch(inputs, weights, bias, true_outputs, padding, stride) i_dev = _h.create_from_numpy(inputs) w_dev = _h.create_from_numpy(weights) b_dev = _h.create_from_numpy(bias) o_dev = _h.create_from_numpy(outputs) _h.conv2d_forward_batch(i_dev, w_dev, b_dev, o_dev, padding, stride) outputs = _h.get_numpy_copy(o_dev) passed = np.allclose(outputs, true_outputs) if not passed: print("Checking Inputs:",(nr_images,) + input_shape + (nr_input_maps,)) print("Filters:", (nr_filters,) + kernel_shape + (nr_input_maps,)) print("Stride: ", stride, "padding: ", padding) print("Expected:\n", true_outputs) print("Obtained:\n", outputs) assert passed
def test_conv2d_forward_batch_pycuda(): from brainstorm.handlers import PyCudaHandler _h = PyCudaHandler() for input_shape in ((3, 3), (5, 4), (4, 9)): for nr_images in (1, 4): for nr_input_maps in (1, 3): for nr_filters in (1, 3): for kernel_shape in ((1, 1), (2, 2), (3, 2)): for stride in ((1, 1), (2, 2), (1, 2)): for padding in (0, 1): inputs = np.random.rand( nr_images, input_shape[0], input_shape[1], nr_input_maps).astype(dtype) weights = np.random.rand( nr_filters, kernel_shape[0], kernel_shape[1], nr_input_maps).astype(dtype) bias = np.zeros(nr_filters).astype(dtype) output_height = \ (input_shape[0] + 2 * padding - kernel_shape[0]) / stride[0] + 1 output_width = \ (input_shape[1] + 2 * padding - kernel_shape[1]) / stride[1] + 1 outputs = np.zeros((nr_images, output_height, output_width, nr_filters), dtype=dtype) true_outputs = np.zeros_like(outputs) _conv2d_forward_batch(inputs, weights, bias, true_outputs, padding, stride) i_dev = _h.create_from_numpy(inputs) w_dev = _h.create_from_numpy(weights) b_dev = _h.create_from_numpy(bias) o_dev = _h.create_from_numpy(outputs) _h.conv2d_forward_batch( i_dev, w_dev, b_dev, o_dev, padding, stride) outputs = _h.get_numpy_copy(o_dev) passed = np.allclose(outputs, true_outputs) if not passed: print("Checking Inputs:", (nr_images, ) + input_shape + (nr_input_maps, )) print("Filters:", (nr_filters, ) + kernel_shape + (nr_input_maps, )) print("Stride: ", stride, "padding: ", padding) print("Expected:\n", true_outputs) print("Obtained:\n", outputs) assert passed