【问题标题】:Rosenblatts perceptron implementation in R ProgrammingR 编程中的 Rosenblatts 感知器实现
【发布时间】:2019-02-22 11:25:52
【问题描述】:

ive train_data 要使用感知器进行分类 .. 给出 nan 的 感知器分类错误

train_data =data.frame(x_1=c(0,1,4,5,6,8,2,3,5,9,12,14,15,16,17,20,19,16,18,19,0,2,3,4,5,6),
                   
x_2=c(1,4,13,2,14,15,3,7,8,11,12,20,16,14,5,6,8,9,12,10,11,16,17,20,20,19),
                   
y_d=c(1,1,0,1,0,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0))


p=ggplot(train_data,aes(train_data$x_1,train_data$x_2))
p
train_data$b=1
head(train_data)
x=train_data[,c(1,2,4)]
head(x)

y= train_data[,3]
head(y)
perceptron = function(x,y,rate,epoch){
  weight_vector= rep(0,ncol(x))
  error=rep(0,epoch)
  for (i in 1:epoch) {
    for (j in 1:nrow(x)) {
            output= sum(weight_vector*x[j,])
            if(output<0){
                    ypred=-1
                    p+geom_point(aes(x[j,1],x[j,2],col="red"))
            }else{
                    ypred=1
                    p+geom_point(aes(x[j,1],x[j,2],col="blue"))
    }
  weightdiff=rate*(y[j]-ypred)*as.numeric(x[j,])
  weight_vector=weight_vector+weightdiff
  if((y[j]-ypred)!=0.0){
    error[i]= error[i]+1
  }
  return(error)
  
  }

 }

}

函数调用

感知器(x,y,0.01,100)

感知器函数将所有错误生成为零,除了第一个错误

 [1] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
 0 0 0 0 0 0 0 0
[45] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0
[89] 0 0 0 0 0 0 0 0 0 0 0 0

我期待输出图如下

这样我就可以画一条线来分类两个类

【问题讨论】:

    标签: r neural-network perceptron


    【解决方案1】:

    请看下面更正的算法,因为算法中有一些错误(激活函数计算 [我采用 Heaviside 激活函数],维数)。我在代码右侧的#m 中标记了它们。由于ggplot2 内部机制,我在算法计算中将ggplot2 替换为base::plot 函数。请看以下结果:

        train_data <- structure(list(x_1 = c(0, 1, 4, 5, 6, 8, 2, 3, 5, 9, 12, 14, 
        15, 16, 17, 20, 19, 16, 18, 19, 0, 2, 3, 4, 5, 6), x_2 = c(1, 
        4, 13, 2, 14, 15, 3, 7, 8, 11, 12, 20, 16, 14, 5, 6, 8, 9, 12, 
        10, 11, 16, 17, 20, 20, 19), y_d = c(1, 1, 0, 1, 0, 0, 1, 1, 
        1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0)), class = "data.frame", 
        row.names = c(NA, -26L))
    
        plot(train_data$x_1, train_data$x_2, type = "n")
    
    perceptron <- function(x, y, rate, epoch){
      weight_vector <- rep(0, ncol(x) + 1) #m
      error <- rep(0, epoch)
      for (i in 1:epoch) {
        for (j in 1:length(y)) { #m
          output <- sum(weight_vector[2:length(weight_vector)] * 
                          as.numeric(x[j, ])) + weight_vector[1] #m
    
          if (output < 0) {
            ypred <- -1
            points(x[j, 1], x[j, 2], col = "red", pch = 19, cex = 2)
          } else {
            ypred <- 1
            points(x[j, 1], x[j, 2], col = "blue", pch = 19, cex = 2)
          }
          weightdiff <- rate * (y[j] - ypred) * c(1, as.numeric(x[j, ])) #m
    
          weight_vector <- weight_vector + weightdiff
          if((y[j] - ypred) != 0.0){
            error[i] <- error[i] + 1
          }
        }
      }
      return(error)
    }
    
    x <- train_data[, c(1,2)]
    y <- train_data[, 3]
    
    
    perceptron(x, y, 0.01, 100)
    

    输出:

      [1] 13 13 13 15 10 11 11 11 14 14 11 11 10 12 10 11 11 12 13 11 12 10 11 11 12 13 11 12 13 11 11 12 10 12 10
     [36] 12 10 12 10 10 10 11 12 11 12 11 12 11 12 11 12 11 12 11 12 12 11 11 12 12 11 11 12 12 11 11 10 11 12 11
     [71] 10 12 12 11 11 10 11 12 11 12 10 12 12 10 12 11 10 11 10 12 10 12 10 10 11 10 12 10 12 10
    

    图形:

    【讨论】:

    • 感谢您的回复..但它仍然被错误分类......经过 10000 次迭代后,错误也在改变,它没有变得恒定。
    • 问题在于您的输入数据本身。请参阅iris 数据集的示例。算法收敛。 rpubs.com/FaiHas/197581.
    猜你喜欢
    • 2017-12-28
    • 2011-06-07
    • 2015-12-05
    • 2018-04-23
    • 2015-04-25
    • 1970-01-01
    • 2020-08-01
    • 2023-03-03
    • 1970-01-01
    相关资源
    最近更新 更多