我想手动计算神经网络模型的输出(预测),其中权重是使用R中的神经网络函数获得的。
我有以下代码,
library(neuralnet)
library(datasets.load)
nn<- neuralnet(formula = dist ~ mag , data = attenu, hidden = c(1,2),act.fct = "logistic" ,stepmax=1e6)
我得到以下结果:
> nn$weights
[[1]]
[[1]][[1]]
[,1]
[1,] -9.219761
[2,] 1.347328
[[1]][[2]]
[,1] [,2]
[1,] -396.1329 -330.0593
[2,] 982.5444 537.1123
[[1]][[3]]
[,1]
[1,] 23.00682
[2,] 57.26966
[3,] 78.06727
相关模型输出如下:
预测输出如下:
> nn$net.result
[[1]]
[,1]
[1,] 80.27648
[2,] 158.34375
[3,] 158.34375
[4,] 158.34375
[5,] 158.34375
[6,] 158.34375
[7,] 158.34375
[8,] 158.34375
[9,] 158.34375
[10,] 158.34375
[11,] 158.34375
[12,] 23.00682
[13,] 23.00682
[14,] 23.00682
[15,] 23.00682
[16,] 23.00682
[17,] 23.00682
[18,] 23.00682
[19,] 23.00682
[20,] 23.00682
[21,] 23.00682
[22,] 80.27647
[23,] 80.27647
[24,] 80.27647
[25,] 80.27647
[26,] 80.27647
[27,] 80.27647
[28,] 80.27647
[29,] 80.27647
[30,] 80.27647
[31,] 80.27647
[32,] 80.27647
[33,] 23.00682
[34,] 23.00682
[35,] 23.00682
[36,] 23.00682
[37,] 23.00682
[38,] 23.00682
[39,] 23.00682
[40,] 80.27647
[41,] 80.27647
[42,] 80.27647
[43,] 80.27647
[44,] 80.27647
[45,] 80.27647
[46,] 80.27647
[47,] 80.27647
[48,] 80.27647
[49,] 80.27647
[50,] 80.27647
[51,] 80.27647
[52,] 80.27647
[53,] 80.27647
[54,] 80.27647
[55,] 80.27647
[56,] 80.27647
[57,] 80.27647
[58,] 80.27647
[59,] 80.27647
[60,] 80.27647
[61,] 80.27647
[62,] 23.00682
[63,] 158.34375
[64,] 158.34375
[65,] 158.34375
[66,] 23.00682
[67,] 23.00682
[68,] 23.00682
[69,] 23.00682
[70,] 23.00682
[71,] 23.00682
[72,] 23.00682
[73,] 23.00682
[74,] 23.00682
[75,] 23.00682
[76,] 23.00682
[77,] 23.00682
[78,] 23.00682
[79,] 23.00682
[80,] 158.34375
[81,] 158.34375
[82,] 158.34375
[83,] 23.00682
[84,] 23.00682
[85,] 23.00682
[86,] 23.00682
[87,] 23.00682
[88,] 23.00682
[89,] 23.00682
[90,] 23.00682
[91,] 23.00682
[92,] 23.00682
[93,] 23.00682
[94,] 23.00682
[95,] 23.00682
[96,] 23.00682
[97,] 23.00682
[98,] 23.00682
[99,] 23.00682
[100,] 23.00682
[101,] 23.00682
[102,] 23.00682
[103,] 23.00682
[104,] 23.00682
[105,] 23.00682
[106,] 23.00682
[107,] 23.00682
[108,] 23.00682
[109,] 23.00682
[110,] 23.00682
[111,] 23.00682
[112,] 23.00682
[113,] 23.00682
[114,] 23.00682
[115,] 23.00682
[116,] 23.00682
[117,] 23.00682
[118,] 23.00682
[119,] 23.00682
[120,] 23.00682
[121,] 23.00682
[122,] 23.00682
[123,] 23.00682
[124,] 23.00682
[125,] 23.00682
[126,] 23.00682
[127,] 23.00682
[128,] 23.00682
[129,] 23.00682
[130,] 23.00682
[131,] 23.00682
[132,] 23.00682
[133,] 23.00682
[134,] 23.00682
[135,] 23.00682
[136,] 23.00682
[137,] 23.00682
[138,] 23.00682
[139,] 23.00682
[140,] 23.00682
[141,] 23.00682
[142,] 23.00682
[143,] 23.00682
[144,] 23.00682
[145,] 23.00682
[146,] 23.00682
[147,] 23.00682
[148,] 23.00682
[149,] 23.00682
[150,] 23.00682
[151,] 23.00682
[152,] 23.00682
[153,] 23.00682
[154,] 23.00682
[155,] 23.00682
[156,] 23.00682
[157,] 23.00682
[158,] 23.00682
[159,] 23.00682
[160,] 23.00682
[161,] 23.00682
[162,] 23.00682
[163,] 23.00682
[164,] 23.00682
[165,] 23.00682
[166,] 23.00682
[167,] 23.00682
[168,] 23.00682
[169,] 23.00682
[170,] 23.00682
[171,] 23.00682
[172,] 23.00682
[173,] 23.00682
[174,] 23.00682
[175,] 23.00682
[176,] 23.00682
[177,] 23.00682
[178,] 23.00682
[179,] 23.00682
[180,] 23.00682
[181,] 23.00682
[182,] 23.00682
但是,使用获得的权重并进行计算,我无法设法获得Neuronet(来自R)获得的输出。
根据结果,对于第一个元素(mag = 7),我将输出计算为:
x=7
u=-9.21976+1.34733*x
=-9.21976+1.34733*7
=0.21
a1=1/(1+EXP(-(396.1329+982.5444*u)))=0
a2=1/(1+EXP(-(330.059+537.112*u)))=0
output=23.0068+57.2697*a1+78.0673*a2
=23.0068
但是,神经网络函数的结果为80.276。
我对结果感到困惑。我无法获得相同的结果。
我将很高兴获得任何帮助。非常感谢。
答案 0 :(得分:1)
激活功能适用于每个单元,包括偏置。在您的示例中,输出80.27648的计算公式为:
act <- function(x) 1/(1 + exp(-x))
u <- act(-9.21976 + 1.34733*x)
a1 <- act(-396.1329 + 982.5444*u)
a2 <- act(-330.059 + 537.112*u)
output <- 23.0068 + 57.2697*a1 + 78.0673*a2