C++ 人工神经网络 模拟 AND 操作符

//dnn dense nero network sample for AND operation
//2017/06/27

#ifndef DNN_EXAMPLE
#define DNN_EXAMPLE
#include <stdio.h>

float input[4][2] = {{0,0},{0,1},{1,0},{1,1}};
float output[4][1] = {{0},{0},{0},{1}};

float* weight;

//one layer to construct net
void load_weight(){

	weight = new float[2];
	weight[0] = 0.1;
	weight[1] = 0.2;
		
}

float threshhold(float x){
	if(x>=0.9)return 1.0;
	else return 0;
}//float

void show(){
	printf("%f  %f\n",weight[0],weight[1]);
	for(int i = 0;i<4;i++){
		float sum = 0.0;
		sum = input[i][0]*weight[0]+input[i][1]*weight[1];
		float value = threshhold(sum);
		printf("%f %f\t %f\n",input[i][0],input[i][1],value);
	}//for
}//void

void train(){
		
	for(int i = 0;i<4;i++){
		float error = 0.0;
		float sum = 0.0;
		sum = input[i][0]*weight[0]+input[i][1]*weight[1];
		
		float value = threshhold(sum);
		error = output[i][0] - value;
		weight[0] = weight[0] + error*0.01;
		weight[1] = weight[1] + error*0.01;

	}

}
void main(int argc, char **argv){
	load_weight();
	int iterations = 40;
	for(int i=0;i<iterations;i++)
		train();
	show();
	getchar();

}

#endif

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值