Skip to content

Instantly share code, notes, and snippets.

@danielpradilla
Created December 3, 2018 15:17
Show Gist options
  • Save danielpradilla/4774d2522a636e7cbbe2b2a392d18296 to your computer and use it in GitHub Desktop.
Save danielpradilla/4774d2522a636e7cbbe2b2a392d18296 to your computer and use it in GitHub Desktop.
if (!require(neuralnet)) {install.packages("neuralnet")}
library(neuralnet)
#nn for learning how to calculate a square root
set.seed(1)
train.x <- sample(1:100, 50, replace = T)
head(train.x)
train.y <- sqrt(train.x)
head(train.y)
train.xy <- as.data.frame(cbind(train.x, train.y))
head(train.xy)
net.sqrt <- neuralnet(train.y ~ train.x,
train.xy,
hidden=10,
threshold = 0.01 #1% error threshold
)
plot(net.sqrt)
test.x <- as.data.frame((1:10)^2) #new set of squared numbers
test.y <- compute(net.sqrt, test.x) #run through ANN
test.y
##results table
net.table <- cbind(test.x,
sqrt(test.x),
round(test.y$net.result, 2))
colnames(net.table) <- c("X", "Y-Exp", "Y-ANN")
net.table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment