---
title: Tensor构造函数
description: "| 产品 | 是否支持 |"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/ascendgraphapi/atlasgeapi_07_0750.html
sourcePath: /source/zh/canncommercial/900/API/ascendgraphapi/atlasgeapi_07_0750.html
indexId: e85ce53bfd9dd40a0179a520284a67dea4b65af8b1a1c56ab6cdf2cf372a40df72
---
# Tensor构造函数

#### 产品支持情况

| 产品 | 是否支持 |
| --- | --- |
| Atlas 350 加速卡 | √ |
| Atlas A3 训练系列产品 / Atlas A3 推理系列产品 | √ |
| Atlas A2 训练系列产品 / Atlas A2 推理系列产品 | √ |
| Atlas 200I/500 A2 推理产品 | √ |
| Atlas 推理系列产品 | √ |
| Atlas 训练系列产品 | √ |


#### 功能说明

创建Tensor对象。


#### 函数原型

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


#### 参数说明

| 参数名 | 输入/输出 | 描述 |
| --- | --- | --- |
| data | 输入 | 数据列表。 |
| file\_path | 输入 | 从文件读取的路径。 |
| data\_type | 输入 | 数据类型，默认为DT\_FLOAT。 |
| format | 输入 | 数据格式，默认为FORMAT\_ND。 |
| shape | 输入 | 形状维度列表，None表示标量。 |


#### 返回值说明

无


#### 约束说明

- data和file_path必须二选一。
- 如果两者都提供，抛出RuntimeError。
- shape必须是整数列表。
- DT_DOUBLE类型在Python Tensor构造函数中不支持。


#### 调用示例

```
# 从数据创建
tensor1 = Tensor(data=[1.0, 2.0, 3.0], data_type=DataType.DT_FLOAT, format=Format.FORMAT_ND, shape=[3])

# 从文件创建
tensor2 = Tensor(file_path="/path/to/file", data_type=DataType.DT_FLOAT, format=Format.FORMAT_ND, shape=[2, 2])
```
