library(RSNNS) # function compute accuracy # input: # model: list(weights, bias) # x: a n*d matrix in which each row is a datum point # y: a n*1 matrix in which each row is the target of x[i,] # output: classification error compute.accuracy <- function(model, x, y) { n.cases <- dim(x)[1] pred <- (predict(model, x) > 0.5) * 1. acc <- sum((y == pred)*1.)/n.cases return(acc) } ############################################################# maxit = 100 learn.rate = 0.1 size = c(5) x <- t(matrix(c( 0,0, 0,1, 1,0, 1,1), 2,4)) y <- matrix(c( 0, 1, 1, 1), 4, 1) colnames(x) <- c("x1", "x2") model <- mlp(x, y, size=size, maxit = maxit, learnFuncParams = learn.rate) # report result plotIterativeError(model) cat("accuracy: ", compute.accuracy(model, x, y), "\n")