设为首页 加入收藏

TOP

RNN递归神经网络 C++实现
2016-12-06 20:24:34 】 浏览:313
Tags:RNN 神经网络 实现
//让程序自己学会是否需要进位,从而学会加法

#include "iostream"
#include "math.h"
#include "stdlib.h"
#include "time.h"
#include "vector"
#include "assert.h"
using namespace std;

#define innode  2       //输入结点数,将输入2个加数
#define hidenode  16    //隐藏结点数,存储“携带位”
#define outnode  1      //输出结点数,将输出一个预测数字
#define alpha  0.1      //学习速率
#define binary_dim 8    //二进制数的最大长度

#define randval(high) ( (double)rand() / RAND_MAX * high )
#define uniform_plus_minus_one ( (double)( 2.0 * rand() ) / ((double)RAND_MAX + 1.0) - 1.0 )  //均匀随机分布


int largest_number = ( pow(2, binary_dim) );  //跟二进制最大长度对应的可以表示的最大十进制数

//激活函数
double sigmoid(double x) 
{
    return 1.0 / (1.0 + exp(-x));
}

//激活函数的导数,y为激活函数值
double dsigmoid(double y)
{
    return y * (1 - y);  
}           
//将一个10进制整数转换为2进制数
void int2binary(int n, int *arr)
{
    int i = 0;
    while(n)
    {
        arr[i++] = n % 2;
        n /= 2;
    }
    while(i < binary_dim)
        arr[i++] = 0;
}

class RNN
{
public:
    RNN();
    virtual ~RNN();
    void train();

public:
    double w[innode][hidenode];        //连接输入层与隐藏层的权值矩阵
    double w1[hidenode][outnode];      //连接隐藏层与输出层的权值矩阵
    double wh[hidenode][hidenode];     //连接前一时刻的隐含层与现在时刻的隐含层的权值矩阵

    double *layer_0;       //layer 0 输出值,由输入向量直接设定
    //double *layer_1;     //layer 1 输出值
    double *layer_2;       //layer 2 输出值
};

void winit(double w[], int n) //权值初始化
{
    for(int i=0; i
  
    layer_1_vector;      //保存隐藏层
    vector
   
     layer_2_delta; //保存误差关于Layer 2 输出值的偏导 for(epoch=0; epoch<11000; epoch++) //训练次数 { double e = 0.0; //误差 for(i=0; i
    
     =0 ; p--) { layer_0[0] = a[p]; layer_0[1] = b[p]; layer_1 = layer_1_vector[p+1]; //当前隐藏层 double *layer_1_pre = layer_1_vector[p]; //前一个隐藏层 for(k=0; k
     
      =0; k--) cout << d[k]; cout << endl; cout << "true:" ; for(k=binary_dim-1; k>=0; k--) cout << c[k]; cout << endl; int out = 0; for(k=binary_dim-1; k>=0; k--) out += d[k] * pow(2, k); cout << a_int << " + " << b_int << " = " << out << endl << endl; } } } int main() { srand(time(NULL)); RNN rnn; rnn.train(); return 0; }
     
    
   
  

这里写图片描述

参考:
http://blog.csdn.net/zzukun/article/details/49968129
http://www.cnblogs.com/wb-DarkHorse/archive/2012/12/12/2815393.html

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇c++使用libiconv 下一篇c++使用libcurl

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目