【deep learning学习笔记】注释yusugomori的LR代码 --- LogisticRegression.cpp

模型实现代码,关键是train函数和predict函数,都很容易。

 

#include <iostream>
#include <string>
#include <math.h>
#include "LogisticRegression.h"
using namespace std;LogisticRegression::LogisticRegression(int size, 	// Nint in, 	// n_inint out		// n_out) 
{N = size;n_in = in;n_out = out;// initialize W, b// W[n_out][n_in], b[n_out]W = new double*[n_out];for(int i=0; i<n_out; i++) W[i] = new double[n_in];b = new double[n_out];for(int i=0; i<n_out; i++) {for(int j=0; j<n_in; j++) {W[i][j] = 0;}b[i] = 0;}
}LogisticRegression::~LogisticRegression() 
{for(int i=0; i<n_out; i++) delete[] W[i];delete[] W;delete[] b;
}void LogisticRegression::train (int *x, 	// the input from input nodes in training setint *y, 	// the output from output nodes in training setdouble lr	// the learning rate) 
{// the probability of P(y|x)double *p_y_given_x = new double[n_out];// the tmp variable which is not necessary being an arraydouble *dy = new double[n_out];// step 1: calculate the output of softmax given inputfor(int i=0; i<n_out; i++) {// initializep_y_given_x[i] = 0;for(int j=0; j<n_in; j++) {// the weight of networksp_y_given_x[i] += W[i][j] * x[j];}// the biasp_y_given_x[i] += b[i];}// the softmax valuesoftmax(p_y_given_x);// step 2: update the weight of networks// w_new = w_old + learningRate * differential (导数)//		 = w_old + learningRate * x (1{y_i=y} - p_yi_given_x) //		 = w_old + learningRate * x * (y - p_y_given_x)for(int i=0; i<n_out; i++) {dy[i] = y[i] - p_y_given_x[i];for(int j=0; j<n_in; j++) {W[i][j] += lr * dy[i] * x[j] / N;}b[i] += lr * dy[i] / N;}delete[] p_y_given_x;delete[] dy;
}void LogisticRegression::softmax (double *x) 
{double max = 0.0;double sum = 0.0;// step1: get the max in the X vectorfor(int i=0; i<n_out; i++) if(max < x[i]) max = x[i];// step 2: normalization and softmax// normalize -- 'x[i]-max', it's not necessary in traditional LR.// I wonder why it appears here? for(int i=0; i<n_out; i++) {x[i] = exp(x[i] - max);sum += x[i];} for(int i=0; i<n_out; i++) x[i] /= sum;
}void LogisticRegression::predict(int *x, 	// the input from input nodes in testing setdouble *y	// the calculated softmax probability) 
{// get the softmax output value given the current networksfor(int i=0; i<n_out; i++) {y[i] = 0;for(int j=0; j<n_in; j++) {y[i] += W[i][j] * x[j];}y[i] += b[i];}softmax(y);
}


 

 

转载于:https://www.cnblogs.com/dyllove98/p/3194108.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/408641.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

5月份 Github 上最热的十个 Python 项目,从Debug工具到AI水军、量化交易系统。

2019 年第 46 篇&#xff0c;总第 70 篇文章原文地址&#xff1a;https://medium.mybridge.co/python-open-source-for-the-past-month-v-may-2019-473e9f60c73f5 月份刚刚过去&#xff0c;之前看到了一篇介绍 5 月份的最热机器学习项目&#xff0c;刚好看到 Mybridge AI 博客又…

注册.NET Framework

C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe /i 转载于:https://www.cnblogs.com/qq1223558/p/3195092.html

[Github项目]基于PyTorch的深度学习网络模型实现

2019 年第 48 篇文章&#xff0c;总第 72 篇文章本文大约 1500 字&#xff0c;阅读大约需要 4 分钟今天主要分享两份 Github 项目&#xff0c;都是采用 PyTorch 来实现深度学习网络模型&#xff0c;主要是一些常用的模型&#xff0c;包括如 ResNet、DenseNet、ResNext、SENet等…

抽象类总结及练习

该篇文章只是说明抽象类的一个使用场景&#xff0c;由于小弟水平一般&#xff0c;如果有说的不对地方希望各位大牛们指出&#xff0c;也欢迎各位亲们补充。 什么是抽象类 用关键字abstract定义的类即为抽象类&#xff0c;且只能作为基类。 抽象类注意的地方 1、 用abstract定义…

Python基础入门_4函数

Python 基础入门前三篇&#xff1a; Python 基础入门–简介和环境配置Python基础入门_2基础语法和变量类型Python基础入门_3条件语句和迭代循环 第四篇内容&#xff0c;这次介绍下函数的基本用法&#xff0c;包括函数的定义、参数的类型、匿名函数、变量作用域以及从模块导入…

event auto模式的问题

。。。转载于:https://www.cnblogs.com/bluebbc/p/3196777.html

一文了解神经网络的基本原理

这是简单介绍神经网络的知识&#xff0c;并介绍一种特别的神经网络–多层感知器(Multi Layer Perceptron,MLP)。 翻译自 https://ujjwalkarn.me/2016/08/09/quick-intro-neural-networks/ 这篇文章并不涉及到对数学公式的推导&#xff0c;只是简单介绍了神经网络的结构和基本…

详解javascript中的call, apply

一些学js的同学一看到call, apply, 就蒙了, 感觉不好懂, 看的头大. 今天我们就一起来研究一下这2个东东.彻底弄清楚它们的用法. 定义: call, apply是函数的方法, 只有函数才有这2个方法.作用: call, apply主要作用是改变函数赖以执行的作用域, 简言之就是改变函数中this的指向.…

[Python技巧]是时候用 defaultdict 和 Counter 代替 dictionary 了

我们在采用 dict 的时候&#xff0c;一般都需要判断键是否存在&#xff0c;如果不存在&#xff0c;设置一个默认值&#xff0c;存在则采取其他的操作&#xff0c;但这个做法其实需要多写几行代码&#xff0c;那么是否有更高效的写法&#xff0c;可以减少代码&#xff0c;但可读…

数据库索引应用

一、索引的概念索引就是加快检索表中数据的方法。数据库的索引类似于书籍的索引。在书籍中&#xff0c;索引允许用户不必翻阅完整个书就能迅速地找到所需要的信息。在数据库中&#xff0c;索引也允许数据库程序迅速地找到表中的数据&#xff0c;而不必扫描整个数据库。二、索引…

[Github推荐]CVPR2019录用论文下载及可视化论文网站

简介 CVPR 是 IEEE Conference on Computer Vision and Pattern Recognition 的缩写&#xff0c;即 IEEE 国际计算机视觉与模式识别会议。该会议是由 IEEE 举办的计算机视觉和模式识别领域的顶级会议。 它是 IEEE 一年一度的学术性会议&#xff0c;会议的主要内容是计算机视觉…

UNIX网络编程——fcntl函数

fcntl函数提供了与网络编程相关的如下特性&#xff1a;非阻塞式I/O。 通过使用F_SETFL命令设置O_NONBLOCK文件状态标志&#xff0c;我们可以把一个套接字设置为非阻塞型。信号驱动式I/O。 通过使用F_SETFL命令设置O_ASYNC文件状态标志&#xff0c;我们可以把一个套接字设置成O…

如何远程访问服务器的 Jupyter notebook

图来自 Unsplash 网站&#xff0c;作者&#xff1a;Christopher Gower2019 年第 52 篇文章&#xff0c;总第 76 篇文章本文大约 4600 字&#xff0c;阅读大约需要 12 分钟写在前面当我们拥有一台服务器的时候&#xff0c;通常服务器都可能包含比本地电脑比较好的配置&#xff0…