def __init__(self, value=None, shape=None, dtype=None, device=None, name=''): if not device: device = use_default_device() if (np.isscalar(value) or isinstance(value, np.ndarray)) and not shape: shape = () if dtype is not None: if isinstance(value, np.ndarray) and dtype != value.dtype: value = np.array(value, dtype=dtype) else: if isinstance(value, np.ndarray): dtype = value.dtype else: dtype = np.float32 if np.isscalar(value): super(Constant, self).__init__(sanitize_shape(shape), sanitize_dtype_cntk(dtype), value, device, name) else: ndav = sanitize_value(shape, value, dtype, device) super(Constant, self).__init__(ndav, name)
def __init__(self, shape=None, init=None, dtype=None, device=None, name=''): if not device: device = use_default_device() if dtype is not None: if isinstance(init, np.ndarray) and dtype != init.dtype: init = np.array(init, dtype=dtype) else: if np.isscalar(init) and not shape: shape = () if isinstance(init, np.ndarray): dtype = init.dtype else: dtype = np.float32 if init is None: init = 0 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, data_type, device=None): from cntk.internal import sanitize_shape, sanitize_dtype_cntk shape = sanitize_shape(shape) data_type = sanitize_dtype_cntk(data_type) if device is None: device = use_default_device() super(NDArrayView, self).__init__(data_type, cntk_py.StorageFormat_Dense, shape, device)
def __init__(self, shape=None, dtype=None, needs_gradient=False, is_sparse=False, dynamic_axes=[cntk_py.Axis.default_batch_axis(), cntk_py.Axis.default_dynamic_axis()], name=''): shape = sanitize_shape(shape) if dtype is None: dtype = np.float32 dtype = sanitize_dtype_cntk(dtype) dynamic_axes = sanitize_dynamic_axes(dynamic_axes) super(Variable, self).__init__(shape, is_sparse, dtype, needs_gradient, name, dynamic_axes)
def __init__(self, value=None, shape=None, dtype=None, device=None, name=''): if dtype is None: if isinstance(value, np.ndarray): dtype = value.dtype else: dtype = np.float32 if device is None: device = DeviceDescriptor.use_default_device() if np.isscalar(value): super(Constant, self).__init__(sanitize_shape(shape), sanitize_dtype_cntk(dtype), value, device, name) else: ndav = sanitize_value(shape, value, dtype, device) super(Constant, self).__init__(ndav, name)
def __init__(self, shape=None, init=None, dtype=None, device=None, name=''): if dtype is None: if isinstance(init, np.ndarray): dtype = init.dtype else: dtype = np.float32 if init is None: init = 0 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 __getitem__(self, shape): shape = sanitize_shape(shape) return Variable._Type(shape, **kwargs) # inject it for @Function
def create(var, data, seq_starts=None, device=None, read_only=False): ''' Creates a :class:`~cntk.core.Value` object. Args: var (:class:`~cntk.variables.Variable`): variable into which ``data`` is passed data: data for `var`. It can be: * a single NumPy array denoting the full minibatch * a list of NumPy arrays or SciPy sparse CSR matrices * a single NumPy array denoting one parameter or constant seq_starts (list of `bool`\ s or None): if None, every sequence is treated as a new sequence. Otherwise, it is interpreted as a list of Booleans that tell whether a sequence is a new sequence (`True`) or a continuation of the sequence in the same slot of the previous minibatch (`False`) device (:class:`~cntk.device.DeviceDescriptor`, default None): device this value should be put on read_only (bool, default False): whether the data is read only Returns: :class:`~cntk.core.Value` object. ''' if not isinstance(var, cntk_py.Variable): raise TypeError('Variable expected, but got "%s"' % type(var)) if not var.dynamic_axes: # No dynamic axes -> we can pass everything in one go data = Value._as_best_data_type(var, data) # Since the core API's Value does not copy single NDArrayViews, # we cannot borrow the memory here. ndav = NDArrayView.from_data(data, device=device, borrow=False) return cntk_py.Value(ndav) elif len(var.dynamic_axes) <= 1 and isinstance(data, list) and len(data) > 1: warnings.warn('you provided the minibatch data as a list, but ' 'your corresponding input variable (uid "%s") has ' 'only one dynamic axis (batch axis). To speed up ' 'graph execution, please convert the data ' 'beforehand into one NumPy array to speed up ' 'training.' % var.uid) if isinstance(data, cntk_py.NDArrayView): return cntk_py.Value(data) if isinstance(data, np.ndarray): # The outermost axis has to be Python list. If the user passes a # full minibatch as one NumPy array, we have to convert it. if data.dtype == object: raise ValueError( 'dtype object is not supported. If this is a ' 'batch of sequences, you need to pass them as a ' 'pure-Python list of NumPy arrays') if seq_starts: data = list(np.atleast_1d(data)) else: data = Value._as_best_data_type(var, data) ndav = NDArrayView.from_data(data, device) return cntk_py.Value(ndav) if isinstance(data, sparse.csr_matrix): if seq_starts: raise ValueError( 'seq_starts are not supported with data specified as a single ' 'SciPy sparse CSR matrix. Please provide a list instead') else: data = Value._as_best_data_type(var, data) ndav = NDArrayView.from_data(data, device) return cntk_py.Value(ndav) if not isinstance(data, list): raise ValueError('batch must be a list of NumPy arrays or ' 'SciPy CSR matrices') # NDArrayViews are all created on CPU. The Value object later then will # move it to the requested device. # As Value will later create copies anyways, we do not create copies in # NDArrayView itself. Because of that, we need to keep around the # instances _as_best_data_type() until we have passed them to # Value_create() where it will be copied further. data = [Value._as_best_data_type(var, sample) for sample in data] device = device or use_default_device() borrow = device.type() == DeviceKind.CPU list_of_ndavs = [ NDArrayView.from_data(sample, device=cpu(), borrow=borrow) for sample in data ] from cntk.internal import sanitize_shape value = cntk_py.Value_create(sanitize_shape(var.shape), list_of_ndavs, seq_starts or [], device, read_only, True) # always create a copy in Value return value
def create(var, data, seq_starts=None, device=None, read_only=False): ''' Creates a :class:`~cntk.core.Value` object. Args: var (:class:`~cntk.variables.Variable`): variable into which ``data`` is passed data: data for `var`. It can be: * a single NumPy array denoting the full minibatch * a list of NumPy arrays or SciPy sparse CSR matrices * a single NumPy array denoting one parameter or constant seq_starts (list of `bool`\ s or None): if None, every sequence is treated as a new sequence. Otherwise, it is interpreted as a list of Booleans that tell whether a sequence is a new sequence (`True`) or a continuation of the sequence in the same slot of the previous minibatch (`False`) device (:class:`~cntk.device.DeviceDescriptor`, default None): device this value should be put on read_only (bool, default False): whether the data is read only Returns: :class:`~cntk.core.Value` object. ''' if not isinstance(var, cntk_py.Variable): raise TypeError('Variable expected, but got "%s"' % type(var)) if not var.dynamic_axes: # No dynamic axes -> we can pass everything in one go data = Value._as_best_data_type(var, data) # Since the core API's Value does not copy single NDArrayViews, # we cannot borrow the memory here. ndav = NDArrayView.from_data(data, device=device, borrow=False) return cntk_py.Value(ndav) elif len(var.dynamic_axes) <= 1 and isinstance(data, list) and len(data) > 1: warnings.warn('you provided the minibatch data as a list, but ' 'your corresponding input variable (uid "%s") has ' 'only one dynamic axis (batch axis). To speed up ' 'graph execution, please convert the data ' 'beforehand into one NumPy array to speed up ' 'training.' % var.uid) if isinstance(data, cntk_py.NDArrayView): return cntk_py.Value(data) if isinstance(data, np.ndarray): # The outermost axis has to be Python list. If the user passes a # full minibatch as one NumPy array, we have to convert it. if data.dtype == object: raise ValueError('dtype object is not supported. If this is a ' 'batch of sequences, you need to pass them as a ' 'pure-Python list of NumPy arrays') if seq_starts: data = list(np.atleast_1d(data)) else: data = Value._as_best_data_type(var, data) ndav = NDArrayView.from_data(data, device) return cntk_py.Value(ndav) if isinstance(data, sparse.csr_matrix): if seq_starts: raise ValueError('seq_starts are not supported with data specified as a single ' 'SciPy sparse CSR matrix. Please provide a list instead') else: data = Value._as_best_data_type(var, data) ndav = NDArrayView.from_data(data, device) return cntk_py.Value(ndav) if not isinstance(data, list): raise ValueError('batch must be a list of NumPy arrays or ' 'SciPy CSR matrices') # NDArrayViews are all created on CPU. The Value object later then will # move it to the requested device. # As Value will later create copies anyways, we do not create copies in # NDArrayView itself. Because of that, we need to keep around the # instances _as_best_data_type() until we have passed them to # Value_create() where it will be copied further. data = [Value._as_best_data_type(var, sample) for sample in data] device = device or use_default_device() borrow = device.type() == DeviceKind.CPU list_of_ndavs = [NDArrayView.from_data(sample, device=cpu(), borrow=borrow) for sample in data] from cntk.internal import sanitize_shape value = cntk_py.Value_create( sanitize_shape(var.shape), list_of_ndavs, seq_starts or [], device, read_only, True) # always create a copy in Value return value