def test_conv2d_forward_batch_numpy(): _h = NumpyHandler(dtype=dtype) 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) _h.conv2d_forward_batch(inputs, weights, bias, outputs, padding, stride) passed = np.allclose(outputs, true_outputs) if not passed: print("Failed for 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_numpy(): _h = NumpyHandler(dtype=dtype) 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, int(output_height), int(output_width), nr_filters), dtype=dtype) true_outputs = np.zeros_like(outputs) _conv2d_forward_batch(inputs, weights, bias, true_outputs, padding, stride) _h.conv2d_forward_batch( inputs, weights, bias, outputs, padding, stride) passed = np.allclose(outputs, true_outputs) if not passed: print("Failed for 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_flatten_time_and_features(): # Testing for NumpyHandler only _h = NumpyHandler(np.float64) shape = (2, 3, 2, 4) x = np.random.randn(*shape) y = flatten_time_and_features(x).copy() yp = x.reshape((6, 8)) assert np.allclose(y, yp)
#!/usr/bin/env python # coding=utf-8 from __future__ import division, print_function, unicode_literals import numpy as np from brainstorm.handlers import DebugHandler, NumpyHandler from brainstorm.structure.buffers import (create_buffer_views_from_layout, get_total_size_slices_and_shapes) from brainstorm.structure.layout import create_layout np.random.seed(1234) HANDLER = DebugHandler(NumpyHandler(np.float64)) def approx_fprime(x0, f, epsilon, *args): """ Calculates the 2-sided numerical gradient of $f$. Args: x0 (ndarray): A 1-D array which specifies the point at which gradient is computed f (callable): A function whose gradient will be computed. Must return a scalar value epsilon (float): Perturbation value for numerical gradient calculation. args (tuple): Any arguments which need to be passed on to `f`. Returns:
import numpy as np import pytest from brainstorm.handlers import NumpyHandler from brainstorm.optional import has_pycuda non_default_handlers = [] handler_ids = [] if has_pycuda: from brainstorm.handlers import PyCudaHandler non_default_handlers.append(PyCudaHandler()) handler_ids.append("PyCudaHandler") # np.random.seed(1234) ref_dtype = np.float32 ref = NumpyHandler(ref_dtype) some_2d_shapes = ((1, 1), (4, 1), (1, 4), (5, 5), (3, 4), (4, 3)) some_nd_shapes = ((1, 1, 4), (1, 1, 3, 3), (3, 4, 2, 1)) np.set_printoptions(linewidth=150) def operation_check(handler, op_name, ref_args, ignored_args=(), atol=1e-8): args = get_args_from_ref_args(handler, ref_args) getattr(ref, op_name)(*ref_args) getattr(handler, op_name)(*args) check_list = [] for i, (ref_arg, arg) in enumerate(zip(ref_args, args)): if i in ignored_args: # print(i, "was ignored") continue
def net(request): n = Network.from_architecture(request.param) n.set_handler(NumpyHandler(dtype=np.float64)) n.initialize(Gaussian(1), seed=235) return n