---
title: CreateTensor
description: "| 产品 | 是否支持 |"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/ascendgraphapi/atlasgeapi_07_0468.html
sourcePath: /source/zh/canncommercial/900/API/ascendgraphapi/atlasgeapi_07_0468.html
indexId: 15f733147aab144aaa23a8e2df84b4291a61fdd4495e37fad3fac7c8080b32c572
---
# CreateTensor

#### 产品支持情况

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


#### 头文件/库文件

- 头文件：#include <ge/es_graph_builder.h>
- 库文件：libeager_style_graph_builder_base.so、libeager_style_graph_builder_base_static.a


#### 功能说明

创建指定数据类型和张量形状的张量。


#### 函数原型

```
template <typename T>
std::unique_ptr<Tensor> CreateTensor(const std::vector<T> &value, const std::vector<int64_t> &dims, DataType dt, Format format = FORMAT_ND);

template <typename T>
std::unique_ptr<Tensor> CreateTensor(const T *value, const int64_t *dims, int64_t dim_num, DataType dt, Format format = FORMAT_ND);
```


#### 参数说明

| 参数名 | 输入/输出 | 说明 |
| --- | --- | --- |
| T | 输入 | 张量数据类型。 |
| value | 输入 | 张量数据指针。 |
| dims | 输入 | 张量维度数组指针。 |
| dim\_num | 输入 | 张量维度数量。 |
| dt | 输入 | 张量的数据类型。 |
| format | 输入 | 张量格式，默认为FORMAT\_ND。 |


#### 返回值说明

| 参数名 | 类型 | 说明 |
| --- | --- | --- |
| \- | std::unique\_ptr<Tensor> | 返回创建的张量智能指针，失败时返回nullptr。 |


#### 约束说明

无


#### 调用示例

模版一：

```
EsGraphBuilder builder("graph_name");
std::vector<float> data = {1.0f, 2.0f, 3.0f};
std::vector<int64_t> dims = {3};
auto tensor = builder.CreateTensor<float>(data, dims, ge::DT_FLOAT);
```

模版二：

```
std::vector<float> data = {1.0f, 2.0f, 3.0f};
std::vector<int64_t> dims = {3};
auto tensor = ge::es::CreateTensor<float>(data.data(), dims.data(), dims.size(), ge::DT_FLOAT);
```
