mindspore_lite.Tensor

class mindspore_lite.Tensor(tensor=None)[source]

The Tensor class defines a tensor in MindSporeLite.

Parameters

tensor (Tensor, optional) – The data to be stored in a new tensor. It can be another Tensor. Default: None.

Raises

TypeErrortensor is neither a Tensor nor None.

Examples

>>> import mindspore_lite as mslite
>>> tensor = mslite.Tensor()
>>> tensor.set_data_type(mslite.DataType.FLOAT32)
>>> print(tensor)
tensor_name: ,
data_type: DataType.FLOAT32,
shape: [],
format: Format.NCHW,
element_num: 1,
data_size: 4.
get_data_size()[source]

Get the data size of the tensor, i.e., data_size = element_num * data_type.

Returns

int, the data size of the tensor data.

Examples

>>> # data_size is related to data_type
>>> import mindspore_lite as mslite
>>> tensor = mslite.Tensor()
>>> tensor.set_data_type(mslite.DataType.FLOAT32)
>>> size = tensor.get_data_size()
>>> print(size)
4
get_data_to_numpy()[source]

Get the data from the tensor to the numpy object.

Returns

numpy.ndarray, the numpy object from tensor data.

Examples

>>> import mindspore_lite as mslite
>>> import numpy as np
>>> tensor = mslite.Tensor()
>>> tensor.set_shape([1, 2, 2, 3])
>>> tensor.set_data_type(mslite.DataType.FLOAT32)
>>> in_data = np.arange(1 * 2 * 2 * 3, dtype=np.float32)
>>> tensor.set_data_from_numpy(in_data)
>>> data = tensor.get_data_to_numpy()
>>> print(data)
[[[[ 0.  1.  2.]
   [ 3.  4.  5.]]
  [[ 6.  7.  8.]
   [ 9. 10. 11.]]]]
get_data_type()[source]

Get the data type of the tensor.

Returns

DataType, the data type of the tensor.

Examples

>>> import mindspore_lite as mslite
>>> tensor = mslite.Tensor()
>>> tensor.set_data_type(mslite.DataType.FLOAT32)
>>> data_type = tensor.get_data_type()
>>> print(data_type)
DataType.FLOAT32
get_element_num()[source]

Get the element num of the tensor.

Returns

int, the element num of the tensor data.

Examples

>>> import mindspore_lite as mslite
>>> tensor = mslite.Tensor()
>>> num = tensor.get_element_num()
>>> print(num)
1
get_format()[source]

Get the format of the tensor.

Returns

Format, the format of the tensor.

Examples

>>> import mindspore_lite as mslite
>>> tensor = mslite.Tensor()
>>> tensor.set_format(mslite.Format.NHWC)
>>> tensor_format = tensor.get_format()
>>> print(tensor_format)
Format.NHWC
get_shape()[source]

Get the shape of the tensor.

Returns

list[int], the shape of the tensor.

Examples

>>> import mindspore_lite as mslite
>>> tensor = mslite.Tensor()
>>> tensor.set_shape([1, 112, 112, 3])
>>> shape = tensor.get_shape()
>>> print(shape)
[1, 112, 112, 3]
get_tensor_name()[source]

Get the name of the tensor.

Returns

str, the name of the tensor.

Examples

>>> import mindspore_lite as mslite
>>> tensor = mslite.Tensor()
>>> tensor.set_tensor_name("tensor0")
>>> tensor_name = tensor.get_tensor_name()
>>> print(tensor_name)
tensor0
set_data_from_numpy(numpy_obj)[source]

Set the data for the tensor from the numpy object.

Parameters

numpy_obj (numpy.ndarray) – the numpy object.

Raises
  • TypeErrornumpy_obj is not a numpy.ndarray.

  • RuntimeError – The data type of numpy_obj is not equivalent to the data type of the tensor.

  • RuntimeError – The data size of numpy_obj is not equal to the data size of the tensor.

Examples

>>> # 1. set tensor data which is from file
>>> import mindspore_lite as mslite
>>> import numpy as np
>>> tensor = mslite.Tensor()
>>> tensor.set_shape([1, 224, 224, 3])
>>> tensor.set_data_type(mslite.DataType.FLOAT32)
>>> in_data = np.fromfile("input.bin", dtype=np.float32)
>>> tensor.set_data_from_numpy(in_data)
>>> print(tensor)
tensor_name: ,
data_type: DataType.FLOAT32,
shape: [1, 224, 224, 3],
format: Format.NCHW,
element_num: 150528,
data_size: 602112.
>>> # 2. set tensor data which is numpy arange
>>> import mindspore_lite as mslite
>>> import numpy as np
>>> tensor = mslite.Tensor()
>>> tensor.set_shape([1, 2, 2, 3])
>>> tensor.set_data_type(mslite.DataType.FLOAT32)
>>> in_data = np.arange(1 * 2 * 2 * 3, dtype=np.float32)
>>> tensor.set_data_from_numpy(in_data)
>>> print(tensor)
tensor_name: ,
data_type: DataType.FLOAT32,
shape: [1, 2, 2, 3],
format: Format.NCHW,
element_num: 12,
data_size: 48.
set_data_type(data_type)[source]

Set data type for the Tensor.

Parameters

data_type (DataType) – The data type of the Tensor.

Raises

TypeErrordata_type is not a DataType.

Examples

>>> import mindspore_lite as mslite
>>> tensor = mslite.Tensor()
>>> tensor.set_data_type(mslite.DataType.FLOAT32)
set_format(tensor_format)[source]

Set format of the tensor.

Parameters

tensor_format (Format) – The format of the tensor.

Raises

TypeErrortensor_format is not a Format.

Examples

>>> import mindspore_lite as mslite
>>> tensor = mslite.Tensor()
>>> tensor.set_format(mslite.Format.NHWC)
set_shape(shape)[source]

Set shape for the tensor.

Parameters

shape (list[int]) – The shape of the tensor.

Raises
  • TypeErrorshape is not a list.

  • TypeErrorshape is a list, but the elements is not int.

Examples

>>> import mindspore_lite as mslite
>>> tensor = mslite.Tensor()
>>> tensor.set_shape([1, 112, 112, 3])
set_tensor_name(tensor_name)[source]

Set the name of the tensor.

Parameters

tensor_name (str) – The name of the tensor.

Raises

TypeErrortensor_name is not a str.

Examples

>>> import mindspore_lite as mslite
>>> tensor = mslite.Tensor()
>>> tensor.set_tensor_name("tensor0")