C++元编程——DBN实现

1、多层RBM堆叠,最后采用罗杰斯特回归进行分类选择;2、采用RBM堆叠,然后采用BP神经网络进行梯度下降训练,得到最终的权重;3、多层堆叠RBM,在最后一层加上标志位输入;4、多层堆叠RBM,采用睡醒方式训练;5、多层堆叠RBM,每层RBM上行和下行的权重不同,非首尾层RBM的上行权重为下行权重2倍。由于说法太多,又没有时间一一证明,就用1实现了一个。但是第5种实现方法好像理论依据比较充足,后期有时间再实现吧。下面是多层RBM堆叠,最后采用softmax激活函数的BP神经网络进行模式判别。
摘要由CSDN通过智能技术生成

深度置信网络DBN的实现方式在网上有很多说法。总结下来有几种。1、多层RBM堆叠,最后采用罗杰斯特回归进行分类选择;2、采用RBM堆叠,然后采用BP神经网络进行梯度下降训练,得到最终的权重;3、多层堆叠RBM,在最后一层加上标志位输入;4、多层堆叠RBM,采用睡醒方式训练;5、多层堆叠RBM,每层RBM上行和下行的权重不同,非首尾层RBM的上行权重为下行权重2倍。由于说法太多,又没有时间一一证明,就用1实现了一个。但是第5种实现方法好像理论依据比较充足,后期有时间再实现吧。老规矩,先上试验代码:

#include <vector>
#include <iostream>
#include <string>
#include <iomanip>
#include <conio.h>
#include <random>
#include "dbn.hpp"

struct train_data
{
	mat<28, 28, double> mt_image;
	mat<10, 1, double> mt_label;
	int					i_num;
};

void assign_mat(mat<28, 28, double>& mt, const unsigned char* sz)
{
	int i_sz_cnt = 0;
	for (int r = 0; r < 28; ++r)
	{
		for (int c = 0; c < 28; ++c)
		{
			mt.get(r, c) = sz[i_sz_cnt++];
		}
	}
}

int main(int argc, char** argv) 
{
	unsigned char sz_image_buf[28 * 28];
	std::vector<train_data> vec_train_data;
	ht_memory mry_train_images(ht_memory::big_endian);
	mry_train_images.read_file("./data/train-images.idx3-ubyte");
	int32_t i_image_magic_num = 0, i_image_num = 0, i_image_col_num = 0, i_image_row_num = 0;
	mry_train_images >> i_image_magic_num >> i_image_num >> i_image_row_num >> i_image_col_num;
	printf("magic num:%d | image num:%d | image_row:%d | image_col:%d\r\n"
		, i_image_magic_num, i_image_num, i_image_row_num, i_image_col_num);
	ht_memory mry_train_labels(ht_memory::big_endian);
	mry_train_labels.read_file("./data/train-labels.idx1-ubyte");
	int32_t i_label_magic_num = 0, i_label_num = 0;
	mry_train_labels >> i_label_magic_num >> i_label_num;
	for (int i = 0; i < i_image_num; ++i)
	{
		memset(sz_image_buf, 0, sizeof(sz_image_buf));
		train_data td;
		unsigned char uc_label = 0;
		mry_train_images.read((char*)sz_image_buf, sizeof(sz_image_buf));
		assign_mat(td.mt_image, sz_image_buf);
		td.mt_image = td.mt_image / 256.;
		mry_train_labels >> uc_label;
		td.i_num = uc_label;
		td.mt_label.get((int)uc_label, 0) = 1;
		vec_train_data.push_back(td);
	}
	std::random_device rd;
	std::mt19937 rng(rd());
	std::shuffle(vec_train_data.begin(), vec_train_data.end(), rng);

	using dbn_type = dbn<double, 28 * 28, 10, 10>;
	using mat_type = mat<28 * 28, 1, double>;
	using ret_type = dbn_type::ret_type;
	dbn_type dbn_net;
	std::vector<mat_type> vec_input;
	std::vector<ret_type> vec_expect;
	for (int i = 0; i < 10; ++i) 
	{
		vec_input.push_back(vec_train_data[i].mt_image.one_col());
		vec_expect.push_back(vec_train_data[i].mt_label.one_col()); 
	}
	dbn_net.pretrain(vec_input, 10000);
	dbn_net.finetune(vec_expect, 10000);
	while (1)
	{
		std::string str_test_num;
		std::cout << "#";
		std::getline(std::cin, str_test_num);
		int i_test_num = std::stoi(str_test_num);
		auto pred = dbn_net.forward(vec_train_data[i_test_num].mt_image.one_col());
		pred.print();
		vec_train_data[i_test_num].mt_label.one_col().print();
		if ('q' == _getch())break;
	}

	return 0;
}

这个试验程序直接用了minist数据集进行测试,DBN主要作用是记忆联想。程序运行结果如下,可以看出来可以正确联想出结果(当然也不能每次都得到这么好的结果):

下面是多层RBM堆叠,最后采用softmax激活函数的BP神经网络进行模式判别。

#ifndef _DBN_HPP_
#define _DBN_HPP_

#include "bp.hpp"
#include "restricked_boltzman_machine.hpp"

/*
DBN的主要思路是通过RBM对输入进行编码,然后将编码后的数据通过BP神经网络进行模式判断
*/

template<typename val_t, int iv, int ih, int...is>
struct dbn
{
	restricked_boltzman_machine<iv, ih, val_t>	rbm;
	dbn<val_t, ih, is...>						dbn_next;
	using ret_type = typename dbn<val_t, ih, is...>::ret_type;


	void pretrain(const std::vector<mat<iv, 1> >& vec, const int& i_epochs = 100) 
	{
		/* 训练当前层 */
		for (int i = 0; i < i_epochs; ++i)
			for (auto itr = vec.begin(); itr != vec.end(); ++itr) 
			{
				rbm.train(*itr);
			}
		/* 准备下层数据 */
		std::vector<mat<ih, 1, val_t> > vec_hs;
		for (auto itr = vec.begin(); itr != vec.end(); ++itr)
		{
			vec_hs.push_back(rbm.forward(*itr));
		}
		/* 用隐含层结果训练下一层 */
		dbn_next.pretrain(vec_hs, i_epochs);
	}

	void finetune(const std::vector<ret_type>& vec_expected, const int& i_epochs = 100)
	{
		dbn_next.finetune(vec_expected, i_epochs);
	}

	auto forward(const mat<iv, 1>& v1)
	{
		return dbn_next.forward(rbm.forward(v1));
	}
};

template<typename val_t, int iv, int ih
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Code provided by Ruslan Salakhutdinov and Geoff Hinton Permission is granted for anyone to copy, use, modify, or distribute this program and accompanying programs and documents for any purpose, provided this copyright notice is retained and prominently displayed, along with a note saying that the original programs are available from our web page. The programs and documents are distributed without any warranty, express or implied. As the programs were written for research purposes only, they have not been tested to the degree that would be advisable in any important application. All use of these programs is entirely at the user's own risk. How to make it work: 1. Create a separate directory and download all these files into the same directory 2. Download from http://yann.lecun.com/exdb/mnist the following 4 files: o train-images-idx3-ubyte.gz o train-labels-idx1-ubyte.gz o t10k-images-idx3-ubyte.gz o t10k-labels-idx1-ubyte.gz 3. Unzip these 4 files by executing: o gunzip train-images-idx3-ubyte.gz o gunzip train-labels-idx1-ubyte.gz o gunzip t10k-images-idx3-ubyte.gz o gunzip t10k-labels-idx1-ubyte.gz If unzipping with WinZip, make sure the file names have not been changed by Winzip. 4. Download Conjugate Gradient code minimize.m 5. Download Autoencoder_Code.tar which contains 13 files OR download each of the following 13 files separately for training an autoencoder and a classification model: o mnistdeepauto.m Main file for training deep autoencoder o mnistclassify.m Main file for training classification model o converter.m Converts raw MNIST digits into matlab format o rbm.m Training RBM with binary hidden and binary visible units o rbmhidlinear.m Training RBM with Gaussian hidden and binary visible units o backprop.m Backpropagation for fine-tuning an autoencoder o backpropclassify.m Backpropagation for classification using "encoder" network o CG_MNIST.m Conjugate Gradient optimization for fine-tuning an autoencoder o CG_CLASSIFY_INIT.m Co

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

腾昵猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值