Chapter 2 感知机

chap2_1

chap2_1

chap2_2

chap2_2

mat <- matrix(c(3,3, 1,
                4,3, 1,
                1,1,-1), nrow = 3, byrow = TRUE)
mat
##      [,1] [,2] [,3]
## [1,]    3    3    1
## [2,]    4    3    1
## [3,]    1    1   -1
train_1 <- function(mat, n = 1) {  # n 为学习率
  
  nr <- dim(mat)[1]
  nc <- dim(mat)[2]
  # 初始化
  w0 <- matrix(0, nc - 1, 1)
  b0 <- 0
  
  result_all <- NULL
  while (is.null(result_all)) {

    print(list(w0 = w0, b0 = b0))
    
    for (i in seq_len(nr)) {
      
      cat("choose the ", i, "sample point\n")
      x <- mat[i, 1:(nc - 1)]
      yi <- mat[i, nc]
      
      result <- (x %*% w0 + b0) * yi
      
      if (result <= 0) {
        
        x <- matrix(mat[i, 1:(nc - 1)], nc - 1, 1)
        
        w0 <- w0 + n * yi * x
        b0 <- b0 + n * yi
        
        result_all <- NULL  #一旦遍历到某个样本点的result<= 0,则更新w0和b0,且result_all重为NULL
        break    #跳出for循环,回到while循环里
      } else {
        result_all <- c(result_all, result)
        cat("result_all = ", result_all, "\n")
      }
    }
  }
}

train_1(mat)
## $w0
##      [,1]
## [1,]    0
## [2,]    0
## 
## $b0
## [1] 0
## 
## choose the  1 sample point
## $w0
##      [,1]
## [1,]    3
## [2,]    3
## 
## $b0
## [1] 1
## 
## choose the  1 sample point
## result_all =  19 
## choose the  2 sample point
## result_all =  19 22 
## choose the  3 sample point
## $w0
##      [,1]
## [1,]    2
## [2,]    2
## 
## $b0
## [1] 0
## 
## choose the  1 sample point
## result_all =  12 
## choose the  2 sample point
## result_all =  12 14 
## choose the  3 sample point
## $w0
##      [,1]
## [1,]    1
## [2,]    1
## 
## $b0
## [1] -1
## 
## choose the  1 sample point
## result_all =  5 
## choose the  2 sample point
## result_all =  5 6 
## choose the  3 sample point
## $w0
##      [,1]
## [1,]    0
## [2,]    0
## 
## $b0
## [1] -2
## 
## choose the  1 sample point
## $w0
##      [,1]
## [1,]    3
## [2,]    3
## 
## $b0
## [1] -1
## 
## choose the  1 sample point
## result_all =  17 
## choose the  2 sample point
## result_all =  17 20 
## choose the  3 sample point
## $w0
##      [,1]
## [1,]    2
## [2,]    2
## 
## $b0
## [1] -2
## 
## choose the  1 sample point
## result_all =  10 
## choose the  2 sample point
## result_all =  10 12 
## choose the  3 sample point
## $w0
##      [,1]
## [1,]    1
## [2,]    1
## 
## $b0
## [1] -3
## 
## choose the  1 sample point
## result_all =  3 
## choose the  2 sample point
## result_all =  3 4 
## choose the  3 sample point
## result_all =  3 4 1
chap2_3

chap2_3

#计算gram矩阵
caculate_gram <- function(mat) {
  
  mat_g <- mat[, -ncol(mat)]    ##最后一列为标签值

  mat_gr <- matrix(0, nrow(mat_g), nrow(mat_g))
  for (i in 1:nrow(mat_g)) {
    for (j in 1:nrow(mat_g)) {
      mat_gr[i, j] <- mat_g[i, ] %*% mat_g[j, ]
    }
  }
  return(mat_gr)
}

mat <- matrix(c(3,3, 1,
                4,3, 1,
                1,1,-1), nrow = 3, byrow = TRUE)
#mat
gram_mat <- caculate_gram(mat)      #计算gram矩阵
gram_mat
##      [,1] [,2] [,3]
## [1,]   18   21    6
## [2,]   21   25    7
## [3,]    6    7    2
train_2 <- function(mat) {
  
  nr <- nrow(mat)
  nc <- ncol(mat)
  n <- 1
  alpha <- matrix(0, nr, 1)
  b0 <- 0
  
  result_all <- NULL
  while (is.null(result_all)) {
    print(list(alpha = alpha,b0 = b0))
    
    for (i in 1:nr) {
      
      cat("choose the ", i, "sample point\n")
      x <- mat[i, 1:(nc - 1)]
      yi <- mat[i, nc]
      
      result <- yi * (sum(alpha * mat[, nc] * gram_mat[i, ]) + b0)
      
      if (result <= 0) {
        x <- matrix(mat[i,1:(nc - 1)], nc - 1, 1)
        
        alpha[i,1] <- alpha[i,1] + n
        b0 <- b0 + n * yi
        
        result_all <- NULL  #一旦遍历到某个样本点的result<= 0,则更新w0和b0,且result_all重归NULL 
        break    #跳出for循环,回到while循环里
      } else {
        result_all <- c(result_all, result)
      }
    }
  }
  
  w <- 0
  for (k in 1:nr) {
    w0 <- alpha[k, 1] * mat[k, -nc] * mat[k, nc]
    w <- w + w0 
  }
  cat("w = ", w, "\n")
}


train_2(mat)
## $alpha
##      [,1]
## [1,]    0
## [2,]    0
## [3,]    0
## 
## $b0
## [1] 0
## 
## choose the  1 sample point
## $alpha
##      [,1]
## [1,]    1
## [2,]    0
## [3,]    0
## 
## $b0
## [1] 1
## 
## choose the  1 sample point
## choose the  2 sample point
## choose the  3 sample point
## $alpha
##      [,1]
## [1,]    1
## [2,]    0
## [3,]    1
## 
## $b0
## [1] 0
## 
## choose the  1 sample point
## choose the  2 sample point
## choose the  3 sample point
## $alpha
##      [,1]
## [1,]    1
## [2,]    0
## [3,]    2
## 
## $b0
## [1] -1
## 
## choose the  1 sample point
## choose the  2 sample point
## choose the  3 sample point
## $alpha
##      [,1]
## [1,]    1
## [2,]    0
## [3,]    3
## 
## $b0
## [1] -2
## 
## choose the  1 sample point
## $alpha
##      [,1]
## [1,]    2
## [2,]    0
## [3,]    3
## 
## $b0
## [1] -1
## 
## choose the  1 sample point
## choose the  2 sample point
## choose the  3 sample point
## $alpha
##      [,1]
## [1,]    2
## [2,]    0
## [3,]    4
## 
## $b0
## [1] -2
## 
## choose the  1 sample point
## choose the  2 sample point
## choose the  3 sample point
## $alpha
##      [,1]
## [1,]    2
## [2,]    0
## [3,]    5
## 
## $b0
## [1] -3
## 
## choose the  1 sample point
## choose the  2 sample point
## choose the  3 sample point
## w =  1 1
##
mat <- matrix(c(3,3, 1,
                4,3, 1,
                1,1,-1), nrow = 3, byrow = TRUE)
mat <- mat[c(2,1,3), ]
gram_mat <- caculate_gram(mat)
train_2(mat)
## $alpha
##      [,1]
## [1,]    0
## [2,]    0
## [3,]    0
## 
## $b0
## [1] 0
## 
## choose the  1 sample point
## $alpha
##      [,1]
## [1,]    1
## [2,]    0
## [3,]    0
## 
## $b0
## [1] 1
## 
## choose the  1 sample point
## choose the  2 sample point
## choose the  3 sample point
## $alpha
##      [,1]
## [1,]    1
## [2,]    0
## [3,]    1
## 
## $b0
## [1] 0
## 
## choose the  1 sample point
## choose the  2 sample point
## choose the  3 sample point
## $alpha
##      [,1]
## [1,]    1
## [2,]    0
## [3,]    2
## 
## $b0
## [1] -1
## 
## choose the  1 sample point
## choose the  2 sample point
## choose the  3 sample point
## $alpha
##      [,1]
## [1,]    1
## [2,]    0
## [3,]    3
## 
## $b0
## [1] -2
## 
## choose the  1 sample point
## choose the  2 sample point
## choose the  3 sample point
## w =  1 0
##
mat <- matrix(c(3,3, 1,
                4,3, 1,
                1,1,-1),nrow = 3,byrow = TRUE)
mat <- mat[c(3,1,2), ]
gram_mat <- caculate_gram(mat)
train_2(mat)
## $alpha
##      [,1]
## [1,]    0
## [2,]    0
## [3,]    0
## 
## $b0
## [1] 0
## 
## choose the  1 sample point
## $alpha
##      [,1]
## [1,]    1
## [2,]    0
## [3,]    0
## 
## $b0
## [1] -1
## 
## choose the  1 sample point
## choose the  2 sample point
## $alpha
##      [,1]
## [1,]    1
## [2,]    1
## [3,]    0
## 
## $b0
## [1] 0
## 
## choose the  1 sample point
## $alpha
##      [,1]
## [1,]    2
## [2,]    1
## [3,]    0
## 
## $b0
## [1] -1
## 
## choose the  1 sample point
## $alpha
##      [,1]
## [1,]    3
## [2,]    1
## [3,]    0
## 
## $b0
## [1] -2
## 
## choose the  1 sample point
## choose the  2 sample point
## $alpha
##      [,1]
## [1,]    3
## [2,]    2
## [3,]    0
## 
## $b0
## [1] -1
## 
## choose the  1 sample point
## $alpha
##      [,1]
## [1,]    4
## [2,]    2
## [3,]    0
## 
## $b0
## [1] -2
## 
## choose the  1 sample point
## $alpha
##      [,1]
## [1,]    5
## [2,]    2
## [3,]    0
## 
## $b0
## [1] -3
## 
## choose the  1 sample point
## choose the  2 sample point
## choose the  3 sample point
## w =  1 1