代码研读 Tensor类
Tensor类定义在tensorflow/core/framework/tensor.h中
Tensor定义了以下几个构造函数
//默认构造函数.初始化一个1维0元素的float张量
Tensor();
//另外一种构造函数Tensor(DT_FLOAT, TensorShape({}))
Tensor(DataType type, const TensorShape& shape);
//指定内存分配器,构造
Tensor(Allocator* a, DataType type, const TensorShape& shape);
/// 注释上来说直接在op中直接调用Tensor构造函数来创建张量的方式已经弃用了,现在是使用OpKernelConstruction/OpKernelContext allocate_*
// 的方式来创建一个新的张量
/// `a` must outlive the lifetime of this Tensor.
Tensor(Allocator* a, DataType type, const TensorShape& shape,
const AllocationAttributes& allocation_attr);
explicit Tensor(DataType type);
针对标量(单个数字),专门定义了一个空的结构体来标记
本文将详细解析TensorFlow库中Tensor类的实现,包括其定义在tensorflow/core/framework/tensor.h中的构造函数,如标量构造、拷贝与移动构造、赋值操作等。此外,还会探讨核心成员变量shape_和buf_,以及Tensor类中声明的友元类和函数,这些友元主要为了方便访问内部资源和管理内存。
订阅专栏 解锁全文
1万+

被折叠的 条评论
为什么被折叠?



