mxnet.numpy_extension.utils¶
Util functions for the numpy module.
Functions
|
Load arrays from |
|
Save an array to a binary file in NumPy |
|
Save several arrays into a single file in uncompressed |
- mxnet.numpy_extension.utils.from_dlpack(dlpack)¶
Returns a np.ndarray backed by a dlpack tensor.
- Parameters:
dlpack (PyCapsule (the pointer of DLManagedTensor)) – input data
- Returns:
an ndarray backed by a dlpack tensor
- Return type:
np.ndarray
Examples
>>> x = mx.np.ones((2,3)) >>> y = mx.npx.to_dlpack_for_read(x) >>> type(y) <class 'PyCapsule'> >>> z = mx.npx.from_dlpack(y) >>> type(z) <class 'mxnet.numpy.ndarray'> >>> z array([[1., 1., 1.], [1., 1., 1.]])
>>> w = mx.npx.to_dlpack_for_write(x) >>> type(w) <class 'PyCapsule'> >>> u = mx.npx.from_dlpack(w) >>> u += 1 >>> x array([[2., 2., 2.], [2., 2., 2.]])
- mxnet.numpy_extension.utils.from_numpy(ndarray, zero_copy=True)¶
Returns an MXNet’s np.ndarray backed by numpy’s ndarray. When zero_copy is set to be true, this API consumes numpy’s ndarray and produces MXNet’s np.ndarray without having to copy the content. In this case, we disallow users to modify the given numpy ndarray, and it is suggested not to read the numpy ndarray as well for internal correctness.
- Parameters:
ndarray (np.ndarray) – input data
zero_copy (bool) – Whether we use DLPack’s zero-copy conversion to convert to MXNet’s np.ndarray. This is only available for c-contiguous arrays, i.e. array.flags[C_CONTIGUOUS] == True.
- Returns:
a np.ndarray backed by a dlpack tensor
- Return type:
np.ndarray
- mxnet.numpy_extension.utils.load(file)[source]¶
Load arrays from
.npy,.npzor legacy MXNet file format.See more details in
save.- Parameters:
file (str) – The filename.
- Returns:
result – Data stored in the file.
- Return type:
Notes
This function can only be called within numpy semantics, i.e., npx.is_np_shape() and npx.is_np_array() must both return true.
- mxnet.numpy_extension.utils.save(file, arr)[source]¶
Save an array to a binary file in NumPy
.npyformat.- Parameters:
See also
savezSave several arrays into a
.npzarchive
Notes
For a description of the
.npyformat, seenumpy.lib.format.
- mxnet.numpy_extension.utils.savez(file, *args, **kwds)[source]¶
Save several arrays into a single file in uncompressed
.npzformat.If arguments are passed in with no keywords, the corresponding variable names, in the
.npzfile, are ‘arr_0’, ‘arr_1’, etc. If keyword arguments are given, the corresponding variable names, in the.npzfile will match the keyword names.- Parameters:
file (str) – Either the filename (string) or an open file (file-like object) where the data will be saved.
args (Arguments, optional) – Arrays to save to the file. Since it is not possible for Python to know the names of the arrays outside savez, the arrays will be saved with names “arr_0”, “arr_1”, and so on. These arguments can be any expression.
kwds (Keyword arguments, optional) – Arrays to save to the file. Arrays will be saved in the file with the keyword names.
- Return type:
None
See also
saveSave a single array to a binary file in NumPy format.
Notes
The
.npzfile format is a zipped archive of files named after the variables they contain. The archive is not compressed and each file in the archive contains one variable in.npyformat. For a description of the.npyformat, seenumpy.lib.format.When opening the saved
.npzfile with load a dictionary object mapping file-names to the arrays themselves.When saving dictionaries, the dictionary keys become filenames inside the ZIP archive. Therefore, keys should be valid filenames. E.g., avoid keys that begin with
/or contain..
- mxnet.numpy_extension.utils.to_dlpack_for_read(data)¶
Returns a reference view of np.ndarray that represents as DLManagedTensor until all previous write operations on the current array are finished.
- data: np.ndarray
input data.
- PyCapsule (the pointer of DLManagedTensor)
a reference view of ndarray that represents as DLManagedTensor.
>>> x = mx.np.ones((2,3)) >>> y = mx.npx.to_dlpack_for_read(x) >>> type(y) <class 'PyCapsule'> >>> z = mx.npx.from_dlpack(y) >>> z array([[1., 1., 1.], [1., 1., 1.]])
- mxnet.numpy_extension.utils.to_dlpack_for_write(data)¶
Returns a reference view of ndarray that represents as DLManagedTensor until all previous read/write operations on the current array are finished.
- data: np.ndarray
input data.
- PyCapsule (the pointer of DLManagedTensor)
a reference view of np.ndarray that represents as DLManagedTensor.
>>> x = mx.np.ones((2,3)) >>> w = mx.npx.to_dlpack_for_write(x) >>> type(w) <class 'PyCapsule'> >>> u = mx.npx.from_dlpack(w) >>> u += 1 >>> x array([[2., 2., 2.], [2., 2., 2.]])