Tensor Constructor

Function Usage

Creates a tensor object.

Prototype

1
Tensor(data: Optional[UnionTensorDataType] = None, file_path: Optional[str] = None, data_type: Optional[DataType] = DataType.DT_FLOAT, format: Optional[Format] = Format.FORMAT_ND, shape: Optional[List[int]] = None)

Parameters

Parameter

Input/Output

Description

data

Input

Data list.

file_path

Input

Path for reading data from a file.

data_type

Input

Data type. The default value is DT_FLOAT.

format

Input

Data format. The default value is FORMAT_ND.

shape

Input

Shape dimension list. None indicates a scalar.

Returns

None

Constraints

  • Either data or file_path must be specified.
  • If both are provided, RuntimeError is thrown.
  • The shape must be an integer list.
  • The DT_DOUBLE type is not supported in Python tensor constructors.

Examples

1
2
3
4
5
# Create from data.
tensor1 = Tensor(data=[1.0, 2.0, 3.0], data_type=DataType.DT_FLOAT, format=Format.FORMAT_ND, shape=[3])

# Create from a file.
tensor2 = Tensor(file_path="/path/to/file", data_type=DataType.DT_FLOAT, format=Format.FORMAT_ND, shape=[2, 2])