Go to the documentation of this file.
26 #ifndef MXNET_CPP_NDARRAY_H_
27 #define MXNET_CPP_NDARRAY_H_
139 NDArray(
const std::vector<mx_uint>& shape,
141 bool delay_alloc =
true,
150 NDArray(
const Shape& shape,
const Context& context,
bool delay_alloc =
true,
int dtype = 0);
166 explicit NDArray(
const std::vector<mx_float>& data);
296 void SyncCopyToCPU(std::vector<mx_float>* data,
size_t size = 0);
315 size_t Offset(
size_t h = 0,
size_t w = 0)
const;
323 size_t Offset(
size_t c,
size_t h,
size_t w)
const;
344 mx_float At(
size_t c,
size_t h,
size_t w)
const;
395 static void Load(
const std::string& file_name,
396 std::vector<NDArray>* array_list =
nullptr,
397 std::map<std::string, NDArray>* array_map =
nullptr);
403 static std::map<std::string, NDArray>
LoadToMap(
const std::string& file_name);
409 static std::vector<NDArray>
LoadToList(
const std::string& file_name);
421 std::vector<NDArray>* array_list =
nullptr,
422 std::map<std::string, NDArray>* array_map =
nullptr);
429 static std::map<std::string, NDArray>
LoadFromBufferToMap(
const void* buffer,
size_t size);
442 static void Save(
const std::string& file_name,
const std::map<std::string, NDArray>& array_map);
448 static void Save(
const std::string& file_name,
const std::vector<NDArray>& array_list);
456 std::vector<mx_uint>
GetShape()
const;
476 return blob_ptr_->handle_;
480 std::shared_ptr<NDBlob> blob_ptr_;
487 #endif // MXNET_CPP_NDARRAY_H_
namespace of mxnet
Definition: api_registry.h:33
void WaitToWrite()
Block until all the pending read/write operations with respect to current NDArray are finished,...
static Context cpu(int device_id=0)
Return a CPU context.
Definition: ndarray.h:80
@ kGPU
Definition: ndarray.h:40
void SyncCopyFromCPU(const mx_float *data, size_t size)
Do a synchronize copy from a contiguous CPU memory region.
ScalarExp< DType > scalar(DType s)
create an scalar expression
Definition: expression.h:103
NDBlob()
default constructor
Definition: ndarray.h:97
static void SampleGaussian(mx_float mu, mx_float sigma, NDArray *out)
Sample gaussian distribution for each elements of out.
static void LoadFromBuffer(const void *buffer, size_t size, std::vector< NDArray > *array_list=nullptr, std::map< std::string, NDArray > *array_map=nullptr)
Load NDArrays from buffer.
void SyncCopyToCPU(mx_float *data, size_t size=0)
Do a synchronize copy to a contiguous CPU memory region.
NDArray & operator-=(mx_float scalar)
elementwise subtract from current ndarray this mutate the current NDArray
NDArray()
construct with a none handle
~NDBlob()
destructor, free the NDArrayHandle
Definition: ndarray.h:106
static void WaitAll()
Block until all the pending read/write operations with respect to current NDArray are finished,...
static void Save(const std::string &file_name, const std::map< std::string, NDArray > &array_map)
save a map of string->NDArray to binary file.
std::ostream & operator<<(std::ostream &out, const NDArray &ndarray)
DeviceType GetDeviceType() const
Definition: ndarray.h:56
NDArray interface.
Definition: ndarray.h:122
size_t Offset(size_t h=0, size_t w=0) const
return offset of the element at (h, w)
static void SampleUniform(mx_float begin, mx_float end, NDArray *out)
Sample uniform distribution for each elements of out.
Context interface.
Definition: ndarray.h:45
struct to store NDArrayHandle
Definition: ndarray.h:92
int GetDeviceId() const
Definition: ndarray.h:62
NDArray & operator+=(mx_float scalar)
elementwise add to current space this mutate the current NDArray
NDArray operator*(mx_float scalar)
NDBlob(NDArrayHandle handle)
construct with a NDArrayHandle
Definition: ndarray.h:102
NDArray & operator%=(mx_float scalar)
elementwise modulo from current ndarray this mutate the current NDArray
@ kCPUPinned
Definition: ndarray.h:40
NDArray & operator=(mx_float scalar)
set all the elements in ndarray to be scalar
static std::vector< NDArray > LoadToList(const std::string &file_name)
Load list of NDArrays from binary file.
float mx_float
manually define float
Definition: c_api.h:67
NDArrayHandle handle_
the NDArrayHandle
Definition: ndarray.h:112
ndarray interface
Definition: ndarray.h:82
NDArray & operator/=(mx_float scalar)
elementwise division from current ndarray this mutate the current NDArray
NDArray Slice(mx_uint begin, mx_uint end) const
Slice a NDArray.
static std::map< std::string, NDArray > LoadFromBufferToMap(const void *buffer, size_t size)
Load map of NDArrays from buffer.
MXNET_DLL int MXNDArrayFree(NDArrayHandle handle)
free the narray handle
NDArray operator+(mx_float scalar)
std::vector< mx_uint > GetShape() const
NDArray operator/(mx_float scalar)
NDArray Reshape(const Shape &new_shape) const
Return a reshaped NDArray that shares memory with current one.
dynamic shape class that can hold shape of arbirary dimension
Definition: shape.h:42
NDArray CopyTo(NDArray *other) const
copy the content of current array to a target array.
Definition: ndarray_handle.h:40
NDArrayHandle GetHandle() const
Definition: ndarray.h:475
NDArray operator-(mx_float scalar)
static void Load(const std::string &file_name, std::vector< NDArray > *array_list=nullptr, std::map< std::string, NDArray > *array_map=nullptr)
Load NDArrays from binary file.
static Context gpu(int device_id=0)
Return a GPU context.
Definition: ndarray.h:71
mx_float At(size_t index) const
return value of the element at (index)
NDArray & operator*=(mx_float scalar)
elementwise multiplication to current ndarray this mutate the current NDArray
NDArray Copy(const Context &) const
return a new copy to this NDArray
base definitions for mxnetcpp
static std::vector< NDArray > LoadFromBufferToList(const void *buffer, size_t size)
Load list of NDArrays from buffer.
NDArray operator%(mx_float scalar)
void WaitToRead() const
Block until all the pending write operations with respect to current NDArray are finished,...
uint32_t mx_uint
manually define unsigned int
Definition: c_api.h:65
@ kCPU
Definition: ndarray.h:40
static std::map< std::string, NDArray > LoadToMap(const std::string &file_name)
Load map of NDArrays from binary file.
DeviceType
Definition: ndarray.h:40
Context GetContext() const
const mx_float * GetData() const
Get the pointer to data (IMPORTANT: The ndarray should not be in GPU)
Context(const DeviceType &type, int id)
Context constructor.
Definition: ndarray.h:52