xiaoing.com©

开花即结果 提问即答案

继续用C语言写BP神经网络pid算法
原始问题:

继续用C语言写BP神经网络pid算法

include <stdio.h>

// 训练参数
float learningRate = 0.01; // 学习率
int maxIteration = 1000; // 最大迭代次数

// 权重和偏差初始化为0
float w1 = 0, w2 = 0, b = 0;

// 误差函数,输出误差的平方和
float error(float x[], float y[]){

float sumError = 0;    // 初始化误差为0 

for(int i=0; i<4; i++){     // 循环遍历求取总的误差 

    float output = w1 * x[i] + w2 * x[i] + b ;     // BP神经网络输出公式 

    sumError += (y[i] - output) * (y[i] - output);     // 求取总的误差(y-output)^2     

}     

return sumError / 4.0 ;       // 返回平均误差 

}

// 梯度下降法优化权重w1,w2,b,使得error最小化。 优化的相当于PID中的P部分。把权重看成Kp,error看成Ek-1。通过不断调整Kp使得Ek-1最小。
void gradientDescent(){

for(int j=0; j<maxIteration; j++){      // 最大迭代次数1000次,不断优化权重w1,w2,b以最小话error。        

    float x[]={3.3 , 4.4 , 5.5 , 6.71};      // 输入x[]                                                y[]={2.5 , 3.5 , 4.5 , 5.9};             // 输出y[]                                          y=wx+b           

    float dw1=0,dw2=0,db=0 ;       // 初始化权重对应的导数为零          

    for(int i=0; i<4 ; i++){          // 遍历计算所有样本对应的导数                                       dw1 += (w1*x[i]+w2*x[i]+b - y[i])*x[i];          dw2 += (w1*x[i]+w2*x[i]+b - y[i])*x[i];           db += (w1*x
继续用C语言写BP神经网络pid算法
https://www.xiaoing.com/zhidao/15605.html
AI / 模型Siliconflow
时间 / DATEApril 21, 2023, 1:17 PM
语言zh-CN
IP / 区域广东 广州