Dlib库【8】——多层神经网络

结果如图所示:(多次运行结果不一样)//小样本正确性有待商榷……





程序如下:

// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*

只是一个利用Dlib库训练多层神经网络的示例程序

*/


#include <iostream>
#include <dlib/mlp.h>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace dlib;
using namespace cv;

int main()
{
	//  定义2*1的矩阵类型
	typedef matrix<double, 2, 1> sample_type;


	// make an instance of a sample matrix so we can use it below
	sample_type sample;

	/*
	创建一个多层神经网络,这个网络输入层有两个节点,第一个影藏层有五个节点
	其他4个参数用默认值
	*/
	mlp::kernel_1a_c net(2, 5);

	/*
	现在往里面丢训练样本
	把41*41的样本按距离原点是否大于10来训练
	*/


	for (int i = 0; i < 5000; ++i)
	{
		for (int r = -20; r <= 20; ++r)
		{
			for (int c = -20; c <= 20; ++c)
			{
				sample(0) = r;
				sample(1) = c;

				// 如果这个点小于等于10,类别为1
				if (sqrt((double)r*r + c*c) <= 10)
					net.train(sample, 1);//输入sample,输出为1
				else
					net.train(sample, 0);
			}
		}
	}

	//来看看结果,每次运行下来结果都不一样……因为每次初始化都不一样
	//以400*400的图片为例,绿的为样本1,蓝的为样本0



	int width = 400, height = 400;
	Mat image = Mat::zeros(height, width, CV_8UC3);
	Vec3b green(0, 255, 0), blue(255, 0, 0);
	for (int i = 0; i < image.rows; ++i)
	{
		for (int j = 0; j < image.cols; ++j)
		{
			//Mat sampleMat = (Mat_<float>(1, 2) << i, j);
			sample(0) = (i - 200) / 10.0;
			sample(1) = (j - 200) / 10.0;
		
			
			if (net(sample)>0.5)
			{
				image.at<Vec3b>(j, i) = green;
			}
			else
			{
				image.at<Vec3b>(j, i) = blue;
			}
		}
	}

	imshow("result", image);
	waitKey(-1);

}





原示例程序如下所示:


// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*

只是一个利用Dlib库训练多层神经网络的示例程序

*/


#include <iostream>
#include <dlib/mlp.h>

using namespace std;
using namespace dlib;


int main()
{
	// The mlp takes column vectors as input and gives column vectors as output.  The dlib::matrix
	// object is used to represent the column vectors. So the first thing we do here is declare 
	// a convenient typedef for the matrix object we will be using.

	// This typedef declares a matrix with 2 rows and 1 column.  It will be the
	// object that contains each of our 2 dimensional samples.   (Note that if you wanted 
	// more than 2 features in this vector you can simply change the 2 to something else)
	typedef matrix<double, 2, 1> sample_type;


	// make an instance of a sample matrix so we can use it below
	sample_type sample;

	// Create a multi-layer perceptron network.   This network has 2 nodes on the input layer 
	// (which means it takes column vectors of length 2 as input) and 5 nodes in the first 
	// hidden layer.  Note that the other 4 variables in the mlp's constructor are left at
	// their default values.  
	mlp::kernel_1a_c net(2, 5);

	// Now let's put some data into our sample and train on it.  We do this
	// by looping over 41*41 points and labeling them according to their
	// distance from the origin.
	for (int i = 0; i < 1000; ++i)
	{
		for (int r = -20; r <= 20; ++r)
		{
			for (int c = -20; c <= 20; ++c)
			{
				sample(0) = r;
				sample(1) = c;

				// if this point is less than 10 from the origin
				if (sqrt((double)r*r + c*c) <= 10)
					net.train(sample, 1);
				else
					net.train(sample, 0);
			}
		}
	}

	// Now we have trained our mlp.  Let's see how well it did.  
	// Note that if you run this program multiple times you will get different results. This
	// is because the mlp network is randomly initialized.

	// each of these statements prints out the output of the network given a particular sample.

	sample(0) = 3.123;
	sample(1) = 4;
	cout << "This sample should be close to 1 and it is classified as a " << net(sample) << endl;

	sample(0) = 13.123;
	sample(1) = 9.3545;
	cout << "This sample should be close to 0 and it is classified as a " << net(sample) << endl;

	sample(0) = 13.123;
	sample(1) = 0;
	cout << "This sample should be close to 0 and it is classified as a " << net(sample) << endl;
	system("pause");
}



祝机器学习愉快~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

朱铭德

五毛也是爱٩(●´৺`●)૭

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

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

打赏作者

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

抵扣说明:

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

余额充值