神经网络pooling层

博主闲来无事,写了pooling函数,由于博主非计算机专业,代码大神可忽略此博文,只是为了熟悉神经网络之用。

注意: 只针对2x2pooling,stride=2,其他pooling修改代码即可.

 

代码经过博主测试,准确无误!

数据matrix.h文件

 

//卷积网络的max_pooling函数
//注意只针对2x2pooling,stride=2
//其他pooling修改代码即可
//作者samylee
#ifndef MATRIX_H
#define MATRIX_H

typedef struct
{
	int length;
	float *idata;
}Inmatrix;

typedef struct
{
	int length;
	float *mdata;
}Midtrix;

typedef struct
{
	int length;
	float *odata;
}Outrix;

#endif

 

 

 

 

 

主函数main.cpp

 

//卷积网络的max_pooling函数
//注意只针对2x2pooling,stride=2
//其他pooling修改代码即可
//作者samylee
#include <iostream>
#include "matrix.h"

using namespace std;

int main()
{
	//定义输入矩阵
	Inmatrix *pool = new Inmatrix;
	int map_w = 6;
	int map_h = 6;
	pool->length = map_w * map_h;
	pool->idata = new float[pool->length];
	for (int i = 0; i < pool->length; i++)
	{
		pool->idata[i] = i;
	}

	//定义转换矩阵
	const int pool_size = 2;
	const int pool_stride = 2;

	int poolo_w = map_w / pool_size;
	int poolo_h = map_h / pool_size;

	int matrix_w = pool_size*pool_size;
	int matrix_h = poolo_w*poolo_h;

	Midtrix *pool_mid = new Midtrix;
	pool_mid->length = pool->length;
	pool_mid->mdata = new float[pool_mid->length];
	int block1 = pool_size * map_w;
	for (int i = 0; i < poolo_h; i++)
	{
		for (int j = 0; j < poolo_w; j++)
		{
			int block2 = j * pool_size;
			int block3 = i * block1;
			int wh = block3 + j * matrix_w;

			int col1 = block3 + block2;
			pool_mid->mdata[wh] = pool->idata[col1];
			pool_mid->mdata[wh + 1] = pool->idata[col1 + 1];

			int col2 = col1 + map_w;
			pool_mid->mdata[wh + 2] = pool->idata[col2];
			pool_mid->mdata[wh + 3] = pool->idata[col2 + 1];
		}
	}

	//定义输出矩阵,max_pool
	Outrix *out = new Outrix;
	out->length = matrix_h;
	out->odata = new float[matrix_h];
	for (int i = 0; i < matrix_h; i++)
	{
		int max = -1000;
		for (int j = 0; j < matrix_w; j++)
		{
			int block = i*matrix_w + j;
			max = pool_mid->mdata[block] > max ?
				pool_mid->mdata[block] : max;
		}
		out->odata[i] = max;
	}

	//验证结果
	cout << "input matrix is: " << endl;
	for (int i = 0; i < map_h; i++)
	{
		for (int j = 0; j < map_w; j++)
		{
			cout << pool->idata[i * map_w + j] << "\t";
		}
		cout << endl;
	}
	cout << endl;

	cout << "Middle matrix is: " << endl;
	for (int i = 0; i < matrix_h; i++)
	{
		for (int j = 0; j < matrix_w; j++)
		{
			cout << pool_mid->mdata[i * matrix_w + j] << "\t";
		}
		cout << endl;
	}
	cout << endl;

	cout << "Output matrix is: " << endl;
	for (int i = 0; i < poolo_h; i++)
	{
		for (int j = 0; j < poolo_w; j++)
		{
			cout << out->odata[i * poolo_w + j] << "\t";
		}
		cout << endl;
	}
	cout << endl;

	//释放内存
	delete[]pool->idata;
	delete pool;

	delete[]pool_mid->mdata;
	delete pool_mid;

	delete[]out->odata;
	delete out;

	system("pause");
	return 0;
}

 

 

 

 

 

效果如下:

 

 

任何问题请加唯一QQ2258205918(名称samylee)!

唯一VX:samylee_csdn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值