实现带有capacity的多维数组tensor

dianyancao 2016-02-16 02:41:46
在实现多维数组,输入的多维数组会改变size
看了两个库
boost和Eigen
boost的resize每次都会重新申请内存,这样效率比较低
Eigen有tensor类型,但是没有使用文档支持,不会用
因此想自己实现,有好用的库推荐吗?

【一些基本的操作】
resize改变tensor大小,有容量capacity,当tensor大小小于容量时不重新申请内存
reshape改变tensor形状,不改变其大小
transpose转置指定的维度
flip翻转指定的维度
squeeze去除大小为1的维度
operator[](size_t)当数据存放是连续时,按一维下标访问元素

转置和翻转是常数时间操作,只改变访问tensor元素的方式(线性索引),不复制数据
【tensor】
tensor持有数据,支持resize,其数据存放是连续的,支持reshape,切割后返回tensor_view
【tensor_view】
tensor_view将某段内存按tensor解释,不支持resize,其数据存放可以是不连续的,
当其查看的数据是连续时,支持reshape,切割后返回tensor_view
【tensor_base】
tensor和tensor_view继承自tensor_base,tensor_base提供tensor和tensor_view的共同成员

为了访问效率,编译时应该确定tensor的rank维度数?
tensor内部数据data_采用共享指针std::share_ptr<std::vector<T>>管理new出的std::vector,
tensor_view内部数据data_准备采用数据指针或者std::vector<T>::iterator迭代器
为了避免直接用原始指针访问数据带来的不安全,有更好的方案提供内部数据吗?

以下是代码:
tensor_view_t.h 空
const_tensor_view_t.h 空

tensor_base_t.h
#include <vector>
#include <memory>
#include <algorithm>
#include <assert.h>

typedef int layer_size_t;

template<typename T>
class range_t
{
public:
range_t()
{
full_ = true;
}
range_t(T begin, T end, T step = T(1))
{
assert(begin < end);
begin_ = begin;
end_ = end;
step_ = step;
full_ = false;
}
size_t size()
{
size_t ret = (end_ - begin_) / step_ + 1;
if ((end_ - begin_) % step_ == 0) ret--;
return ret;
}
size_t size() const
{
size_t ret = (end_ - begin_) / step_ + 1;
if ((end_ - begin_) % step_ == 0) ret--;
return ret;
}
T begin_;
T end_;
T step_;
bool full_;
};

typedef range_t<size_t> range_type;

template<typename T>
class extents_t
{
public:
extents_t<T>& operator[](const range_t<T>& range)
{
ranges.push_back(range);
return *this;
}
extents_t<T>& operator[](T index)
{
ranges.push_back(range_t<T>(index, index + 1));
return *this;
}
std::vector<range_t<T>> ranges;
};

typedef extents_t<size_t> extents_type;

template<typename derived,typename value_type,typename data_type>
class tensor_base_t
{
public:
typedef typename int index_value;//signed
typedef typename std::vector<index_value> shape_type;
typedef typename shape_type index_type;
typedef typename derived tensor_type;
typedef typename value_type value_type;
typedef typename std::vector<value_type> vector_type;
typedef typename data_type data_type;
protected:
data_type data_;
bool continuous_;

int offset_;
shape_type shape_;
index_type steps_;
size_t size_;
public:
tensor_base_t() = default;
void assign(const std::initializer_list<value_type>& vec)
{
assert(size_ == vec.size());
(*data_).assign(vec.begin(), vec.end());
}
derived& operator =(derived& tensor)
{
data_ = tensor.data();
offset_ = tensor.offset();
size_ = tensor.size();
shape_ = tensor.shape();
steps_ = tensor.steps();

return *this;
}
void reshape(const std::initializer_list<size_t>& sizes_list)
{
reshape(shape_type(sizes_list));
}
void reshape(const shape_type& shape)//if this failed please clone this tensor to itself
{
size_t size = calculate_size(shape);
assert(offset_ == 0 && size_ == (*data).size());
assert(size == size_);
shape_ = shape;
init_steps();
}
const data_type& data()
{
return data_;
}
bool continuous()
{
return continuous_;
}
derived clone()//iterate through view and copy data
{
derived ret(shape_);
index_type index(shape_.size(), 0);
bool finish = false;

while (true)
{
for (size_t i = 0; i < shape_.size(); ++i)
{
if (index[i] == shape_[i])
{
if (i == shape_.size() - 1)
{
finish = true;
break;
}
index[i] = 0;
index[i + 1]++;
}
else{
break;
}
}
if (finish) break;

ret(index) = (*this)(index);
index[0]++;
}
return ret;
}
derived operator()(const extents_type& extents)
{
derived ret(*static_cast<derived*>(this));
const auto& ranges = extents.ranges;
assert(dims() == ranges.size());

for (size_t i = 0; i < ranges.size(); ++i)
{
if (ranges[i].full_) continue;
ret.offset_ += ranges[i].begin_ * ret.steps_[i];
ret.shape_[i] = ranges[i].size();
ret.steps_[i] *= ranges[i].step_;
ret.update_size();
}

ret.squeeze();

return ret;
}

void squeeze()//去除大小为1的维度
{
for (size_t i = 0; i < shape_.size(); ++i)
{
if (shape_[i] == 1)
{
shape_.erase(shape_.begin() + i);
steps_.erase(steps_.begin() + i);
}
}
}
void transpose(const std::initializer_list<size_t>& permutation)
{
assert(permutation.size() == dims());
index_type index(dims());

for (size_t i = 0; i < dims(); ++i)
{
index[i] = i;
}
assert(std::is_permutation(permutation.begin(), permutation.end(), index.begin()));
index_type steps(dims());

for (size_t i = 0; i < dims(); ++i)
{
steps[permutation.begin()[i]] = steps_[i];
}
steps_ = steps;
}
void flip(const std::initializer_list<bool>& flip_flags)
{
assert(flip_flags.size() == dims());
for (size_t i = 0; i < shape_.size(); ++i)
{
if (flip_flags.begin()[i] == false) continue;
offset_ += (shape_[i] - 1) * steps_[i];
steps_[i] *= -1;
}
}

value_type& operator()(const std::initializer_list<index_value>& index)
{
return (*data_)[_index(index)];
}
const value_type& operator()(const std::initializer_list<index_value>& index) const
{
return (*data_)[_index(index)];
}
value_type& operator()(const index_type& index)
{
return (*data_)[_index(index)];
}
const value_type& operator()(const index_type&& index) const
{
return (*data_)[_index(index)];
}

value_type& operator[](size_t index)
{
#if _ITERATOR_DEBUG_LEVEL == 2
assert(offset_ == 0 && size_ == (*data_).size());
#endif
return (*data_)[index];
}
const value_type& operator[](size_t index) const
{
#if _ITERATOR_DEBUG_LEVEL == 2
assert(offset_ == 0 && size_ == (*data_).size());
#endif
return (*data_)[index];
}
private:
/*
template<int ndims>
size_t index(const std::vector<T>& coordinate)
{
return coordinate[ndims - 1] * steps_[ndims - 1] + index<ndims - 1>(coordinate);
}
template<>
size_t index<0>(const std::vector<T>& coordinate)
{
return 0;
}
*/
size_t _index(const std::initializer_list<index_value>& coordinate)
{
return _index(index_type(coordinate));
}
size_t _index(const index_type& coordinate)
{
int ret = offset_;
const size_t ndims = coordinate.size();
#if _ITERATOR_DEBUG_LEVEL == 2
assert(ndims == dims());
#endif
for (size_t i = 0; i < ndims; ++i)
{
#if _ITERATOR_DEBUG_LEVEL == 2
if (coordinate[i] >= shape_[i])
{ // report error
_DEBUG_ERROR("tensor subscript out of range");
_SCL_SECURE_OUT_OF_RANGE;
}
#endif
ret += coordinate[i] * steps_[i];
}
return ret;
}
public:
const index_type& shape()
{
return shape_;
}
const index_type& shape() const
{
return shape_;
}
index_value shape(size_t i)
{
return shape_[i];
}
index_value shape(size_t i) const
{
return shape_[i];
}
const index_type& steps()
{
return steps_;
}
const index_type& steps() const
{
return steps_;
}
size_t size()
{
return size_;
}
size_t size() const
{
return size_;
}
size_t dims()
{
return shape_.size();
}
size_t dims() const
{
return shape_.size();
}
int offset()
{
return offset_;
}
int offset() const
{
return offset_;
}
protected:
size_t calculate_size(const shape_type& shape)
{
double product = 1;
size_t size = 1;
for (auto& v : shape)
{
product *= v;
size *= v;
}
assert(product < std::numeric_limits<size_t>::max());//check whether size overflow

return size;
}
void update_size()
{
size_ = calculate_size(shape_);
}
void init_steps()
{
steps_.resize(shape_.size());
std::fill(steps_.begin(), steps_.end(), 1);
for (size_t i = 1; i < steps_.size(); ++i)
for (size_t k = 0; k < i; ++k)
steps_[i] *= shape_[k];
}
public:
friend class tensor_base_t<derived,value_type,data_type>;
};
...全文
174 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-02-17
  • 打赏
  • 举报
回复
引用 3 楼 dianyancao 的回复:
[quote=引用 2 楼 zhao4zhong1 的回复:] 仅供参考:
//在堆中开辟一个2×3×4×5的4维int数组
这个多维数组数据不是连续存放的,效率比较低 我用的是通过index_用一维方式访问数组[/quote] 无profiler不要谈效率!!尤其在这个云计算、虚拟机、模拟器、CUDA、多核 、多级cache、指令流水线、多种存储介质、……满天飞的时代!
赵4老师 2016-02-16
  • 打赏
  • 举报
回复
仅供参考:
//在堆中开辟一个2×3×4×5的4维int数组
#include <stdio.h>
#include <malloc.h>
int ****p;
int h,i,j,k;
void main() {
    p=(int ****)malloc(2*sizeof(int ***));
    if (NULL==p) return;
    for (h=0;h<2;h++) {
        p[h]=(int ***)malloc(3*sizeof(int **));
        if (NULL==p[h]) return;
        for (i=0;i<3;i++) {
            p[h][i]=(int **)malloc(4*sizeof(int *));
            if (NULL==p[h][i]) return;
            for (j=0;j<4;j++) {
                p[h][i][j]=(int *)malloc(5*sizeof(int));
                if (NULL==p[h][i][j]) return;
            }
        }
    }
    for (h=0;h<2;h++) {
        for (i=0;i<3;i++) {
            for (j=0;j<4;j++) {
                for (k=0;k<5;k++) {
                    p[h][i][j][k]=h*60+i*20+j*5+k;
                }
            }
        }
    }
    for (h=0;h<2;h++) {
        for (i=0;i<3;i++) {
            for (j=0;j<4;j++) {
                for (k=0;k<5;k++) {
                    printf(" %3d",p[h][i][j][k]);
                }
                printf("\n");
            }
            printf("--------------------\n");
        }
        printf("=======================\n");
    }
    for (h=0;h<2;h++) {
        for (i=0;i<3;i++) {
            for (j=0;j<4;j++) {
                free(p[h][i][j]);
            }
            free(p[h][i]);
        }
        free(p[h]);
    }
    free(p);
}
//   0   1   2   3   4
//   5   6   7   8   9
//  10  11  12  13  14
//  15  16  17  18  19
//--------------------
//  20  21  22  23  24
//  25  26  27  28  29
//  30  31  32  33  34
//  35  36  37  38  39
//--------------------
//  40  41  42  43  44
//  45  46  47  48  49
//  50  51  52  53  54
//  55  56  57  58  59
//--------------------
//=======================
//  60  61  62  63  64
//  65  66  67  68  69
//  70  71  72  73  74
//  75  76  77  78  79
//--------------------
//  80  81  82  83  84
//  85  86  87  88  89
//  90  91  92  93  94
//  95  96  97  98  99
//--------------------
// 100 101 102 103 104
// 105 106 107 108 109
// 110 111 112 113 114
// 115 116 117 118 119
//--------------------
//=======================
//
dianyancao 2016-02-16
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
仅供参考:
//在堆中开辟一个2×3×4×5的4维int数组
这个多维数组数据不是连续存放的,效率比较低 我用的是通过index_用一维方式访问数组
dianyancao 2016-02-16
  • 打赏
  • 举报
回复
tensor_t.h
#pragma once

#include "tensor_base_t.h"

//当数组维度大于等于2时才使用tensor_base_t,否则使用std::vector
template<typename value_type>
class tensor_t : public tensor_base_t < tensor_t<value_type>, value_type, std::shared_ptr<std::vector<value_type>> >
{
public:
	typedef typename int index_value;//signed
	typedef typename std::vector<index_value> shape_type;
	typedef typename shape_type index_type;
	typedef typename tensor_base_t < tensor_t<value_type>, value_type, std::shared_ptr<std::vector<value_type>>> base;

	typedef typename base::value_type value_type;
	typedef typename base::tensor_type tensor_type;
	typedef typename base::vector_type vector_type;
	typedef typename base::data_type data_type;
public:
	tensor_t()
	{
		reset(1, 0);
	}
	tensor_t(const std::initializer_list<index_value>& sizes_list, const value_type& value = value_type())
	{
		reset(sizes_list, value);
	}
	tensor_t(const shape_type& shape, const value_type& value = value_type())
	{
		reset(shape, value);
	}
	tensor_t(tensor_type& tensor)
	{
		*this = tensor;
	}
	void reset(const shape_type& shape, const value_type& value = value_type())
	{
		data_.reset(new vector_type());
		resize(shape, value);
	}
	void reset(size_t size, const value_type& value = value_type())
	{
		data_.reset(new vector_type());
		resize(size, value);
	}
	void resize(const size_t& size1, const value_type& value = value_type())
	{
		resize(shape_type(1, size1), value);
	}
	void resize(const std::initializer_list<index_value>& list, const value_type& value = value_type())
	{
		resize(shape_type(list), value);
	}
	void resize(const shape_type& shape, const value_type& value = value_type()) //after resize the data is dirty,must manual initialize data
	{
		size_t size = calculate_size(shape);
		assert(size > 0);
		(*data_).resize(size, value);
		offset_ = 0;
		size_ = size;
		shape_ = shape;
		init_steps();
		squeeze();
	}
};
main.cpp
#include <iostream>
#include <time.h>
#include "tensor_t.h"

using namespace std;

int main()
{
	tensor_t<double> tensor;
	tensor.reset({ 2, 3, 5 });//重新分配内存,并设置形状

	tensor({ 1, 0, 2 }) = 123456;//对给定位置的元素赋值
	cout << tensor({ 1, 0, 2 }) << std::endl; //123456
	cout << tensor[1 + 0 * 2 + 2 * 2 * 3] << std::endl; //123456
	
	tensor.assign({ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
		2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
		2, 2, 2, 2, 2, 2, 2, 2, 2, 2 });//复制给定数据
	tensor = tensor.clone();//深度拷贝
	cout << tensor({ 1, 0, 2 }) << std::endl; //2

	tensor[1 + 2 * 2 + 2 * 2 * 3] = 2345;//按一维下标访问多维数组
	tensor_t<double> tensor2 = tensor(extents_type()[1][range_type()][range_type(0, 5, 2)]);//切割数组
	cout << tensor2({ 2,1 }) << std::endl;//2345
	tensor2.transpose({ 1, 0 });//按排列转置数组
	cout << tensor2({ 1, 2 }) << std::endl;//2345

	tensor2.flip({ false, true });//翻转数组的指定维度
	cout << tensor2({  1, 3 - 1 - 2 }) << std::endl;//2345

	system("PAUSE");
	return 0;
}
                《人工智能:深度学习入门到精通实战》课程主要就人工智能领域相关的深度学习基础、深度学习计算、卷积神经网络+经典网络、循环神经网络+RNN进阶、优化算法、计算机视觉和自然语言处理等,配套实战案例与项目全部基于真实数据集与实际任务展开,结合深度学习框架进行建模实战。                由浅入深,每一个理论搭配一个实验,引领学员浸泡式逐步掌握各项技能和实战项目,且侧重技能不同,学员的知识体系会更加全面课程大纲:第一章:深度学习基础-深度学习简介01.1-前置知识01.2-传统编程与数据编程01.3-深度学习起源01.4-深度学习崛起与发展01.5-深度学习成功案例01.6-深度学习特点 第二章:深度学习基础-Python基础02.1-PyTorch介绍与环境配置02.2-数据操作与创建Tensor02.3-算术操作、索引与改变形状02.4-线性代数、广播机制与内存开销02.5-Tensor和NumPy相互转换与Tensor on GPU02.6-实验01-创建和使用Tensor-102.7-实验01-创建和使用Tensor-202.8-梯度下降02.9-实验02-梯度下降-102.10-实验02-梯度下降-202.11-自动求梯度概念02.12-自动求梯度实例02.13-实验03-自动求梯度-102.14-实验03-自动求梯度-2 第三章:深度学习基础-线性回归03.1-线性回归讲解03.2-线性回归实例03.3-实验04-从零实现线性回归-103.4-实验04-从零实现线性回归-203.5-实验05-线性回归的简洁实现-103.6-实验05-线性回归的简洁实现-2 第四章:深度学习基础-softmax回归04.1-softmax回归04.2-实验06-FashionMNIST04.3-实验07-从零实现Softmax回归-104.4-实验07-从零实现Softmax回归-204.5-实验08-softmax回归的简洁实现 第五章:深度学习基础-多层感知机05.1-感知机05.2-多层感知机05.3-多层感知机与神经网络05.4-激活函数05.5-正向传播05.6-反向传播05.7-正向传播和反向传播05.8-批大小05.9-实验09-从零实现MLP05.10-实验10-MLP的简洁实现 第六章:深度学习基础-模型选择、欠拟合和过拟合06.1-训练误差和泛化误差06.2-模型选择06.3-欠拟合和过拟合06.4-权重衰减06.5-丢弃法06.6-实验11-多项式函数拟合实验06.7-实验12-高维线性回归实验-106.8-实验12-高维线性回归实验-206.9-实验13-Dropout 第七章:深度学习基础-数值稳定性和模型初始化07.1-数值稳定性和模型初始化-107.2-数值稳定性和模型初始化-207.3-实验14-房价预测案例-107.4-实验14-房价预测案例-207.5-实验14-房价预测案例-3 第八章:深度学习计算-模型构造08.1-模型构造-108.2-模型构造-208.3-模型构造-308.4-实验15-模型构造-108.5-实验15-模型构造-2 第九章:深度学习计算-模型参数的访问、初始化和共享09.1-模型参数的访问09.2-模型参数初始化和共享09.3-实验16-模型参数-109.4-实验16-模型参数-2 第十章:深度学习计算-自定义层与读取和储存10.1-不含模型参数的自定义层10.2-含模型参数的自定义层10.3-实验17-自定义层10.4-读取和储存10.5-GPU计算10.6-实验18-读取和储存  第十一章:卷积神经网络11.01-卷积神经网络11.02-卷积神经网络的组成层11.03-图像分类的局限性11.04-二维卷积层与卷积层11.05-卷积在图像中的直观作用11.06-实验19-二维卷积层11.07-填充与步幅11.08-卷积过程11.09-卷积层参数-111.10-卷积层参数-211.11-实验20-Pad和Stride11.12-多输入和输出通道11.13-实验21-多通道11.14-池化层11.15-实验22-池化层 第十二章:经典网络12.01-卷积神经网络12.02-实验23-LeNet模型12.03-深度卷积神经网络12.04-实验24-AlexNet模型12.05-使用重复元素的网络12.06-实验25-VGG模型12.07-网络中的网络12.08-实验26-NiN模型12.09-含并行连接的网络12.10-实验27-GoogLeNet模型12.11-批量归一化-112.12-批量归一化-212.13-实验28-批量归一化12.14-残差网络12.15-实验29-残差网络12.16-稠密连接网络12.17-实验30-稠密连接网络 第十三章:循环神经网络13.01-语言模型和计算13.02-n元语法13.03-RNN和RNNs13.04-标准RNN向前输出流程和语言模型应用13.05-vector-to-sequence结构13.06-实验31-语言模型数据集-113.07-实验31-语言模型数据集-213.08-实验32-从零实现循环神经网络-113.09-实验32-从零实现循环神经网络-213.10-实验32-从零实现循环神经网络-313.11-实验32-从零实现循环神经网络-413.12-实验33-简洁实现循环神经网络-113.13-实验33-简洁实现循环神经网络-2 第十四章:RNN进阶14.01-通过时间反向传播-114.02-通过时间反向传播-214.03-长短期记忆-114.04-长短期记忆-214.05-实验34-长短期记忆网络-114.06-实验34-长短期记忆网络-214.07-门控循环单元14.08-RNN扩展模型14.09-实验35-门控循环单元 第十五章:优化算法15.01-优化与深度学习15.02-局部最小值和鞍点15.03-提高深度学习的泛化能力15.04-实验36-小批量梯度下降-115.05-实验36-小批量梯度下降-215.06-动量法-115.07-动量法-215.08-实验37-动量法15.09-AdaGrad算法与特点15.10-实验38-AdaGrad算法15.11-RMSrop算法15.12-实验39-RMSProp算法15.13-AdaDelta算法15.14-实验40-AdaDelta算法15.15-Adam算法15.16-实验41-Adam算法15.17-不用二阶优化讲解与超参数 第十六章:计算机视觉16.01-图像增广与挑战16.02-翻转、裁剪、变化颜色与叠加16.03-实验42-图像增广-116.04-实验42-图像增广-216.05-微调16.06-迁移学习16.07-实验43-微调-116.08-实验43-微调-216.09-目标检测16.10-边界框16.11-实验44-边界框16.12-锚框与生成多个锚框16.13-交并比16.14-实验45-生成锚框-116.15-实验45-生成锚框-216.17-标注训练集的锚框-116.18-标注训练集的锚框-216.19-实验46-标注训练集的锚框-116.20-实验46-标注训练集的锚框-216.21-实验46-标注训练集的锚框-316.22-输出预测边界框16.23-实验47-输出预测边界框-116.24-实验47-输出预测边界框-216.25-多尺度目标检测16.26-实验48-多尺度目标检测16.27-目标检测算法分类16.28-SSD与模型设计16.29-预测层16.30-损失函数16.31-SSD预测16.32-实验49-目标检测数据集16.33-实验50-SSD目标检测-116.34-实验50-SSD目标检测-216.35-实验50-SSD目标检测-316.36-实验50-SSD目标检测-416.37-实验50-SSD目标检测-516.38-实验50-SSD目标检测-6 第十七章:自然语言处理17.01-词嵌入和词向量17.02-神经网络模型17.03-跳字模型17.04-训练跳字模型17.05-连续词袋模型17.06-负采样17.07-层序softmax17.08-子词嵌入17.09-Fasttext模型17.10-全局向量的词嵌入17.11-实验51-word2vec之数据预处理-117.12-实验51-word2vec之数据预处理-217.13-实验52-word2vec之负采样-117.14-实验52-word2vec之负采样-217.15-实验53-word2vec之模型构建-117.16-实验53-word2vec之模型构建-217.17-实验54-求近义词和类比词-117.18-实验54-求近义词和类比词-217.19-实验55-文本情感分类RNN-117.20-实验55-文本情感分类RNN-217.21-实验55-文本情感分类RNN-317.22-实验55-文本情感分类RNN-417.23-TextCNN17.24-TextCNN流程17.25-实验56-文本情感分类textCNN-117.26-实验56-文本情感分类textCNN-217.27-Seq2Seq的历史与网络架构17.28-Seq2Seq的应用与存在的问题17.29-Attention机制与Bucket机制17.30-实验57-机器翻译之模型构建-117.31-实验57-机器翻译之模型构建-217.32-实验57-机器翻译之模型构建-317.33-实验58-机器翻译之训练评估-117.34-实验58-机器翻译之训练评估-217.35-实验58-机器翻译之训练评估-3

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧