Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- updateWeights <- function(x, w, a, target){
- act <- x %*% w
- out <- ifelse(act > 0.5, 1, 0)
- e <- target - out
- update <- (t(t(w) + as.numeric(e*a)) * x)
- w <- ifelse(update == 0, w, update)
- return(w)
- }
- trainANN <- function(train, w, a, train_classes){
- ind <- sample(1:nrow(train), nrow(train), FALSE)
- for(i in ind){
- w <- updateWeights(train[i , ], w, a, train_classes[i, ])
- }
- return(w)
- }
- testANN <- function(test, w, test_classes){
- r <- matrix(0, nrow(test))
- for(i in 1:nrow(test)){
- x <- test[i, ]
- target <- test_classes[i, ]
- out <- ifelse(x %*% w > 0.5, 1, 0)
- r[i] <- ifelse(all(target == out), 1, 0)
- }
- return(sum(r) / length(r))
- }
- d <- matrix(c(runif(100, 0, 0.4), runif(100, 0.5, 1)), 100, 2, TRUE)
- plot(d, col = rep(c("blue", "green"), each = 50))
- classes <- matrix(rep(c(1,0,0,1), each = 50), 100, 2)
- ind <- sample(1:nrow(d), nrow(d)*.9, FALSE)
- train <- d[ind, ]
- test <- d[-(ind), ]
- train_classes <- classes[ind, ]
- test_classes <- classes[-(ind), ]
- nEpochs <- 500
- results <- rep(0, nEpochs)
- a <- 0.2
- w <- matrix(runif(ncol(train)*ncol(classes), 0, 1), ncol(train), ncol(classes))
- for(i in 1:nEpochs){
- w <- trainANN(train, w, a, train_classes)
- results[i] <- testANN(test, w, test_classes)
- }
- plot(results)
- lines(results)
- mean(results)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement