BP神经网络(原理及代码实现)

前言

  本文主要内容是BP神经网络的Python实现(借助tensorflow库)和C++实现(未借助相关库)

Python实现BP神经网络

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'#暂时屏蔽警告,只显示error信息
from plugin import *
#构建数据
x_data = np.arange(-1,1,0.01)[: ,np.newaxis]
print("x_data",x_data)
noise = np.random.normal(0,0.05,x_data.shape)
y_data= 0.5*(np.sin(x_data)+noise)
print("y_data",y_data)
    

#占位符
xs = tf.compat.v1.placeholder(tf.float32,[None,1])
ys =tf.compat.v1.placeholder(tf.float32,[None,1])
h1 = add_layer(xs,1,20,tf.nn.relu)
prediction = add_layer(h1,20,1)
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices =[1] ) )
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
init = tf.global_variables_initializer()#初始化所有变量
sess = tf.compat.v1.Session()


if __name__ == "__main__":   
    sess.run(init)
    for i in range(10000):
        sess.run(train_step,feed_dict = {xs:x_data,ys:y_data})
        if i %100 ==0:
            print(sess.run(loss,feed_dict = {xs:x_data,ys:y_data}))



C++实现BP神经网络

BP(Back-Propagating)神经网络的特点是向前传送输入数据,向后反馈误差。理论上,一个三层(输入层,隐含层,输出层)的BP神经网络可以实现任意m维到任意n维的映射。
查看相应代码可访问我的github(https://github.com/YuruTu),博客主要发挥介绍性的工作,github上更新最新代码
https://github.com/YuruTu/BPNN/blob/master
头文件pch.h

#ifndef PCH_H
#define PCH_H
#include <cmath>
#include <iostream>
#include <vector>
#include <string.h>
#include <ctime>
#include <random>

#endif //PCH_H

主函数main.cpp


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员Albert

感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值