neon.backends.backend.Tensor

class neon.backends.backend.Tensor(backend, shape=None, dtype=<type 'numpy.float32'>, name=None, persist_values=True)[source]

Bases: future.types.newobject.newobject

The n-dimensional array data structure. GPUTensor and Tensor inherits Tensor. Depending on backend, may have additional keyword arguments. All non-keywords arguments shall be in exact same order as Tensor.

Parameters:
  • backend (Backend) – backend of the tensor.
  • shape (tuple, optional) – shape of the tensor.
  • dtype (numpy.ndtype, optional) – underlying data type of the elements.
  • name (str, optional) – name identifying the tensor (used in printing).
  • persist_values (bool, optional) – If set to True (the default), the values assigned to this Tensor will persist across multiple begin and end calls. Setting to False may provide a performance increase if values do not need to be maintained across such calls

See also

GPUTensor class, Tensor class

Notes

Unlike numpy, in this implementation we never collapse dimensions, and the minimal number of dimensions will be _min_dims (currently set to 2). So a wrapped scalar will have dimension 1x1.

__init__(backend, shape=None, dtype=<type 'numpy.float32'>, name=None, persist_values=True)[source]

Methods

__init__(backend[, shape, dtype, name, …])
asnumpyarray() Convert the tensor to an in host memory numpy.ndarray.
copy(a) Construct and return a deep copy of the Tensor passed.
copy_from(a) Copy contents from a.
dimension_reorder(new_order) Re-orders dimensions of a tensor without preserving data
fill(value) Assign specified value to each element of this Tensor.
get() Copy tensor to host as numpy array.
hist(tag) Compute a histogram of the current tensor values.
next()
raw() Access the raw buffer.
reshape(*shape) Adjusts the dimensions of the data to the specified shape.
set(ary) Copy host array to the tensor.
take(indices, axis[, out]) Select a subset of elements from an array across an axis.
transpose([out]) Return a transposed view of the data.
T

Return a transposed view of the data.

Returns:transposed view of self.
Return type:Tensor
Raises:NotImplementedError – Can’t be instantiated directly.
asnumpyarray()[source]

Convert the tensor to an in host memory numpy.ndarray. A copy of the data may be made depending on where the Tensor normally resides.

Returns:numpy.ndarray view or copy of the Tensor data.
Raises:NotImplementedError – Can’t be instantiated directly.
copy(a)[source]

Construct and return a deep copy of the Tensor passed.

Parameters:a (Tensor) – the object to copy
Returns:new array object with the same values as tsr.
Return type:Tensor
Raises:NotImplementedError – Can’t be instantiated directly.
copy_from(a)[source]

Copy contents from a.

Parameters:a (numpy.ndarray) – the host-resident object to copy from
Raises:NotImplementedError – Can’t be instantiated directly.
dimension_reorder(new_order)[source]

Re-orders dimensions of a tensor without preserving data

Parameters:new_order (list) – new order of dimensions
fill(value)[source]

Assign specified value to each element of this Tensor.

Parameters:value (numeric) – The value to be assigned to each element.
Returns:updated view of the data.
Return type:Tensor
Raises:NotImplementedError – Can’t be instantiated directly.
get()[source]

Copy tensor to host as numpy array.

Returns:A host numpy array
Return type:numpy.ndarray
hist(tag)[source]

Compute a histogram of the current tensor values.

Parameters:tag (string) – Tag to identify the current state of the tensor, useful for disambiguating multiple histograms of the same tensor at different points in time.
Returns:Tensor containing the histogram data.
Raises:NotImplementedError – Can’t be instantiated directly.
next()
raw()[source]

Access the raw buffer.

Returns:A device specific pointer
Return type:pointer
reshape(*shape)[source]

Adjusts the dimensions of the data to the specified shape. The number of elements represented by the new shape must be the same as before.

Parameters:shape (int, list) – new length of each dimension
Raises:NotImplementedError – Can’t be instantiated directly.
set(ary)[source]

Copy host array to the tensor.

Parameters:ary (numpy.ndarray) – host array, needs to be contiguous
Returns:self
Return type:Tensor
take(indices, axis, out=None)[source]

Select a subset of elements from an array across an axis.

Parameters:
  • indices (Tensor, numpy ndarray) – indicies of elements to select
  • axis (int) – axis across which to select the values
  • out (Tensor, numpy ndarray, optional) – place the resultant values into this array if specified.
Returns:

Tensor with selected values

Return type:

Tensor

Raises:

NotImplementedError – Can’t be instantiated directly.

transpose(out=None)[source]

Return a transposed view of the data. Alias of .T property needed for MOP compatibility.

Parameters:out (Tensor, numpy ndarray, optional) – place the resultant values into this array if specified.
Returns:transposed view of self.
Return type:Tensor
Raises:NotImplementedError – Can’t be instantiated directly.