R中从零开始的神经网络

时间:2018-11-23 05:41:08

标签: r machine-learning neural-network

我正在用R编程语言从头开始编写一个神经网络,并将其应用于虹膜数据集。 这里

d1f是输出层传播误差的矩阵

d2f是隐藏层传播误差的矩阵

a1,a2,a3,a4是隐藏层的激活输出

o1,o2,o3是输出层的激活输出

w1b和w2b分别是隐藏层和输出层的权重矩阵

j用于存储每次观察的成本

到目前为止,我只写了一次遍历整个数据集。因此,仅计算传播误差,但权重不会更新。 每当我运行这段代码时,我都会报错

a1(1-a1)错误:找不到函数“ a1”

终止后,我可以在R-Studio的右侧看到a1的值。 似乎d2f甚至没有计算出来。所以我在做什么错了。

这是代码

library(datasets)
library("phonTools", lib.loc="~/R/win-library/3.5")
iris.df =data(iris)
data(iris)
data("iris")
iris.df=iris

J = vector(mode = "numeric" , length = nrow(iris.df) )

attach(iris.df)

iris.df$y1 = 0

iris.df$y2 = 0

iris.df$y3 = 0

iris.df$y1[iris.df$Species =="setosa"] = 1

iris.df$y2[iris.df$Species =="versicolor"] = 1

iris.df$y3[iris.df$Species =="virginica"] = 1

w1b = matrix(rexp(20, rate=.1), ncol=5)  #Declaring weights between 1st             layer

w1b = wb1*0.24 - 0.12

q = c(1,1,1,1)

#w1b = cbind(q,w1b)

w2b = matrix(rexp(12, rate=.1), ncol=4)

w2b = w2b*0.24 - 0.12

q = c(1,1,1)

#w2b = cbind(q,w2b)

d1f = zeros(3,4)

d2f = zeros(4,5)

sigmoid = function(z)
{
  sig = 1/( 1 + exp(-z) )

  return(sig)  
}

for (i in 1:150) {


# Forward Propagation For First Layer

a1 = sigmoid( 1*w1b[1,1] + iris.df$Sepal.Length[i]*w1b[1,2] +     iris.df$Sepal.Width[i]*w1b[1,3] + iris.df$Petal.Length[i]*w1b[1,4] +     iris.df$Petal.Width[i]*w1b[1,5] )

a2 = sigmoid( 1*w1b[2,1] + iris.df$Sepal.Length[i]*w1b[2,2] + iris.df$Sepal.Width[i]*w1b[2,3] + iris.df$Petal.Length[i]*w1b[2,4] + iris.df$Petal.Width[i]*w1b[2,5] )

a3 = sigmoid( 1*w1b[3,1] + iris.df$Sepal.Length[i]*w1b[3,2] + iris.df$Sepal.Width[i]*w1b[3,3] + iris.df$Petal.Length[i]*w1b[3,4] + iris.df$Petal.Width[i]*w1b[3,5] )

a4 = sigmoid( 1*w1b[4,1] + iris.df$Sepal.Length[i]*w1b[4,2] + iris.df$Sepal.Width[i]*w1b[4,3] + iris.df$Petal.Length[i]*w1b[4,4] + iris.df$Petal.Width[i]*w1b[4,5] )

#Forward Propagation For Second Layer

o1 = sigmoid( 1*w2b[1,1] + a1*w2b[1,2] + a1*w2b[1,3] + a1*w2b[1,4] )

o2 = sigmoid( 1*w2b[2,1] + a2*w2b[2,2] + a2*w2b[2,3] + a2*w2b[2,4] )

o3 = sigmoid( 1*w2b[3,1] + a3*w2b[3,2] + a3*w2b[3,3] + a3*w2b[3,4] )




#Backward Propagation For First Layer

d1f[1,1] = d1f[1,1] + o1*(1-o1) *(o1-y1[i])  #For Baised Node

d1f[2,1] = d1f[2,1] + o2*(1-o2) *(o2-y2[i])  #For Baised Node

d1f[3,1] = d1f[3,1] + o3*(1-o3) *(o3-y3[i])  #For Baised Node



d1f[1,2] = d1f[1,2] + a1*o1*(1-o1)*(o1-y1[i])

d1f[2,2] = d1f[2,2] + a1*o2*(1-o2)*(o2-y2[i])

d1f[3,2] = d1f[3,2] + a1*o3*(1-o3)*(o3-y3[i])




d1f[1,3] = d1f[1,3] + a2*o1*(1-o1)*(o1-y1[i])

d1f[2,3] = d1f[2,3] + a2*o2*(1-o2)*(o2-y2[i])

d1f[3,3] = d1f[3,3] + a2*o3*(1-o3)*(o3-y3[i])




d1f[1,4] = d1f[1,4] + a3*o1*(1-o1)*(o1-y1[i])

d1f[2,4] = d1f[2,4] + a3*o2*(1-o2)*(o2-y2[i])

d1f[3,4] = d1f[3,4] + a3*o3*(1-o3)*(o3-y3[i])



#Backward Propagation For Second Layer

d2f[1,2] = d2f[1,2] + iris.df$Sepal.Length[i] * a1(1-a1) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )

d2f[1,3] = d2f[1,3] + iris.df$Sepal.Width[i] * a1(1-a1) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )

d2f[1,4] = d2f[1,4] + iris.df$Petal.Length[i] * a1(1-a1) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )

d2f[1,5] = d2f[1,5] + iris.df$Petal.Width[i] * a1(1-a1) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )


###########

d2f[1,1] = d2f[1,1] + a1(1-a1) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] ) #For Biased Node

d2f[2,1] = d2f[2,1] + a2(1-a2) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] ) #For Biased Node

d2f[3,1] = d2f[3,1] + a3(1-a3) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] ) #For Biased Node

d2f[4,1] = d2f[4,1] + a4(1-a4) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] ) #For Biased Node





d2f[2,2] = d2f[2,2] + iris.df$Sepal.Length[i] * a2(1-a2) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )

d2f[2,3] = d2f[2,3] + iris.df$Sepal.Width[i] * a2(1-a2) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )

d2f[2,4] = d2f[2,4] + iris.df$Petal.Length[i] * a2(1-a2) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )

d2f[2,5] = d2f[2,5] + iris.df$Petal.Width[i] * a2(1-a2) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )




d2f[3,2] = d2f[3,2] + iris.df$Sepal.Length[i] * a3(1-a3) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )

d2f[3,3] = d2f[3,3] + iris.df$Sepal.Width[i] * a3(1-a3) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )

d2f[3,4] = d2f[3,4] + iris.df$Petal.Length[i] * a3(1-a3) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )

d2f[3][5] = d2f[3,5] + iris.df$Petal.Width[i] * a3(1-a3) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )




d2f[4,2] = d2f[4,2] + iris.df$Sepal.Length[i] * a4(1-a4) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )

d2f[4][3] = d2f[4,3] + iris.df$Sepal.Width[i] * a4(1-a4) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )

d2f[4,4] = d2f[4,4] + iris.df$Petal.Length[i] * a4(1-a4) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )

d2f[4,5] = d2f[4,5] + iris.df$Petal.Width[i] * a4(1-a4) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )




cost = ( iris.df$y1[i] * log(o1) + (1-y1[i]) * log(1-o1) + iris.df$y2[i] * log(o2) + (1-y2[i]) * log(1-o2) + iris.df$y3[i] * log(o3) + (1-y3[i]) * log(1-o3) )/nrow(iris.df)

j[i] = cost
}

1 个答案:

答案 0 :(得分:0)

如果要获得a1和1-a1的乘积,则必须在两者之间明确地写上'*'。

d2f[1,2] = d2f[1,2] + iris.df$Sepal.Length[i] * a1*(1-a1) * ( (o1-y1[i])*o1*(1-o1)*w2b[1,2] + (o2-y2[i])*o2*(1-o2)*w2b[2,2] + (o3-y3[i])*o3*(1-o3)*w2b[3,2] )

如果在a1之后打开括号,r会认为您是在指代称为a1的函数,因为那是函数的语法。相同的问题是在您的代码的多个场合。您应始终声明要使用的算术运算符。