set.seed(12345)
library(MASS)
## Warning: package 'MASS' was built under R version 4.2.3
library(neuralnet)
## Warning: package 'neuralnet' was built under R version 4.2.3
# Matriz de covarianzas
#-----------------------------------
cov <- matrix(c(0.05, 0, 0, 0.05), 2, 2)
cov
##      [,1] [,2]
## [1,] 0.05 0.00
## [2,] 0.00 0.05
# Datos
#-----------------------------
num.points <- 10000
first <- mvrnorm(num.points, c(0,0), cov)  # first row
second <- mvrnorm(num.points, c(0,1), cov)   # second row
third <- mvrnorm(num.points, c(1,0), cov)    # third row
fourth <- mvrnorm(num.points, c(1,1), cov)     # fourth row

all.points <- rbind(first, second, third, fourth)
labels <- rep(c(0,1,1,0), each = num.points)
xor.data <- as.data.frame(cbind(labels, all.points))
colnames(xor.data) <- c("label", "x", "y")
num.sample.rows <- 10
display.rows <- sample(1:nrow(xor.data), num.sample.rows)
xor.data[display.rows, ]
##       label           x           y
## 24809     1  0.91674574 -0.15010789
## 2121      0 -0.09790066  0.21107425
## 20288     1  1.34159930  0.13889533
## 23947     1  0.88285702 -0.05650109
## 24434     1  1.72165377  0.38207740
## 4422      0  0.27492968  0.35150220
## 24101     1  1.09267878 -0.04940034
## 36385     0  1.30911935  1.23678599
## 2650      0 -0.09085386 -0.18115259
## 4833      0 -0.02627418 -0.01039376
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
ggplot(xor.data, aes(x= x, y=y, color = factor(label))) + geom_point() +
  scale_color_manual(name ="Labels", values = c("blue", "red"),
                     labels = c("False", "True")) +
  ggtitle("XOR Function Data") + xlab("x") + ylab("Y")

xor.nnet <- neuralnet("label ~ x + y", data = xor.data, threshold = 1,
                      hidden = c(20),  # 1 hidden layer with 20 points
                      linear.output = F, # Classification
                      err.fct = "ce", # Error function
                      act.fct = "logistic") # Activation Function
plot(xor.nnet, rep = "best")

test.data <- data.frame(x = c(0,0,1,1),
                        y = c(0,1,0,1),
                        true.label = c(0,1,1,0))
prediction <- compute(xor.nnet, test.data[ , c("x", "y")])$net.result
cbind(test.data, prediction)
##   x y true.label   prediction
## 1 0 0          0 7.077155e-05
## 2 0 1          1 9.998881e-01
## 3 1 0          1 9.998496e-01
## 4 1 1          0 1.215426e-04
more.test.data <- data.frame(x=runif(10), y =runif(10))
more.predictions <- compute(xor.nnet, more.test.data)$net.result
cbind(more.test.data, more.predictions)
##             x         y more.predictions
## 1  0.02627987 0.5262379      0.587353358
## 2  0.54825321 0.8317945      0.287321963
## 3  0.22414624 0.2650587      0.006765542
## 4  0.89492819 0.1735192      0.997874167
## 5  0.57845540 0.1248127      0.842721206
## 6  0.67250371 0.8057823      0.018073839
## 7  0.38450936 0.5088162      0.559824941
## 8  0.13743792 0.7106330      0.983996225
## 9  0.78793421 0.7348427      0.013371082
## 10 0.50663285 0.8560593      0.485167194
num.interpolating.points <- 100

x.values <- seq(0, 1, len = num.interpolating.points)
y.values <- seq(0, 1, len = num.interpolating.points)

test.points <- as.data.frame(expand.grid(x.values, y.values))
colnames(test.points) <- c("x", "y")

predictions <- compute(xor.nnet, test.points)$net.result

ggplot() + geom_point(aes(x = test.points$x,y=test.points$y, color= predictions)) +
  scale_color_gradient("Prediction", low = "blue", high = "red") +
  ggtitle("Visualizing the Neural Network's Decision Pattern") + xlab("X") + ylab("Y")

library(devtools)
## Warning: package 'devtools' was built under R version 4.2.3
## Loading required package: usethis
## Warning: package 'usethis' was built under R version 4.2.3
source_url('https://gist.githubusercontent.com/fawda123/7471137/raw/466c1474d0a505ff044412703516c34f1a4684a5/nnet_plot_update.r')
## ℹ SHA-1 hash of file is "74c80bd5ddbc17ab3ae5ece9c0ed9beb612e87ef"
library(reshape)
## Warning: package 'reshape' was built under R version 4.2.3
plot.nnet(xor.nnet, pos.col = "blue", neg.col = "red")
## Loading required package: scales
## Warning: package 'scales' was built under R version 4.2.3

source_gist('6206737')
## ℹ Sourcing gist "6206737"
## ℹ SHA-1 hash of file is "7aa33496459a2fe0d3b359351c44fd423928bcfd"
gar.fun("y", xor.nnet)
## Loading required package: plyr
## Warning: package 'plyr' was built under R version 4.2.3
## 
## Attaching package: 'plyr'
## The following objects are masked from 'package:reshape':
## 
##     rename, round_any

xor.2.nnet <- neuralnet("label ~ x + y", data = xor.data, threshold = 1,
                        hidden = c(20, 20), # 2 hidden layers with 20 units each
                        linear.output = F, # Classification
                        err.fct ="ce", # Error function
                        act.fct = "logistic")  # Activation function
plot(xor.2.nnet, rep = "best")

simple.predictions.2 <- compute(xor.2.nnet, test.data[, c("x", "y")])$net.result
cbind(test.data, simple.predictions.2)
##   x y true.label simple.predictions.2
## 1 0 0          0         0.0001268721
## 2 0 1          1         0.9999201295
## 3 1 0          1         0.9995335686
## 4 1 1          0         0.0002611904
predictions.2 <- compute(xor.2.nnet, test.points)$net.result
ggplot() + geom_point(aes(x = test.points$x,y=test.points$y, color= predictions.2)) +
  scale_color_gradient("Prediction", low = "blue", high = "red") +
  ggtitle("Visualizing the Neural Network's Decision Pattern") + xlab("X") + ylab("Y")

ggplot() + geom_point(aes(x = test.points$x,y=test.points$y, color= predictions.2 - predictions)) +
  scale_color_gradient("Prediction", low = "blue", high = "red") +
  ggtitle("Visualizing the Neural Network's Decision Pattern") + xlab("X") + ylab("Y")

source_gist('6206737')
## ℹ Sourcing gist "6206737"
## ℹ SHA-1 hash of file is "7aa33496459a2fe0d3b359351c44fd423928bcfd"
gar.fun("y", xor.2.nnet)