我正在尝试使用以下代码计算神经网络
#include <math.h>
double feat_hid[10];
double output[4];
const double w_hid[8][10] =
{
{0.0706307913427445,-0.332136075169963,1.86159334821608,1.55009706502897,1.55669357055137,-1.93539022266504,1.40698067828037,-0.196054693169832,-0.317952257631136,4.69951344792126},
{-0.860303689780744,-0.142965971622307,1.00938809624265,3.03147950247308,0.26570362705432,-0.63835074899079,-0.108472152405573,-0.465092677563643,-1.26640787415541,3.47897119519849},
{-0.272080445746987,0.388264886856488,1.93120693933139,2.63260374630115,-0.403646834510117,-1.69806363784962,-1.47457233648421,-1.50775491078107,-0.835555146571103,0.586017106027158},
{0.523268479966795,1.60528083325264,1.8945332979293,2.83572254589689,-1.68716858062427,-0.575246483351286,0.408066044886574,0.58303163597391,1.27390651063673,0.00318251321831592},
{0.363035740315566,-0.696513924175445,1.41563086072654,1.42184591641306,2.04584849389715,0.528929786530872,1.07312589674496,0.309695239536318,1.25983320795077,0.522937576790264},
{1.91097305183671,0.526449978690799,-0.00374666478341612,-0.783150846208907,1.01286141219052,-1.60758427665251,-0.040595792623655,-0.751738504583724,-0.930564482543811,-0.0312598878148829},
{-0.364318798326876,6.0862613777679,-1.97910374691563,-0.990892711032243,-4.01625281770135,-0.730723605319977,2.79951479383628,1.23870829231029,-1.96949038202552,-1.48674789633524},
{-2.51147997247322,-2.42006923324837,0.0825476300769622,0.780436871112016,-0.151841195266197,0.3603578344112,-1.10038496139283,1.01784327038377,-2.33380006889957,-0.276962857144392}
};
const double w_out[11][4] =
{
{-0.848153262003428,1.04206942072723,-0.56827515726385,0.368049168481758},
{2.65699122475564,-2.31195040158215,-0.324512289348535,-0.0248140339246439},
{0.819329194280546,0.039943623410223,-1.25202766689859,0.393685637397724},
{-1.28944659517956,2.66620424439295,-0.968011825082177,-0.404502491304304},
{-0.224432096177094,-0.239370531739619,1.95124987383465,-1.47584887746872},
{0.520195254302156,-0.0716719005040372,-0.795815587506882,0.344121703272989},
{-1.260329779349,2.04221311676928,0.251086051743563,-1.03192351329109},
{-0.680299559455324,0.452665873842875,-0.457043098095797,0.699247978937067},
{0.262467710721102,0.876119011755995,-1.84541649804428,0.689973715895836},
{0.602141912445942,-2.3913813206841,1.43782014708464,0.346066727829377},
{-0.0789595649460575,0.0759034703177117,-0.954465934396254,-1.06633481363805}
};
double accum;
double feature[7] = { 0.802748046, 0.79983412 ,-0.379610559, 0.02006534, 0.244444444 -1, 0.872340426};
void setup()
{
/* add setup code here */
Serial.begin(9600);
}
void loop()
{
for (int i = 0; i < 10; i++)
{
accum =0;
accum = w_hid[7][i];
for (int j = 0; j < 7; j++)
{
accum += feature[j] * w_hid[j][i];
}
//Serial.println(accum,4);
feat_hid[i] = tanh(accum);
}
for (int i = 0; i < 4; i++)
{
accum=0;
accum = w_hid[10][i];
for (int j = 0; j < 10; j++)
{
accum += feat_hid[j] * w_out[j][i];
}
output[i] = (accum);
}
Serial.println("wew");
Serial.println(output[0], 7);
Serial.println(output[1], 7);
Serial.println(output[2], 7);
Serial.println(output[3], 7);
for (size_t i = 0; i < 10; i++)
{
feat_hid[i] = 0;
}
for (size_t i = 0; i < 4; i++)
{
output[i] = 0;
}
}
输出结果为:
-2.0422704
-0.2195258
3.6133847
-1.3158617
但是当我尝试使用matlab重新计算时,输出结束(这是正确的):
0.856575444075245
-0.997328999300809
-0.911066976760467
-0.949747387652545
我的问题是,
arduino和浮动/双重计算有什么问题会导致计算错误吗?我记得,上面的草图和matlab代码之间的代码是类似的。
感谢任何反馈〜
答案 0 :(得分:0)
double feature [7] = {0.802748046,0.79983412,-0.379610559,0.02006534, 0.244444444 -1 ,0.872340426};
你有没有忘记这两个值之间的逗号?
(很抱歉发布此答案,仍然无法在此发表评论)