mindspore_lite.Tensor

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

The Tensor class defines a Tensor in MindSpore Lite.

Parameters

tensor (Tensor, optional) – The data to be stored in a new Tensor. It can be from 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.

data size of the Tensor = the element num of the Tensor * size of unit data type of the Tensor.

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")