def _sanitize_value(shape, value, dtype, device, is_param=False): np_dtype = utils.sanitize_dtype_numpy(dtype) cntk_dtype = utils.sanitize_dtype_cntk(dtype) if value is None: if shape is None: raise ValueError('you need to specify at least shape or value') shape = utils.sanitize_shape(shape) if is_param: # TODO: expose the initialization params ndav = NDArrayView.random_uniform_float(shape, -0.05, 0.05, 1, device) else: ndav = utils.create_NDArrayView(shape, cntk_dtype, device) else: if not isinstance(value, np.ndarray) or value.dtype != np_dtype: value = np.asarray(value, dtype=np_dtype) #TODO: check whether this copy operation from cpu to gpu is not needed if device != DeviceDescriptor_cpudevice(): ndav_cpu = utils.create_NDArrayView_from_NumPy(value) ndav = utils.create_NDArrayView(value.shape, data_type=cntk_dtype, dev=device) ndav.copy_from(ndav_cpu) else: ndav = utils.create_NDArrayView_from_NumPy(value, device) return ndav
def value(self, val): if isinstance(val, np.ndarray): ndarray = NDArrayView.from_dense(val.astype(self.dtype)) super(Parameter, self).set_value(ndarray) elif isinstance(val, cntk_py.NDArrayView): super(Parameter, self).set_value(val) else: raise TypeError("Unsupported value type: %s", type(val))
def test_native_binary_function(): # user functions need to be registered before being callable by python if not nopt.native_convolve_function_registered: pytest.skip("Could not find {0} library. " "Please check if HALIDE_PATH is configured properly " "and try building {1} again".format( 'Cntk.BinaryConvolution-' + C.__version__.rstrip('+'), 'Extnsibiliy\BinaryConvolution')) # be sure to only run on CPU, binary convolution does not have GPU support for now dev = C.cpu() # create an arbitrary input mimicking a realistic cifar input x = input((64, 28, 28)) # random filter weights for testing w = parameter((64, 64, 3, 3), init=np.reshape(2 * (np.random.rand(64 * 64 * 3 * 3) - .5), (64, 64, 3, 3)), dtype=np.float32, device=dev) # set the convolution parameters by passing in an attribute dictionary #attributes = {'stride' : 1, 'padding' : False, 'size' : 3} attributes = { 'stride': 1, 'padding': False, 'size': 3, 'h': 28, 'w': 28, 'channels': 64, 'filters': 64 } # define the binary convolution op op = ops.native_user_function('NativeBinaryConvolveFunction', [w, x], attributes, 'native_binary_convolve') # also define an op using python custom functions that should have the same output op2 = C.convolution(CustomMultibitKernel(w, 1), CustomSign(x), auto_padding=[False]) # create random input data x_data = NDArrayView.from_dense(np.asarray(np.reshape( 2 * (np.random.rand(64 * 28 * 28) - .5), (64, 28, 28)), dtype=np.float32), device=dev) # evaluate the CPP binary convolve result = op.eval({x: x_data}, device=dev) # evaluate the python emulator result2 = op2.eval({x: x_data}, device=dev) native_times_primitive = op.find_by_name('native_binary_convolve') # assert that both have the same result '''
def _to_cntk_dict_value(py_value): if isinstance(py_value, dict): return DictionaryValueFromDict(_py_dict_to_cntk_dict(py_value)) if isinstance(py_value, list): py_list = list(map(_to_cntk_dict_value, py_value)) return DictionaryValue(py_list) if isinstance(py_value, np.ndarray): py_value = NDArrayView.from_dense(py_value) return DictionaryValueFromNDArrayView(py_value) if py_value is None: return DictionaryValue() return DictionaryValue(py_value)
def _to_cntk_dict_value(py_value): if isinstance(py_value, dict): return DictionaryValueFromDict(_py_dict_to_cntk_dict(py_value)) if isinstance(py_value, list): py_list = list(map(_to_cntk_dict_value, py_value)) return DictionaryValue(py_list) if isinstance(py_value, np.ndarray): py_value = NDArrayView.from_dense(py_value) return DictionaryValueFromNDArrayView(py_value) if isinstance(py_value, cntk_py.training_double_parameter_schedule): return cntk_py.DictionaryValueFromTrainingDoubleParameterSchedule(py_value) if py_value is None: return DictionaryValue() return DictionaryValue(py_value)
def test_native_binary_function(): # user functions need to be registered before being callable by python if not nopt.native_convolve_function_registered: pytest.skip("Could not find {0} library. " "Please check if HALIDE_PATH is configured properly " "and try building {1} again" .format('Cntk.BinaryConvolution-' + C.__version__.rstrip('+'), 'Extnsibiliy\\BinaryConvolution')) # be sure to only run on CPU, binary convolution does not have GPU support for now dev = C.cpu() # create an arbitrary input mimicking a realistic cifar input x = input((64, 28, 28)) # random filter weights for testing w = parameter((64, 64, 3, 3), init=np.reshape(2*(np.random.rand(64*64*3*3)-.5), (64, 64, 3, 3)), dtype=np.float32, device=dev) # set the convolution parameters by passing in an attribute dictionary #attributes = {'stride' : 1, 'padding' : False, 'size' : 3} attributes = {'stride' : 1, 'padding' : False, 'size' : 3, 'h' : 28, 'w' : 28, 'channels' : 64, 'filters' : 64 } # define the binary convolution op op = ops.native_user_function('NativeBinaryConvolveFunction', [w, x], attributes, 'native_binary_convolve') # also define an op using python custom functions that should have the same output op2 = C.convolution(CustomMultibitKernel(w, 1), CustomSign(x), auto_padding = [False]) # create random input data x_data = NDArrayView.from_dense(np.asarray(np.reshape(2*(np.random.rand(64*28*28)-.5), (64, 28, 28)),dtype=np.float32), device=dev) # evaluate the CPP binary convolve result = op.eval({x : x_data}, device=dev) # evaluate the python emulator result2 = op2.eval({x : x_data}, device=dev) native_times_primitive = op.find_by_name('native_binary_convolve') # assert that both have the same result '''
def _to_cntk_dict_value(py_value): if isinstance(py_value, dict): return DictionaryValueFromDict(_py_dict_to_cntk_dict(py_value)) if isinstance(py_value, list): py_list = list(map(_to_cntk_dict_value, py_value)) return DictionaryValue(py_list) if isinstance(py_value, np.ndarray): py_value = NDArrayView.from_dense(py_value) return DictionaryValueFromNDArrayView(py_value) if isinstance(py_value, cntk_py.training_double_parameter_schedule): return cntk_py.DictionaryValueFromTrainingDoubleParameterSchedule( py_value) if py_value is None: return DictionaryValue() return DictionaryValue(py_value)
def test_native_binary_function(): # user functions need to be registered before being callable by python ops.register_native_user_function( 'NativeBinaryConvolveFunction', 'Cntk.BinaryConvolutionExample-' + C.__version__.rstrip('+'), 'CreateBinaryConvolveFunction') # be sure to only run on CPU, binary convolution does not have GPU support for now dev = cpu() # create an arbitrary input mimicking a realistic cifar input x = input((64, 30, 30)) # random filter weights for testing w = parameter((64, 64, 3, 3), init=np.reshape(2 * (np.random.rand(64 * 64 * 3 * 3) - .5), (64, 64, 3, 3)), dtype=np.float32, device=dev) # set the convolution parameters by passing in an attribute dictionary attributes = {'stride': 1, 'padding': False, 'size': 3} # define the binary convolution op op = ops.native_user_function('NativeBinaryConvolveFunction', [w, x], attributes, 'native_binary_convolve_function') # also define an op using python custom functions that should have the same output op2 = C.convolution(CustomMultibitKernel(w, 1), CustomSign(x), auto_padding=[False]) # create random input data x_data = NDArrayView.from_dense(np.asarray(np.reshape( 2 * (np.random.rand(64 * 30 * 30) - .5), (64, 30, 30)), dtype=np.float32), device=dev) # evaluate the CPP binary convolve result = op.eval({x: x_data}, device=dev) # evaluate the python emulator result2 = op2.eval({x: x_data}, device=dev) native_times_primitive = op.find_by_name('native_binary_convolve_function') # assert that both have the same result assert np.allclose(result, result2, atol=0.001)
def test_native_binary_function(): # user functions need to be registered before being callable by python ops.register_native_user_function('NativeBinaryConvolveFunction', 'Cntk.BinaryConvolutionExample-' + C.__version__.rstrip('+'), 'CreateBinaryConvolveFunction') # be sure to only run on CPU, binary convolution does not have GPU support for now dev = cpu() # create an arbitrary input mimicking a realistic cifar input x = input((64, 30, 30)) # random filter weights for testing w = parameter((64, 64, 3, 3), init=np.reshape(2*(np.random.rand(64*64*3*3)-.5), (64, 64, 3, 3)), dtype=np.float32, device=dev) # set the convolution parameters by passing in an attribute dictionary attributes = {'stride' : 1, 'padding' : False, 'size' : 3} # define the binary convolution op op = ops.native_user_function('NativeBinaryConvolveFunction', [w, x], attributes, 'native_binary_convolve_function') # also define an op using python custom functions that should have the same output op2 = C.convolution(CustomMultibitKernel(w, 1), CustomSign(x), auto_padding = [False]) # create random input data x_data = NDArrayView.from_dense(np.asarray(np.reshape(2*(np.random.rand(64*30*30)-.5), (64, 30, 30)),dtype=np.float32), device=dev) # evaluate the CPP binary convolve result = op.eval({x : x_data}, device=dev) # evaluate the python emulator result2 = op2.eval({x : x_data}, device=dev) native_times_primitive = op.find_by_name('native_binary_convolve_function') # assert that both have the same result assert np.allclose(result, result2, atol=0.001)
def test_Value_raises(): from cntk import NDArrayView, Value with pytest.raises(ValueError): nd = NDArrayView.from_dense(np.asarray([[[4, 5]]], dtype=np.float32)) val = Value(nd)
def test_Value_raises(): from cntk import NDArrayView, Value with pytest.raises(ValueError): nd = NDArrayView.from_dense(np.asarray([[[4,5]]], dtype=np.float32)) val = Value(nd)