一个简单的C++多维数组类

医学图像处理入门知识 | 格式DICOM,MHD+RAW | 坐标系 | ITK-SNAP | 重采样 医学图像处理入门知识引言常见的医学图像格式DICOMmhd+raw坐标系世界坐标体系解剖学坐标体系图像坐标体系这三个坐标体系有什么关系呢?怎样转换呢?在这里插入图片描述ITK-SNAP工具使用基本功能调整对比度重采样项目思路总结 引言 最近做了一个医学图像的项目,将这块好好的整理一下,给入门的同学提供点帮助,让大家能够将时间花在核心算法上,缩短准备工作的时间。 文章内容较多,本人力求将每个地方讲明白并没有错误,不过文笔有限,可能还是有错误或者表述上不够严谨。如果有错误或者不妥的地方,还请指正,衷心感谢。 本 阅读详情

 

该数组类,是为了满足和JAVA类似的语法而写的,不过初始化方面,还是不够灵活

 

#ifndef _MULTIARRAY_

#define _MULTIARRAY_

 

template <typename IT, typename IT2>

void elemcopy(IT srcbegin, IT srcend, IT2 destbegin)

{

while (srcbegin != srcend)

{

*destbegin = *srcbegin;

++destbegin;

++srcbegin;

}

}

 

template <int DIMEN>

struct DIM_INFO

{

int dimsize[DIMEN];

DIM_INFO(const DIM_INFO<DIMEN - 1>& e, int size)

{

dimsize[0] = size;

elemcopy(&e.dimsize[0], &e.dimsize[DIMEN - 1], &dimsize[1]);

}

DIM_INFO<DIMEN + 1> operator[] (int size) const

{

return DIM_INFO<DIMEN + 1>(*this, int(size));

}

};

 

template <>

struct DIM_INFO<0>

{

int dimsize[1];

DIM_INFO<1> operator[] (int size) const

{

return DIM_INFO<1>(*this, int(size));

}

};

typedef DIM_INFO<0> 0DIM;

 

#ifdef DIM

#undef DIM

#endif

#define DIM (0DIM())

 

#ifndef NULL

#define NULL (0)

#endif

 

//////////////////////////////////////////////////////////////////////////

 

#ifdef INITCOUNTER

#undef INITCOUNTER

#endif

#define INITCOUNTER (1)

 

//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////

 

template <typename T>

class _share_array_data_

{

public:

_share_array_data_<T> * Duplicate()

{

if (this != NULL)

{

++_nRef;

}

return this;

}

 

void Release()

{

if (this != NULL)

{

// assert(_nRef  >= INITCOUNTER);

--_nRef;

if (_nRef < INITCOUNTER)

{

delete this;

}

}

}

 

static _share_array_data_<T>* NewData(int size)

{

return ((0 == size)?NULL:(new _share_array_data_<T>(size)));

}

 

 

inline int Size() const {return (NULL == this)?0:_nSize;}

 

inline const T* begin() const {return (NULL == this)?NULL:_pValue;}

inline const T* end() const {return (NULL == this)?NULL:(_pValue + _nSize);}

 

inline T* begin() {return (NULL == this)?NULL:_pValue;}

inline T* end() {return (NULL == this)?NULL:(_pValue + _nSize);}

 

private:

_share_array_data_<T>& operator=(const _share_array_data_<T>&);

_share_array_data_(const _share_array_data_<T>&);

 

_share_array_data_(int size)

{

_nSize = size; // assert(_nSize != 0);

_pValue = new T[size]; // assert(_pValue != NULL);

_nRef = INITCOUNTER;

}

~_share_array_data_()

{

// assert(_nRef  < INITCOUNTER);

// assert(_pValue != NULL);

delete[] _pValue;

}

 

int _nRef;

int _nSize;

T *_pValue;

};

 

//////////////////////////////////////////////////////////////////////////

 

template <typename T>

inline void InitFunc(T*, T*)

{}

 

template <typename T>

inline void InitFunc(T **pbegin, T **pend)

{

MEMSET(pbegin, 0, (pend - pbegin)*sizeof(T*));

}

 

inline void InitFunc(int *pbegin, int *pend)

{

MEMSET(pbegin, 0, (pend - pbegin)*sizeof(int));

}

 

inline void InitFunc(short *pbegin, short *pend)

{

MEMSET(pbegin, 0, (pend - pbegin)*sizeof(short));

}

 

inline void InitFunc(char *pbegin, char *pend)

{

MEMSET(pbegin, 0, (pend - pbegin)*sizeof(char));

}

 

inline void InitFunc(signed char *pbegin, signed char *pend)

{

MEMSET(pbegin, 0, (pend - pbegin)*sizeof(signed char));

}

 

//////////////////////////////////////////////////////////////////////////

// Interface: multi_array

 

template <typename T, int DIMEN>

class multi_array;

 

//////////////////////////////////////////////////////////////////////////

 

template<typename T>

class multi_array<T, 1>

{

public:

typedef DIM_INFO<1> THISDIM;

typedef multi_array<T, 1> THISTYPE;

 

typedef T ELETYPE;

enum {DIMENTION = 1};

typedef T VALTYPE;

typedef _share_array_data_<VALTYPE> DATATYPE;

 

~multi_array()

{

_pRef->Release();

_pRef = NULL;

}

 

multi_array() : _pRef(NULL)

{

}

 

multi_array(const THISDIM& diminfo) : _pRef(DATATYPE::NewData(diminfo.dimsize[0]))

{

InitFunc((_pRef->begin()), (_pRef->end()));

}

 

multi_array(const THISTYPE& src) : _pRef(src._pRef->Duplicate())

{

}

 

multi_array(THISTYPE *psrc) : _pRef(NULL)

{

if (psrc != NULL)

{

_pRef = psrc->_pRef->Duplicate();

delete psrc;

}

}

 

//////////////////////////////////////////////////////////////////////////

 

THISTYPE& operator = (const THISDIM& diminfo)

{

_pRef->Release();

_pRef = DATATYPE::NewData(diminfo.dimsize[0]);

InitFunc((_pRef->begin()), (_pRef->end()));

return *this;

}

 

THISTYPE& operator = (const THISTYPE& src)

{

if (_pRef != src._pRef)

{

_pRef->Release();

_pRef = src._pRef->Duplicate();

}

return *this;

}

 

THISTYPE& operator = (THISTYPE *psrc) // 只是为了适应JAVA语法[Object a; a = new Object;]

{

if (psrc != NULL)

{

if (_pRef != psrc->_pRef)

{

_pRef->Release();

_pRef = psrc->_pRef->Duplicate();

}

else if (this != psrc)

{

delete psrc;

}

}

else

{

_pRef->Release();

_pRef = NULL;

}

return *this;

}

 

inline VALTYPE& operator [] (int index)

{

return (_pRef->begin())[index];

}

 

inline const VALTYPE& operator [] (int index) const

{

return (_pRef->begin())[index];

}

 

inline int length() const {return _pRef->Size();}

 

inline VALTYPE *begin() {return (_pRef->begin());}

 

inline const VALTYPE *begin() const {return (_pRef->begin());}

 

inline VALTYPE *end() {return (_pRef->end());}

 

inline const VALTYPE *end() const {return (_pRef->end());}

 

inline operator boolean() const {return (_pRef != NULL);}

 

friend inline boolean operator == (const THISTYPE& _l, const THISTYPE& _r)

{

return (_l._pRef == _r._pRef);

}

friend inline boolean operator != (const THISTYPE& _l, const THISTYPE& _r)

{

return (_l._pRef != _r._pRef);

}

 

 

private:

DATATYPE *_pRef;

};

 

//////////////////////////////////////////////////////////////////////////

 

template <typename T, int DIMEN>

class multi_array

{

public:

typedef DIM_INFO<DIMEN> THISDIM;

typedef DIM_INFO<DIMEN - 1> NEXTDIM;

typedef multi_array<T, DIMEN> THISTYPE;

 

typedef T ELETYPE;

enum {DIMENTION = DIMEN};

typedef multi_array<T, DIMEN - 1> VALTYPE;

typedef _share_array_data_<VALTYPE> DATATYPE;

 

~multi_array()

{

_pRef->Release();

_pRef = NULL;

}

 

multi_array() : _pRef(NULL)

{

}

 

multi_array(const THISDIM& diminfo) : _pRef(DATATYPE::NewData(diminfo.dimsize[DIMEN - 1]))

{

if (_pRef)

{

VALTYPE *pdata = _pRef->begin();

int size = _pRef->Size();

const NEXTDIM& d_1 = (const NEXTDIM&)diminfo;

for (int i = 0; i < size; ++i) pdata[i] = d_1;

}

}

 

multi_array(const THISTYPE& src) : _pRef(src._pRef->Duplicate())

{

}

 

multi_array(THISTYPE *psrc) : _pRef(NULL)

{

if (psrc != NULL)

{

_pRef = psrc->_pRef->Duplicate();

delete psrc;

}

}

 

//////////////////////////////////////////////////////////////////////////

 

 

THISTYPE& operator = (const THISDIM& diminfo)

{

_pRef->Release();

_pRef = DATATYPE::NewData(diminfo.dimsize[DIMEN - 1]);

if (_pRef)

{

VALTYPE *pdata = _pRef->begin();

int size = _pRef->Size();

const NEXTDIM& d_1 = (const NEXTDIM&)diminfo;

for (int i = 0; i < size; ++i) pdata[i] = d_1;

}

return *this;

}

 

THISTYPE& operator = (const THISTYPE& src)

{

if (_pRef != src._pRef)

{

_pRef->Release();

_pRef = src._pRef->Duplicate();

}

return *this;

}

 

THISTYPE& operator = (THISTYPE *psrc) // 只是为了适应JAVA语法[Object a; a = new Object;]

{

if (psrc != NULL)

{

if (_pRef != psrc->_pRef)

{

_pRef->Release();

_pRef = psrc->_pRef->Duplicate();

}

else if (this != psrc)

{

delete psrc;

}

}

else

{

_pRef->Release();

_pRef = NULL;

}

return *this;

}

 

inline VALTYPE& operator [] (int index)

{

return (_pRef->begin())[index];

}

 

inline const VALTYPE& operator [] (int index) const

{

return (_pRef->begin())[index];

}

 

inline int length() const {return _pRef->Size();}

 

inline VALTYPE *begin() {return (_pRef->begin());}

 

inline const VALTYPE *begin() const {return (_pRef->begin());}

 

inline VALTYPE *end() {return (_pRef->end());}

 

inline const VALTYPE *end() const {return (_pRef->end());}

 

inline operator boolean() const {return (_pRef != NULL);}

 

friend boolean operator == (const THISTYPE& _l, const THISTYPE& _r)

{

return (_l._pRef == _r._pRef);

}

friend boolean operator != (const THISTYPE& _l, const THISTYPE& _r)

{

return (_l._pRef != _r._pRef);

}

 

private:

DATATYPE *_pRef;

};

 

 

//////////////////////////////////////////////////////////////////////////

 

#endif

 

 

C++:动态数组类 动态创建多维数组 vector深复制 动态数组类 如果我们的类中需要使用动态申请的内存空间,并且该空间是依附于对象的,我们一般在该类的构造函数中去申请空间(new),在该类的析构函数中释放空间(delete). #include&amp;lt;iostream&amp;gt; #include&amp;lt;cassert&amp;gt; using namespace std; class Point{ public: Point():x(0),y(0){ ... 阅读详情

相关推荐

boost源码剖析之:泛型多维数组类multi_array

boost源码剖析之:boost::multi_array 谢轩 刘未鹏C++的罗浮宫(http://blog.csdn.net/pongba) Note: 并非新作,是以前和老朋友谢轩写的,也可以在谢轩的blog上找到。  动机      C++是一门自由的语言,允许你自由的表达自己的意图,对不对? 所以我们既然可以new一个一维数组,也应该可以new出多维数组,

刘未鹏|C++的罗浮宫 1万+

python中size的用法.dim_【Numpy库学习笔记】Numpy中dim、shape和size的理解

一、dim理解:dim就是维数,比如数组会有一维数组,二维数组……。numpy的dim维数同理。那如何判断numpy的维数,最简单的方法就是数一数"中括号"的数量。没有中括号,dim=0; 1个中括号,dim=1; 2个中括号,dim=2 ……如:①a = np.array([[1,2,3],[2,3,4]]),在array和第一行元素1,2,3中间有2个[,说明是2维。②a = np.array...

weixin_39911056的博客 834

【BOOST C++】高级5:多维数组Array

只学习一个C语言可能“没用”,但是如果你是搞AI的,或是深度学习、或是自动驾驶、或是机器人,达到一定程度,都能发现C++的不足。因此,凡是搞人工智能的,业余时间磨一磨C++是有益的。本文是BOOST库的C或C++的系列文章,本文针对其中的多维数组元素Array对象进行讨论。

gongdiwudu的专栏 5031

C++ 多维数组

c/c++多维数组 ; 多维数组的创建与初始化; 多维数组的遍历 ; 多维数组与数组; 二维数组与一维数组的关系; 多维数组的理解;

m0_62870588的博客 2472

大疆OcuSync vs 传统2.4GHz:深度拆解无人机图传射频模块的硬件差异

本文深度对比了大疆OcuSync与传统2.4GHz无人机图传射频模块的硬件差异,涵盖芯片组、天线系统、PCB设计、散热方案及生产测试等核心环节。重点解析OcuSync的全定制SoC芯片、智能MIMO天线阵列及精密PCB布局如何实现革命性性能提升,为无人机图传技术发展提供专业参考。

u5v6w7的博客 811

CubeMX实战:ETH+LWIP+FreeRTOS网络工程从零搭建与AC6编译优化

本文手把手教你使用STM32CubeMX从零搭建集成以太网(ETH)、LWIP协议栈和FreeRTOS的嵌入式网络工程。文章详细解析了外设配置、协议栈调优、任务规划等关键步骤,并重点分享了如何解决ARM Compiler 6(AC6)的兼容性问题,实现编译速度与稳定性的双重提升,助你快速构建稳定的网络应用原型。

n2o3p4的博客 413

C++多维数组库:高效编程的艺术

C++多维数组库:高效编程的艺术 1. 项目基础介绍与编程语言 本项目是一个开源的C++多维数组库,旨在提供一种在C++中处理多维数组的高效方法。该库名为 array,可在 GitHub 上获取。该项目主要使用C++编程语言实现,遵循C++标准模板库(STL)的惯例,同时支持CUDA,为GPU计算提供了便利。 2. 项目核心功能 该库的核心功能是提供一个多维数组类,具有以下设计目标: 允许将数组...

gitblog_01028的博客 507

机器学习(三) ----------线性回归算法(梯度下降+正则化)

线性回归算法在机器学习中具有核心地位,它以其简单性和直观性为基础,为理解数据间的关系提供了基本框架。作为许多复杂算法的基础,线性回归不仅用于预测目标变量,还用于解释自变量对目标的影响。同时,它也是机器学习入门的经典算法,对于推广机器学习知识和技术具有重要意义。

2301_77539454的博客 1874

多维数组类

C++ code #include "stdafx.h" #include <fstream> #include <assert.h> #include <string> #include "procs.h" #include "str.h" #include "LinkList.h" #pragma once using namesp...

weixin_30746117的博客 164

RK3568平台开发系列讲解:Linux wifi的调试

查询结果中state为ENABLE,表示hostapd程序启用成功。首先查看设备是否存在。

内核笔记 169

华为OD机考双机位C卷 - 构成正方形的数量 (Java & Python& JS & C/C++ & GO )

华为OD机考双机位C卷 - 成正方形的数量 (OD上机考试双机位C卷)。提供在线OJ刷题。Java、C++、Python、GO语言、C语言、JavaScript题解,并提供完整思路讲解。

算法大师! 5945

more effective c++——Item M30 代理类(一)多维数组的实现

代理类:用一个类来实现我们所需要的而 C++语言中并没有提供的东西,用户并不需要该类的具体实现总结: 1.模板类中,输入输出操作符重载不能在模板类外部实现 2.数组类需要提供基本的操作:单参构造,拷贝构造,赋值操作,输出操作符 3.n维数组类持有n-1维数组的二级指针 4.多维数组可以看作n-1维数组的叠加,构造时需要先构造row个n-1个维数组的指针,然后再

卢然小子的博客 497

BME688传感器原理图设计,已量产(环境传感器)

在便携式空气质量监测、可穿戴健康设备这类低功耗场景中,BME688 这类集成了 VOC、温湿度、气压的四合一 MEMS 传感器越来越受欢迎 —— 它体积小、功耗低,刚好匹配便携设备的需求。但这类小信号传感器对电源噪声、电平匹配的要求很细,今天结合实际项目里的一款 BME688 接口电路,聊聊硬件设计的关键细节。

不脱发的程序猿 526

C++实现多维数组与栈式虚拟机:从内存布局到指令执行

在计算机科学中,数据结构和虚拟机是构建软件系统的两大基石。数据结构负责高效组织与存储数据,其内存布局直接影响程序的缓存利用率和执行性能。虚拟机则提供了一层抽象,通过定义中间指令集和解释执行机制,实现了跨平台兼容与代码安全。理解多维数组在内存中的连续存储原理(如行优先布局)以及栈式虚拟机的取指-解码-执行循环,对于深入系统编程、编译器设计和高性能计算至关重要。本文以C++实践为例,探讨如何实现一个通用的多维数组类,并构建一个精简的栈式虚拟机来操作该数组,通过自定义字节码完成矩阵加法等计算任务,从而揭示高级语言

weixin_33178839的博客 254

别再只当调包侠了!YOLO检测头到损失函数全链路拆解,看完才算真懂

YOLO的精妙不在于某个单一创新,而在于各模块围绕“实时精准检测”这一目标的协同设计。Backbone追求高效特征压缩,Neck保障多尺度信息流通,Head解耦任务冲突,Loss动态引导优化方向。只有理解这套内在逻辑,才能跳出“调包-试错”的低效循环,在面对新场景、新问题时做出有理有据的改进决策。技术深度决定职业天花板。下次再调YOLO时,不妨先问自己:我改的这个参数,到底影响了系统中的哪一环?参考资料。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 188

【MATLAB-app】csdn上第一套关于matlab appdesigner界面设计 系列“视频课”来啦~~

写作时间: 2020-12-10 标题: 01_为什么推荐你学习appdesigner? 目录: 1.什么是appdesigner? 2.appdesigner能干什么,有什么好处? 3.怎么学习?保证你一周内能够学会! 4.我的课程总目录 5.重磅来袭(如何获取学习资料代码+文档+视频) 正文: 1.什么是appdesigner? 2.appdesigner能干什么,有什么好处? 3.怎么学习?保证你一周内能够学会! 4.我的课程总目录 5.重磅来袭(如何获取学习资料代码+文档+视频) THE END

wind 2万+

探索多维度数组的未来 —— 强力C++库深度解读

探索多维度数组的未来 —— 强力C++库深度解读 在现代软件开发中,尤其是数据处理和高性能计算领域,高效的多维数组管理是不可或缺的一环。今天,我们要向您介绍一个专为C++设计的强大库,它以提高性能、灵活性与编译时优化为目标,旨在成为处理多维度数据的新宠儿。 项目简介 这个开源图书馆通过提供一个灵活且高效的多维度数组类,革新了C++编程的格局。它的核心设计理念包括利用编译时常量来参数化数组特征,从而...

gitblog_00054的博客 491

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C++中的动态多维数组

转:http://www.yesky.com/25/165025.shtml 前言]C++的new操作符是该语言一个非常好的语法特性,然而实际使用中却发现new操作符有不少限制,为突出的一点便是用new操作符分配多维数组空间时,不能让数组的每一维都动态可变。本文将对此提出一个简单直观的解决方案,在一个实际问题的简化模型中加以说明,并以此释清许多初学者对C++中new操作符与多维数组的误区。

zhangmiaoping23的专栏 1047

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

C++23中的多维下标操作符超越传统数组索引的现代设计

C++23的多维下标操作符是对语言表达能力的一次重要增强。它通过提供一个简洁、安全且高效的新语法,解决了传统多维索引方式的诸多痛点。这一特性不仅符合现代C++的发展方向,强调类型安全和零开销抽象,也为库作者提供了构建更富表现力和鲁棒性的多维数据结构的有力工具,进一步巩固了C++在高性能计算领域的地位。

tqt_xy的博客 444

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

C++数组类模板

* 作为数组类模板,肯定没有vector做得好,可是普通的数组有1个优点就是能直接操作内存。vector在这方面就不是非常方便了。网上尽管也有数组类模板。多维的设计基本上都不是非常好。我这个类模板多维的设计借鉴了vector,如2维数组vector<vector<int>> vvArr;下附源代码*/ #pragma once enum E...

weixin_30912051的博客 132
上一篇: Brew中使用C++需要注意的事项
mars_man
博客等级 码龄20年 6粉丝 2原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值