def test_OptimizedRNNStack(bidirectional, num_layers, input_size, hidden_size, recurrent_op, tmpdir, device_id): pytest.skip('Need to support new ONNX spec.') if device_id == -1: pytest.skip('Test only runs on GPU') dev = cntk_device(device_id) from _cntk_py import constant_initializer model_filename = 'optimized_rnn_stack_' + ( 'bi' if bidirectional else 'uni') + '_layers' + str( num_layers) + '_inp' + str(input_size) + '_hid' + str(hidden_size) W = C.parameter((C.InferredDimension, input_size), constant_initializer(0.1), device=dev) x = C.sequence.input_variable(shape=(input_size, )) s = np.asarray(np.random.uniform(-1, 1, (5, input_size)), dtype=np.float32) f = C.optimized_rnnstack(x, W, hidden_size, num_layers, bidirectional=bidirectional, recurrent_op=recurrent_op, name='MyRnnStack') f.parameters[0].value = np.reshape( np.arange(np.prod(f.parameters[0].value.shape), dtype=np.float32), f.parameters[0].value.shape) verify_one_input(f, s, tmpdir, model_filename)
def _initializer_for(init, rank_params=None): if init is None: raise ValueError("init parameter cannot be None") # if default then select if init is _default_sentinel_init: init = _current_default_options.init elif init is _default_sentinel_init_bias: init = _current_default_options.init_bias # scalar constant: that's it, nothing further to do here if np.isscalar(init): # BUGBUG: this is sometimes required when dimensions are unknown; shouldn't. from _cntk_py import constant_initializer return constant_initializer(init) # return init # TODO: change to this once this works, e.g. for layers.BatchNormalization() # implant additional rank parameters if rank_params: from cntk.initializer import initializer_with_rank init = initializer_with_rank(init, **rank_params) return init
def test_OptimizedRNNStack(bidirectional, num_layers, input_size, hidden_size, recurrent_op, tmpdir, device_id): pytest.skip('Need to support new ONNX spec.') if device_id == -1: pytest.skip('Test only runs on GPU') dev = cntk_device(device_id) from _cntk_py import constant_initializer model_filename = 'optimized_rnn_stack_' + ('bi' if bidirectional else 'uni') + '_layers' + str(num_layers) + '_inp' + str(input_size) + '_hid' + str(hidden_size) W = C.parameter((C.InferredDimension, input_size), constant_initializer(0.1), device=dev) x = C.sequence.input_variable(shape=(input_size,)) s = np.asarray(np.random.uniform(-1, 1, (5,input_size)), dtype=np.float32) f = C.optimized_rnnstack(x, W, hidden_size, num_layers, bidirectional=bidirectional, recurrent_op=recurrent_op, name='MyRnnStack') f.parameters[0].value = np.reshape(np.arange(np.prod(f.parameters[0].value.shape), dtype=np.float32), f.parameters[0].value.shape) verify_one_input(f, s, tmpdir, model_filename)
def _initializer_for(init, rank_params=None): if init is None: raise ValueError("init parameter cannot be None") # scalar constant: that's it, nothing further to do here if np.isscalar(init): # BUGBUG: this is sometimes required when dimensions are unknown; shouldn't. from _cntk_py import constant_initializer return constant_initializer(init) #return init # TODO: change to this once this works, e.g. for layers.BatchNormalization() # implant additional rank parameters if rank_params: from cntk.initializer import initializer_with_rank init = initializer_with_rank(init, **rank_params) return init
def __init__(self, shape=None, init=None, dtype=default_override_or(np.float32), device=None, name=''): if not device: device = use_default_device() if init is None: init = 0 pure = get_default_override(None, pure=default_override_or(False)) if pure: raise TypeError( 'parameters cannot be created inside a @Function def') from _cntk_py import constant_initializer if np.isscalar(init): init = constant_initializer(init) if not shape: shape = () dtype = get_default_override(Parameter, dtype=dtype) if dtype is not None: if isinstance(init, np.ndarray) and dtype != init.dtype: init = np.array(init, dtype=dtype) else: if isinstance(init, np.ndarray): dtype = init.dtype else: dtype = np.float32 if isinstance(init, (np.ndarray, list, float, int)): ndav = sanitize_value(shape, init, dtype, device) super(Parameter, self).__init__(ndav, name) else: shape = sanitize_shape(shape) cntk_dtype = sanitize_dtype_cntk(dtype) super(Parameter, self).__init__(shape, cntk_dtype, init, device, name)
def __init__(self, shape=None, init=None, dtype=default_override_or(np.float32), device=None, name=''): if not device: device = use_default_device() if init is None: init = 0 pure = get_default_override(None, pure=default_override_or(False)) if pure: raise TypeError('parameters cannot be created inside a @Function def') from _cntk_py import constant_initializer if np.isscalar(init): init = constant_initializer(init) if not shape: shape = () dtype = get_default_override(Parameter, dtype=dtype) if dtype is not None: if isinstance(init, np.ndarray) and dtype != init.dtype: init = np.array(init, dtype=dtype) else: if isinstance(init, np.ndarray): dtype = init.dtype else: dtype = np.float32 if isinstance(init, (np.ndarray, list, float, int)): ndav = sanitize_value(shape, init, dtype, device) super(Parameter, self).__init__(ndav, name) else: shape = sanitize_shape(shape) cntk_dtype = sanitize_dtype_cntk(dtype) super(Parameter, self).__init__(shape, cntk_dtype, init, device, name)