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.name = "tensor1"
>>> print(tensor.name)
tensor1
>>> tensor.dtype = mslite.DataType.FLOAT32
>>> print(tensor.dtype)
DataType.FLOAT32
>>> tensor.shape = [1, 3, 2, 2]
>>> print(tensor.shape)
[1, 3, 2, 2]
>>> tensor.format = mslite.Format.NCHW
>>> print(tensor.format)
Format.NCHW
>>> print(tensor.element_num)
12
>>> print(tensor.data_size)
48
>>> print(tensor)
name: tensor1,
dtype: DataType.FLOAT32,
shape: [1, 3, 2, 2],
format: Format.NCHW,
element_num: 12,
data_size: 48.
property data_size

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.

property dtype

Get the data type of the Tensor.

Returns

DataType, the data type of the Tensor.

property element_num

Get the element num of the Tensor.

Returns

int, the element num of the Tensor data.

property format

Get the format of the Tensor.

Returns

Format, the format of the Tensor.

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.shape = [1, 3, 2, 2]
>>> tensor.dtype = mslite.DataType.FLOAT32
>>> in_data = np.arange(1 * 3 * 2 * 2, 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.]]]]
property name

Get the name of the Tensor.

Returns

str, the name of the Tensor.

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.shape = [1, 3, 224, 224]
>>> tensor.dtype = mslite.DataType.FLOAT32
>>> in_data = np.fromfile("input.bin", dtype=np.float32)
>>> tensor.set_data_from_numpy(in_data)
>>> print(tensor)
name: ,
dtype: DataType.FLOAT32,
shape: [1, 3, 224, 224],
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.shape = [1, 3, 2, 2]
>>> tensor.dtype = mslite.DataType.FLOAT32
>>> in_data = np.arange(1 * 3 * 2 * 2, dtype=np.float32)
>>> tensor.set_data_from_numpy(in_data)
>>> print(tensor)
name: ,
dtype: DataType.FLOAT32,
shape: [1, 3, 2, 2],
format: Format.NCHW,
element_num: 12,
data_size: 48.
property shape

Get the shape of the Tensor.

Returns

list[int], the shape of the Tensor.