单层感知器收敛

时间:2019-08-29 00:56:19

标签: r machine-learning perceptron

我正在尝试使用感知器对虹膜数据进行分类,无论花朵是否是刚毛,但不能正确收敛。


pla=function(x,y,w=NULL){

  n=ncol(x)
  w=as.vector(length(n))
  t=0
  iter=0
  e=los(x,y,w)

  while(e != 0 && iter<=100){
 print(paste("iteration: ",iter,"__________________________________________"))

    for (i in 1:nrow(x )){
      if (sign(t(w)%*%x[i,])!=y[i]){
        w=w+y[i]%*%x[i,]
        t=t+1
        print(paste( "Update: ", t, " | Weights (w): ", w[1], w[2], w[3], "Ls(w) : ", e))
        }
    }
    e=los(x,y,w)
    iter=iter+1
   abline(-w[1]/w[3],w[2]/w[3],col="green")

  }

  print(paste( "end with iter=",iter," Update: ", t, " | Weights (w): ", w[1], w[2], w[3], "Ls(w) : ", e))

  abline(-w[1]/w[3],w[2]/w[3],col="red")
  return(w)
}

开始时输的是1,但此后始终为0.3333333333。并且收到50条或更多警告:

In if (sign(t(w) %*% x[i, ]) != y[i]) { ... :
  the condition has length > 1 and only the first element will be used 

如果我有什么问题,但我不知道。感谢您帮我做功课,我对R不熟悉

0 个答案:

没有答案