Dlib库【8】——多层神经网络
2017-05-19 10:15
232 查看
结果如图所示:(多次运行结果不一样)//小样本正确性有待商榷……
程序如下:
原示例程序如下所示:
祝机器学习愉快~
程序如下:
// 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"); }
祝机器学习愉快~
相关文章推荐
- 多层神经网络与反向传播算法
- ufldl学习笔记与编程作业:Multi-Layer Neural Network(多层神经网络+识别手写体编程)
- 神经网络基本介绍(四):前馈网络(下)多层感知机
- 如何用Python创建一个多层神经网络
- 【UFLDL】多层神经网络
- 神经网络之多层感知机MLP的实现(Python+TensorFlow)
- 小白学Tensorflow之多层神经网络
- “深度学习”和“多层神经网络”的区别
- 详解多层神经网络 Python源码
- 多层神经网络(MLP)快速入门
- 神经网络入门之构建多层网络
- 如何用Python创建一个多层神经网络
- Tensorlayer学习笔记——多层神经网络
- 使用反向传播算法训练多层神经网络(图示)
- 用Tensorflow实现多层神经网络
- 神经网络:多层网络与C++实现
- 深层神经网络——多层网络解决异或运算
- 模式分类 学习笔记 第6章 多层神经网络
- 深度学习之自编码器和多层神经网络