---
title: TensorBase
description: "TensorBase的构造函数，用来创建TensorBase对象，根据传入的不同参数可选用不同的构造函数。"
url: https://www.hiascend.com/document/detail/zh/Atlas200IDKA2DeveloperKit/latest/Application%20Development%20Guide/msadg/mxvisionug_0425.html
sourcePath: /source/zh/Atlas200IDKA2DeveloperKit/23.0.RC2/Application Development Guide/msadg/mxvisionug_0425.html
indexId: 3210f9fb5d71410908c89b2828874a7aa8b854d039212eb1931943f31f064427102
---
# TensorBase

#### 函数功能

TensorBase的构造函数，用来创建TensorBase对象，根据传入的不同参数可选用不同的构造函数。

如出现因内存耗尽，导致构造函数执行失败抛出的异常时，请勿继续调用后续的成员函数。


#### 函数原型

```
TensorBase();
```

```
TensorBase(const MemoryData &memoryData, const bool &isBorrowed, const std::vector<uint32_t> &shape, const TensorDataType &type);
```

```
TensorBase(const std::vector<uint32_t> &shape, const TensorDataType &type, const MemoryData::MemoryType &bufferType, const int32_t &deviceId);
```

```
TensorBase(const std::vector<uint32_t> &shape, const TensorDataType &type, const int32_t &deviceId);
```

```
TensorBase(const std::vector<uint32_t> &shape, const TensorDataType &type);
```

```
TensorBase(const std::vector<uint32_t> &shape);
```

```
TensorBase(const TensorBase& tensor) = default;
```


#### 参数说明

- 函数原型2、3、4、5只是预先设置了shape，但不会申请内存空间，需要调用  TensorBaseMalloc
才能申请对应内存空间。
- 函数原型1使用场景：用户已在外部申请内存空间，构造Tensor对象的时候直接引用已申请的内存，需确保内存空间大小与Tensor对象的shape一致，同时可以选择外部申请的空间是Tensor对象内部自行释放还是用户外部释放。


| 参数名 | 输入/输出 | 说明 |
| --- | --- | --- |
| memoryData | 输入 | 用于构造TensorBase对象的参数，内存管理结构体，具体请参见MemoryData。 |
| isBorrowed | 输入 | 表示传入的MemoryData数据是否要Tensor主动释放。 若为“true”，表示不需要主动释放，用户自行释放。 若为“false”，用户不需要手动释放，Tensor析构时自动释放。 |
| shape | 输入 | 用于构造TensorBase对象的参数，张量的形状。 |
| type | 输入 | 用于构造TensorBase对象的参数，TensorDataType类型数据，具体请参见TensorDataType枚举说明。 |
| bufferType | 输入 | 用于构造TensorBase对象的参数，张量数据的内存类型。 |
| deviceId | 输入 | 用于构造TensorBase对象的参数，int类型数据，设备编号。 |
| tensor | 输入 | TensorBase对象，用于构造TensorBase对象的参数。 |
