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

  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4. #include "LogisticRegression.h"
  5. using namespace std;
  6.  
  7. LogisticRegression::LogisticRegression(
  8. int size, // N
  9. int in, // n_in
  10. int out // n_out
  11. )
  12. {
  13. N = size;
  14. n_in = in;
  15. n_out = out;
  16.  
  17. // initialize W, b
  18. // W[n_out][n_in], b[n_out]
  19. W = new double*[n_out];
  20. for(int i=0; i<n_out; i++)
  21. W[i] = new double[n_in];
  22. b = new double[n_out];
  23.  
  24. for(int i=0; i<n_out; i++)
  25. {
  26. for(int j=0; j<n_in; j++)
  27. {
  28. W[i][j] = 0;
  29. }
  30. b[i] = 0;
  31. }
  32. }
  33.  
  34. LogisticRegression::~LogisticRegression()
  35. {
  36. for(int i=0; i<n_out; i++)
  37. delete[] W[i];
  38. delete[] W;
  39. delete[] b;
  40. }
  41.  
  42. void LogisticRegression::train (
  43. int *x, // the input from input nodes in training set
  44. int *y, // the output from output nodes in training set
  45. double lr // the learning rate
  46. )
  47. {
  48. // the probability of P(y|x)
  49. double *p_y_given_x = new double[n_out];
  50. // the tmp variable which is not necessary being an array
  51. double *dy = new double[n_out];
  52.  
  53. // step 1: calculate the output of softmax given input
  54. for(int i=0; i<n_out; i++)
  55. {
  56. // initialize
  57. p_y_given_x[i] = 0;
  58. for(int j=0; j<n_in; j++)
  59. {
  60. // the weight of networks
  61. p_y_given_x[i] += W[i][j] * x[j];
  62. }
  63. // the bias
  64. p_y_given_x[i] += b[i];
  65. }
  66. // the softmax value
  67. softmax(p_y_given_x);
  68.  
  69. // step 2: update the weight of networks
  70. // w_new = w_old + learningRate * differential (导数)
  71. // = w_old + learningRate * x (1{y_i=y} - p_yi_given_x)
  72. // = w_old + learningRate * x * (y - p_y_given_x)
  73. for(int i=0; i<n_out; i++)
  74. {
  75. dy[i] = y[i] - p_y_given_x[i];
  76. for(int j=0; j<n_in; j++)
  77. {
  78. W[i][j] += lr * dy[i] * x[j] / N;
  79. }
  80. b[i] += lr * dy[i] / N;
  81. }
  82. delete[] p_y_given_x;
  83. delete[] dy;
  84. }
  85.  
  86. void LogisticRegression::softmax (double *x)
  87. {
  88. double max = 0.0;
  89. double sum = 0.0;
  90.  
  91. // step1: get the max in the X vector
  92. for(int i=0; i<n_out; i++)
  93. if(max < x[i])
  94. max = x[i];
  95. // step 2: normalization and softmax
  96. // normalize -- 'x[i]-max', it's not necessary in traditional LR.
  97. // I wonder why it appears here?
  98. for(int i=0; i<n_out; i++)
  99. {
  100. x[i] = exp(x[i] - max);
  101. sum += x[i];
  102. }
  103. for(int i=0; i<n_out; i++)
  104. x[i] /= sum;
  105. }
  106.  
  107. void LogisticRegression::predict(
  108. int *x, // the input from input nodes in testing set
  109. double *y // the calculated softmax probability
  110. )
  111. {
  112. // get the softmax output value given the current networks
  113. for(int i=0; i<n_out; i++)
  114. {
  115. y[i] = 0;
  116. for(int j=0; j<n_in; j++)
  117. {
  118. y[i] += W[i][j] * x[j];
  119. }
  120. y[i] += b[i];
  121. }
  122.  
  123. softmax(y);
  124. }

【deep learning学习笔记】注释yusugomori的LR代码 --- LogisticRegression.cpp的更多相关文章

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

    继续看yusugomori的代码,看逻辑回归.在DBN(Deep Blief Network)中,下面几层是RBM,最上层就是LR了.关于回归.二类回归.以及逻辑回归,资料就是前面转的几篇.套路就是设 ...

  2. 【deep learning学习笔记】注释yusugomori的DA代码 --- dA.h

    DA就是“Denoising Autoencoders”的缩写.继续给yusugomori做注释,边注释边学习.看了一些DA的材料,基本上都在前面“转载”了.学习中间总有个疑问:DA和RBM到底啥区别 ...

  3. 【deep learning学习笔记】注释yusugomori的RBM代码 --- 头文件

    百度了半天yusugomori,也不知道他是谁.不过这位老兄写了deep learning的代码,包括RBM.逻辑回归.DBN.autoencoder等,实现语言包括c.c++.java.python ...

  4. [置顶] Deep Learning 学习笔记

    一.文章来由 好久没写原创博客了,一直处于学习新知识的阶段.来新加坡也有一个星期,搞定签证.入学等杂事之后,今天上午与导师确定了接下来的研究任务,我平时基本也是把博客当作联机版的云笔记~~如果有写的不 ...

  5. Deep Learning 学习笔记(8):自编码器( Autoencoders )

    之前的笔记,算不上是 Deep Learning, 只是为理解Deep Learning 而需要学习的基础知识, 从下面开始,我会把我学习UFDL的笔记写出来 #主要是给自己用的,所以其他人不一定看得 ...

  6. 【deep learning学习笔记】Recommending music on Spotify with deep learning

    主要内容: Spotify是个类似酷我音乐的音乐站点.做个性化音乐推荐和音乐消费.作者利用deep learning结合协同过滤来做音乐推荐. 详细内容: 1. 协同过滤 基本原理:某两个用户听的歌曲 ...

  7. Neural Networks and Deep Learning学习笔记ch1 - 神经网络

    近期開始看一些深度学习的资料.想学习一下深度学习的基础知识.找到了一个比較好的tutorial,Neural Networks and Deep Learning,认真看完了之后觉得收获还是非常多的. ...

  8. paper 149:Deep Learning 学习笔记(一)

     1. 直接上手篇 台湾李宏毅教授写的,<1天搞懂深度学习> slideshare的链接: http://www.slideshare.net/tw_dsconf/ss-62245351? ...

  9. Deep Learning 学习笔记——第9章

    总览: 本章所讲的知识点包括>>>> 1.描述卷积操作 2.解释使用卷积的原因 3.描述pooling操作 4.卷积在实践应用中的变化形式 5.卷积如何适应输入数据 6.CNN ...

随机推荐

  1. kingso_sort - Taocode

    kingso_sort - Taocode 如何编写新sort 由于排序逻辑多种多样,kingso的排序设计成是由一个个排序对象串起的排序链条组成.排序对象之间可以任意组合(只需要改配置文件),就可以 ...

  2. 使用线程新建WPF窗体(公用进度条窗体)

    使用线程新建窗体 项目中需要一个公用的进度条窗体.大家知道在wpf中,有两个线程,一个是UI线程,另一个是监听线程(一直监听用户的输入).如果我们后台有阻塞UI线程的计算存在,那么界面上的比如进度条什 ...

  3. Java NIO与IO

    当学习了Java NIO和IO的API后,一个问题立即涌入脑海: 我应该何时使用IO,何时使用NIO呢?在本文中,我会尽量清晰地解析Java NIO和IO的差异.它们的使用场景,以及它们怎样影响您的代 ...

  4. webform--常用的控件

    一.简单控件 1.Lable——标签:在网页中呈现出来的时候会变成span标签 属性:Text——标签上的文字  BackColor,ForeColor——背景色,前景色 Font——字体 Bold- ...

  5. java--equal&==

    [转自]http://blog.csdn.net/yiqunattack/article/details/5727143 [非常详细的介绍了string的用法http://blog.csdn.net/ ...

  6. java.util.concurrent-------TimeUnit

    java.util.concurrent并发库是JDK1.5新增的,其作者是Doug Lea ,此人是个BOSS级别的天才人物了.有了他提供的类库,使得我们对多线程并发.锁有了很大的帮助,减少了并发难 ...

  7. android在view.requestFocus(0)返回false的解决办法

    我们有时候想让listview的第一行自动获取到焦点,我们就会使用view.requestFocus(0)来操作,而有时候并不生效,debug后显示rerurn为false. 这是因为我们获取焦点太早 ...

  8. 物理Data Guard主备切换步骤

    物理Data Guard角色转换步骤   Step  1   验证主库是否能执行角色转换到备库(原主库执行) SQL> SELECT SWITCHOVER_STATUS FROM V$DATAB ...

  9. 强大的Http监控工具Fidder

    软件下载:http://fiddler2.com/get-fiddler 软件学习:http://www.cnblogs.com/TankXiao/archive/2012/02/06/2337728 ...

  10. USACO inflate

    全然背包,转化为0/1背包 dp[i, j] = max(dp[i-1, j], dp[i, j - minutes[i]] + points[i]) /* ID:kevin_s1 PROG:infl ...