当前位置: 首页 > 编程日记 > 正文

吴恩达老师深度学习视频课笔记:逻辑回归公式推导及C++实现

逻辑回归(Logistic Regression)是一个二分分类算法。逻辑回归的目标是最小化其预测与训练数据之间的误差。为了训练逻辑回归模型中的参数w和b,需要定义一个成本函数(cost function)。

成本函数(cost function):它是针对整个训练集的。衡量参数w和b在整个训练集上的效果。

损失函数或误差函数(loss function or error function):它是针对单个训练样本进行定义的。可以用来衡量算法的效果,衡量预测输出值与实际值有多接近。

梯度下降法的核心是最小化成本函数。使用梯度下降法可以找到一个函数的局部极小值。

关于逻辑回归的介绍可以参考: http://blog.csdn.net/fengbingchun/article/details/78283675

关于梯度下降法的介绍可以参考: http://blog.csdn.net/fengbingchun/article/details/75351323

关于激活函数sigmoid函数的介绍可以参考: http://blog.csdn.net/fengbingchun/article/details/73848734

关于MNIST数据集的介绍可以参考:  http://blog.csdn.net/fengbingchun/article/details/49611549

以下截图来自吴恩达老师深度学习视频课:




以下code是完全按照上面的推导公式进行实现的,训练数据集为从MNIST中train中随机选取的0、1各10个图像;测试数据集为从MNIST中test中随机选取的0、1各10个图像,如下图,其中第一排前10个0用于训练,后10个0用于测试;第二排前10个1用于训练,后10个1用于测试:


logistic_regression2.hpp:

#ifndef FBC_SRC_NN_LOGISTIC_REGRESSION2_HPP_
#define FBC_SRC_NN_LOGISTIC_REGRESSION2_HPP_#include <vector>
#include <string>namespace ANN {template<typename T>
class LogisticRegression2 { // two categories
public:LogisticRegression2() = default;int init(const T* data, const T* labels, int train_num, int feature_length, T learning_rate = 0.00001, int iterations = 10000);int train(const std::string& model);int load_model(const std::string& model);T predict(const T* data, int feature_length) const; // y = 1/(1+exp(-(wx+b)))private:int store_model(const std::string& model) const;T calculate_sigmoid(T value) const; // y = 1/(1+exp(-value))T calculate_z(const std::vector<T>& feature) const;std::vector<std::vector<T>> x; // training setstd::vector<T> y; // ground truth labelsint iterations = 1000;int m = 0; // train samples numint feature_length = 0;T alpha = (T)0.00001; // learning ratestd::vector<T> w; // weightsT b = (T)0.; // threshold
}; // class LogisticRegression2} // namespace ANN#endif // FBC_SRC_NN_LOGISTIC_REGRESSION2_HPP_
logistic_regression2.cpp:

#include "logistic_regression2.hpp"
#include <fstream>
#include <algorithm>
#include <random>
#include <cmath>
#include "common.hpp"namespace ANN {template<typename T>
int LogisticRegression2<T>::init(const T* data, const T* labels, int train_num, int feature_length, T learning_rate, int iterations)
{if (train_num < 2) {fprintf(stderr, "logistic regression train samples num is too little: %d\n", train_num);return -1;}if (learning_rate <= 0) {fprintf(stderr, "learning rate must be greater 0: %f\n", learning_rate);return -1;}if (iterations <= 0) {fprintf(stderr, "number of iterations cannot be zero or a negative number: %d\n", iterations);return -1;}this->alpha = learning_rate;this->iterations = iterations;this->m = train_num;this->feature_length = feature_length;this->x.resize(train_num);this->y.resize(train_num);for (int i = 0; i < train_num; ++i) {const T* p = data + i * feature_length;this->x[i].resize(feature_length);for (int j = 0; j < feature_length; ++j) {this->x[i][j] = p[j];}this->y[i] = labels[i];}return 0;
}template<typename T>
T LogisticRegression2<T>::calculate_z(const std::vector<T>& feature) const
{T z{ 0. };for (int i = 0; i < this->feature_length; ++i) {z += w[i] * feature[i];}z += b;return z;
}template<typename T>
int LogisticRegression2<T>::train(const std::string& model)
{CHECK(x.size() == y.size());w.resize(this->feature_length, (T)0.);std::random_device rd;std::mt19937 generator(rd());std::uniform_real_distribution<T> distribution(-0.1, 0.1);for (int i = 0; i < this->feature_length; ++i) {w[i] = distribution(generator);}b = distribution(generator);for (int iter = 0; iter < this->iterations; ++iter) {T J = (T)0., db = (T)0.;std::vector<T> dw(this->feature_length, (T)0.);std::vector<T> z(this->m, (T)0), a(this->m, (T)0), dz(this->m, (T)0);for (int i = 0; i < this->m; ++i) {z[i] = calculate_z(x[i]); // z(i)=w^T*x(i)+ba[i] = calculate_sigmoid(z[i]); // a(i)= 1/(1+e^(-z(i)))J += -(y[i] * std::log(a[i]) + (1 - y[i] * std::log(1 - a[i]))); // J+=-[y(i)*loga(i)+(1-y(i))*log(1-a(i))]dz[i] = a[i] - y[i]; // dz(i) = a(i)-y(i)for (int j = 0; j < this->feature_length; ++j) {dw[j] += x[i][j] * dz[i]; // dw(i)+=x(i)(j)*dz(i)}db += dz[i]; // db+=dz(i)}J /= this->m;for (int j = 0; j < this->feature_length; ++j) {dw[j] /= m;}db /= m;for (int j = 0; j < this->feature_length; ++j) {w[j] -= this->alpha * dw[j];}b -= this->alpha*db;}CHECK(store_model(model) == 0);return 0;
}template<typename T>
int LogisticRegression2<T>::load_model(const std::string& model)
{std::ifstream file;file.open(model.c_str(), std::ios::binary);if (!file.is_open()) {fprintf(stderr, "open file fail: %s\n", model.c_str());return -1;}int length{ 0 };file.read((char*)&length, sizeof(length));this->w.resize(length);this->feature_length = length;file.read((char*)this->w.data(), sizeof(T)*this->w.size());file.read((char*)&this->b, sizeof(T));file.close();return 0;
}template<typename T>
T LogisticRegression2<T>::predict(const T* data, int feature_length) const
{CHECK(feature_length == this->feature_length);T value{ (T)0. };for (int t = 0; t < this->feature_length; ++t) {value += data[t] * this->w[t];}value += this->b;return (calculate_sigmoid(value));
}template<typename T>
int LogisticRegression2<T>::store_model(const std::string& model) const
{std::ofstream file;file.open(model.c_str(), std::ios::binary);if (!file.is_open()) {fprintf(stderr, "open file fail: %s\n", model.c_str());return -1;}int length = w.size();file.write((char*)&length, sizeof(length));file.write((char*)w.data(), sizeof(T) * w.size());file.write((char*)&b, sizeof(T));file.close();return 0;
}template<typename T>
T LogisticRegression2<T>::calculate_sigmoid(T value) const
{return ((T)1 / ((T)1 + exp(-value)));
}template class LogisticRegression2<float>;
template class LogisticRegression2<double>;} // namespace ANN
main.cpp:

#include "funset.hpp"
#include <iostream>
#include "perceptron.hpp"
#include "BP.hpp""
#include "CNN.hpp"
#include "linear_regression.hpp"
#include "naive_bayes_classifier.hpp"
#include "logistic_regression.hpp"
#include "common.hpp"
#include "knn.hpp"
#include "decision_tree.hpp"
#include "pca.hpp"
#include <opencv2/opencv.hpp>
#include "logistic_regression2.hpp"// ================================ logistic regression =====================
int test_logistic_regression2_train()
{const std::string image_path{ "E:/GitCode/NN_Test/data/images/digit/handwriting_0_and_1/" };cv::Mat data, labels;for (int i = 1; i < 11; ++i) {const std::vector<std::string> label{ "0_", "1_" };for (const auto& value : label) {std::string name = std::to_string(i);name = image_path + value + name + ".jpg";cv::Mat image = cv::imread(name, 0);if (image.empty()) {fprintf(stderr, "read image fail: %s\n", name.c_str());return -1;}data.push_back(image.reshape(0, 1));}}data.convertTo(data, CV_32F);std::unique_ptr<float[]> tmp(new float[20]);for (int i = 0; i < 20; ++i) {if (i % 2 == 0) tmp[i] = 0.f;else tmp[i] = 1.f;}labels = cv::Mat(20, 1, CV_32FC1, tmp.get());ANN::LogisticRegression2<float> lr;const float learning_rate{ 0.0001f };const int iterations{ 10000 };int ret = lr.init((float*)data.data, (float*)labels.data, data.rows, data.cols);if (ret != 0) {fprintf(stderr, "logistic regression init fail: %d\n", ret);return -1;}const std::string model{ "E:/GitCode/NN_Test/data/logistic_regression2.model" };ret = lr.train(model);if (ret != 0) {fprintf(stderr, "logistic regression train fail: %d\n", ret);return -1;}return 0;
}int test_logistic_regression2_predict()
{const std::string image_path{ "E:/GitCode/NN_Test/data/images/digit/handwriting_0_and_1/" };cv::Mat data, labels, result;for (int i = 11; i < 21; ++i) {const std::vector<std::string> label{ "0_", "1_" };for (const auto& value : label) {std::string name = std::to_string(i);name = image_path + value + name + ".jpg";cv::Mat image = cv::imread(name, 0);if (image.empty()) {fprintf(stderr, "read image fail: %s\n", name.c_str());return -1;}data.push_back(image.reshape(0, 1));}}data.convertTo(data, CV_32F);std::unique_ptr<int[]> tmp(new int[20]);for (int i = 0; i < 20; ++i) {if (i % 2 == 0) tmp[i] = 0;else tmp[i] = 1;}labels = cv::Mat(20, 1, CV_32SC1, tmp.get());CHECK(data.rows == labels.rows);const std::string model{ "E:/GitCode/NN_Test/data/logistic_regression2.model" };ANN::LogisticRegression2<float> lr;int ret = lr.load_model(model);if (ret != 0) {fprintf(stderr, "load logistic regression model fail: %d\n", ret);return -1;}for (int i = 0; i < data.rows; ++i) {float probability = lr.predict((float*)(data.row(i).data), data.cols);fprintf(stdout, "probability: %.6f, ", probability);if (probability > 0.5) fprintf(stdout, "predict result: 1, ");else fprintf(stdout, "predict result: 0, ");fprintf(stdout, "actual result: %d\n", ((int*)(labels.row(i).data))[0]);}return 0;
}
测试结果如下:由执行结果可知,测试图像全部分类正确。由于w和b初始值是随机产生的,因此每次执行的结果多少有些差异。

GitHub: https://github.com/fengbingchun/NN_Test

相关文章:

网络运行时间提高100倍,Google使用的AI视频理解架构有多强?

译者 | 刘畅出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;视频理解是一个很有挑战性的问题。由于视频包含时空数据&#xff0c;因此图像的特征表示需要同时提取图像和运动信息。这不仅对自动理解视频语义内容有重要性&#xff0c;还对机器人的感知和学习也至关重要…

iOS学习笔记(十三)——获取手机信息(UIDevice、NSBundle、NSLocale)

2019独角兽企业重金招聘Python工程师标准>>> iOS的APP的应用开发的过程中&#xff0c;有时为了bug跟踪或者获取用反馈的需要自动收集用户设备、系统信息、应用信息等等&#xff0c;这些信息方便开发者诊断问题&#xff0c;当然这些信息是用户的非隐私信息&#xff0…

吴恩达老师深度学习视频课笔记:单隐含层神经网络公式推导及C++实现(二分类)

关于逻辑回归的公式推导和实现可以参考&#xff1a; http://blog.csdn.net/fengbingchun/article/details/79346691 下面是在逻辑回归的基础上&#xff0c;对单隐含层的神经网络进行公式推导&#xff1a;选择激活函数时的一些经验&#xff1a;不同层的激活函数可以不一样。如果…

「2019中国大数据技术大会」超值学生票来啦!

大会官网&#xff1a;https://t.csdnimg.cn/U1wA经过11年的沉淀与发展&#xff0c;中国大数据技术大会见证了大数据技术生态在中国的建立、发展和成熟&#xff0c;已经成为国内大数据行业极具影响力的盛会&#xff0c;也是大数据人非常期待的年度深度分享盛会。在新的时代背景下…

校验正确获取对象或者数组的属性方法(babel-plugin-idx/_.get)

背景&#xff1a; 开发中经常遇到取值属性的时候&#xff0c;需要校验数值的有效性。 例如&#xff1a; 获取props对象里面的friends属性 props.user && props.user.friends && props.user.friends[0] && props.user.friends[0].friends 对于深层的对…

Ring Tone Manager on Windows Mobile

2019独角兽企业重金招聘Python工程师标准>>> 手机铃声经常能够体现一个人的个性&#xff0c;有些哥们儿在自习室不把手机设置成震动&#xff0c;一来电就#$^%^&^%#$&$*&#xff0c;声音还很大&#xff0c;唯恐别人听不到。 Windows Mobile设备上如何来设置手…

OpenCV3.3中K-Means聚类接口简介及使用

OpenCV3.3中给出了K-均值聚类(K-Means)的实现&#xff0c;即接口cv::kmeans&#xff0c;接口的声明在include/opencv2/core.hpp文件中&#xff0c;实现在modules/core/src/kmeans.cpp文件中,其中&#xff1a;下面对此接口中的参数作个简单说明&#xff1a;(1)、data&#xff1a…

一文读懂对抗机器学习Universal adversarial perturbations | CSDN博文精选

作者 | Icoding_F2014来源 | CSDN博客本文提出一种 universal 对抗扰动&#xff0c;universal 是指同一个扰动加入到不同的图片中&#xff0c;能够使图片被分类模型误分类&#xff0c;而不管图片到底是什么。示意图&#xff1a;形式化的定义&#xff1a;对于d维数据分布 μ&…

Reactor模式与Proactor模式

博主一脚刚踏进分布式的大门&#xff08;看《分布式Java应用》&#xff0c;如果大家有啥推荐的书欢迎留言~&#xff09;&#xff0c;发现书中对NIO采用的Reactor模式、AIO采用的Proactor模式一笔带过&#xff0c;好奇心趋势我找了一下文章&#xff0c;发现两篇挺不错的文章&…

linux下使profile和.bash_profile立即生效的方法

使profile生效的方法1.source /etc/profile使用.bash_profile生效的方法1 . .bash_profile2 source .bash_profile3 exec bash --login转载于:https://blog.51cto.com/shine20/1436473

吴恩达老师深度学习视频课笔记:多隐含层神经网络公式推导(二分类)

多隐含层神经网络的推导步骤非常类似于单隐含层神经网络的步骤&#xff0c;只不过是多重复几遍。关于单隐含层神经网络公式的推导可以参考&#xff1a; http://blog.csdn.net/fengbingchun/article/details/79370310 逻辑回归是一个浅层模型(shadow model)&#xff0c;或称单层…

Python中的元编程:一个关于修饰器和元类的简单教程

作者 | Saurabh Kukade译者 | 刘畅出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;最近&#xff0c;作者遇到一个非常有趣的概念&#xff0c;它就是用 Python 进行元编程。我想在本文中分享我对该主题的见解。作者希望它可以帮助解决这个问题&#xff0c;因为很多人说…

获取用户电脑的上网IP地址

在项目中经常要获取用户的上网的IP地址&#xff0c;如何获取用户的IP地址&#xff0c;方法很多&#xff0c;现在介绍以下2种。 /// <summary> /// 获取本机在局域网的IP地址 /// </summary> /// <returns></returns> …

数学图形(1.40)T_parameter

不记得在哪搞了个数学公式生成的图形. vertices 1000t from 0 to (2*PI) r 2.0 x r*(5*cos(t) - cos(6*t)) y r*(3*sin(t) - sin(4*t)) 给线加上一维变量的变化,使之变成面: vertices D1:360 D2:21u from 0 to (2*PI) D1 v from 0 to 20 D2x (v2)*cos(u) - cos((v3)*u…

K-均值聚类(K-Means) C++代码实现

K-均值聚类(K-Means)简介可以参考&#xff1a; http://blog.csdn.net/fengbingchun/article/details/79276668 以下是K-Means的C实现&#xff0c;code参考OpenCV 3.3中的cv::kmeans函数&#xff0c;均值点初始化的方法仅支持KMEANS_RANDOM_CENTERS。以下是从数据集MNIST中提取…

让学生网络相互学习,为什么深度相互学习优于传统蒸馏模型?| 论文精读

作者 | Ying Zhang&#xff0c;Tao Xiang等译者 | 李杰出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;蒸馏模型是一种将知识从教师网络&#xff08;teacher&#xff09;传递到学生网络&#xff08;student&#xff09;的有效且广泛使用的技术。通常来说&#xff0c;…

mac apache 配置

mac系统自带apache这无疑给广大的开发朋友提供了便利&#xff0c;接下来是针对其中的一些说明 一、自带apache相关命令 1. sudo apachectl start 启动服务&#xff0c;需要权限&#xff0c;就是你计算机的password 2. sudo apachectl stop 终止服务 ####3. sudo apachectl rest…

jQuery学习---------认识事件处理

3种事件模型&#xff1a;原始事件模型DOM事件模型IE事件模型原始事件模型&#xff08;0级事件模型&#xff09;1、事件处理程序被定义为函数实例&#xff0c;然后绑定到DOM元素事件对象上&#xff0c;实现事件的注册。例子&#xff1a;var btn document.getElementsByTagName(…

C++中的虚函数表介绍

在C语言中,当我们使用基类的引用或指针调用一个虚成员函数时会执行动态绑定。因为我们直到运行时才能知道到底调用了哪个版本的虚函数&#xff0c;所以所有虚函数都必须有定义。通常情况下&#xff0c;如果我们不使用某个函数&#xff0c;则无须为该函数提供定义。但是我们必须…

AI如何赋能金融行业?百度、图灵深视等同台分享技术实践

近日&#xff0c;由BTCMEX举办的金融技术创新研讨会在北京举办。BTCMEX投资人李笑来&#xff0c;AI技术公司TuringPass、百度、美国Apache基金会项目Pulsar、区块链安全公司SlowMist等相关专家参加了此次会议&#xff0c;共同探讨了金融技术在创新方面的现状。 图灵深视副总裁许…

【Win32 API学习]打开可执行文件

在MFC中打开其他可执行文件常用到的方法有&#xff1a;WinExec、ShellExecute、CreatProcess。 1.WinExec WinExec 主要运行EXE文件&#xff0c;用法简单&#xff0c;只有两个参数&#xff0c;前一个指定命令路径&#xff0c;后一个指定窗口显示方式&#xff1a; UINT WinExec(…

支付宝接口使用文档说明 支付宝异步通知

支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url. 现支付宝的通知有两类。 A服务器通知&#xff0c;对应的参数为notify_url&#xff0c;支付宝通知使用POST方式 B页面跳转通知&#xff0c;对应的参数为return_url&#xff0c;支付宝通知使用GET方式 &#xff…

完全隐藏Master Page Site Actions菜单只有管理员才可以看见

1. 在Master Page Head 增加下面的Style <style type"text/css"> .ms-cui-tt{visibility:hidden;} </style> 2. 增加SPSecurityTrimmedControl <SharePoint:SPRibbonPeripheralContent runat"server" Location"TabRowLeft&qu…

深度学习中的随机梯度下降(SGD)简介

随机梯度下降(Stochastic Gradient Descent, SGD)是梯度下降算法的一个扩展。机器学习中反复出现的一个问题是好的泛化需要大的训练集&#xff0c;但大的训练集的计算代价也更大。机器学习算法中的代价函数通常可以分解成每个样本的代价函数的总和。随着训练集规模增长为数十亿…

推荐系统中的前沿技术研究与落地:深度学习、AutoML与强化学习 | AI ProCon 2019...

整理 | 夕颜出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;个性化推荐算法滥觞于互联网的急速发展&#xff0c;随着国内外互联网公司&#xff0c;如 Netflix 在电影领域&#xff0c;亚马逊、淘宝、京东等在电商领域&#xff0c;今日头条在内容领域的采用和推动&…

运维日志管理系统

因公司数据安全和分析的需要&#xff0c;故调研了一下 GlusterFS lagstash elasticsearch kibana 3 redis 整合在一起的日志管理应用&#xff1a;安装&#xff0c;配置过程&#xff0c;使用情况等续一&#xff0c;glusterfs分布式文件系统部署&#xff1a;说明&#xf…

NLP学习思维导图,非常的全面和清晰

作者 | Tae Hwan Jung & Kyung Hee编译 | ronghuaiyang【导读】Github上有人整理了NLP的学习路线图&#xff08;思维导图&#xff09;&#xff0c;非常的全面和清晰&#xff0c;分享给大家。先奉上GitHub地址&#xff1a;https://github.com/graykode/nlp-roadmapnlp-roadm…

Go在windows10 64位上安装过程

1. 从 https://golang.org/dl/ 下载最新的发布版本go1.10即go1.10.windows-amd64.msi; 2. 双击go1.10.windows-amd64.msi ,使用默认选项&#xff0c;默认会安装到C:\Go目录下&#xff1b; 3. 将C:\Go\bin目录添加到系统环境变量中(默认已自动添加)&#xff0c;此目录下有go.exe…

Windows SharePoint Services 3.0 应用程序模板

微软发布的一些WSS模板&#xff0c;看了一下&#xff0c;跟以前看到的模板好像不同模板分两类&#xff0c;一类是站点管理模板&#xff0c;一类是服务器管理模板站点管理模板&#xff1a;董事会、业务绩效报告、政府机构案例管理、课堂管理、临床试验启动和管理、竞争性分析站点…

HAProxy+Keepalived高可用负载均衡配置

一、系统环境&#xff1a;系统版本&#xff1a;CentOS5.5 x86_64master_ip:172.20.27.40backup_ip:172.20.27.50 vip:172.20.27.200web_1: 172.20.27.90web_2:172.20.27.100二、haproxy安装&#xff1a;1.首先172.20.27.40安装上安装&#xff1a;1.1安装 tar zxvf haproxy-1.3.…