diff --git a/DESCRIPTION b/DESCRIPTION index 21f6012..a2ae5ae 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,7 +21,8 @@ Imports: stats, Rmpfr, nnet, - pROC + pROC, + bench Suggests: knitr, rmarkdown, diff --git a/NAMESPACE b/NAMESPACE index cf2edb5..973e0a0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,6 +11,7 @@ export(feed_forward) export(map) export(sigmoid) importFrom(Rmpfr,pmax) +importFrom(bench,mark) importFrom(nnet,nnet) importFrom(pROC,roc) importFrom(stats,predict) diff --git a/R/BackPropNN.R b/R/BackPropNN.R index 539ecb6..878843f 100644 --- a/R/BackPropNN.R +++ b/R/BackPropNN.R @@ -10,11 +10,22 @@ NULL #' @importFrom nnet nnet NULL +#' @importFrom bench mark +NULL + #' BackPropNN #' #' A collection of functions #' -#' @description We add stuff up... +#' @description This R-package will contain functions that will implement NN +#' training (via back-propagation) from scratch (using basic R packages). +#' The function will take data (X and Y) as an input and will ask user to specify +#' no. of input nodes, no. of hidden nodes, no. of output nodes, learning rate, and an +#' activation function to be used. The function will produce the NN model in terms +#' of matrices containing weights for each nodes. The user will be able to choose +#' activation function between ReLU and Sigmoid. The user will be able to compare +#' the performance of this R-package in comparison to existing R NN packages in +#' terms of accuracy and computational time. #' #' @docType package #' @name BackPropNN diff --git a/R/back_propagation_training.R b/R/back_propagation_training.R index f550c2c..e56e55c 100644 --- a/R/back_propagation_training.R +++ b/R/back_propagation_training.R @@ -13,21 +13,17 @@ #' @return A list of class \code{BackPropNN_back_propagation_training}: #' @examples #' set.seed(100) -#' i <- 2 # number of input nodes -#' h <- 2 # number of hidden nodes -#' o <- 1 # number of output nodes -#' learning_rate <- 0.1 # The learning rate of the algorithm -#' activation_func <- "sigmoid" # the activation function #' data <- data.frame(X1 = 1:100, X2 = 2:101, Y = sample(c(0,1), 100, replace=TRUE)) -#' nn_model <- back_propagation_training(i, h, o, learning_rate, activation_func, data) +#' nn_model <- back_propagation_training(i=2, h=2, o=1, learning_rate=0.01, +#' activation_func="sigmoid", data=data) #' #' @export back_propagation_training <- function(i, h, o, learning_rate, activation_func, data){ - W_IH = matrix(0.01,h,i) - W_HO = matrix(0.01,o,h) - B_H = matrix(0.01,h,1) - B_O = matrix(0.01,o,1) + W_IH = matrix(0.01,nrow=h,ncol=i) + W_HO = matrix(0.01,nrow=o,ncol=h) + B_H = matrix(0.01,nrow=h,ncol=1) + B_O = matrix(0.01,nrow=o,ncol=1) X = as.matrix(data[1:ncol(data)-1]) Y = as.matrix(data[,ncol(data)]) @@ -86,7 +82,7 @@ plot.BackPropNN_back_propagation_training <- function(x) { Y = as.matrix(data[,ncol(data)]) nn_R <- nnet::nnet(X,Y,size=x$num_nodes[2], trace=FALSE) - nn_R_pred <- as.numeric(stats::predict(nn_R,X)) + nn_R_pred <- as.numeric(stats::predict(nn_R,X, type="raw")) pROC::roc(data[,ncol(data)],feed_forward(data,x)$pred, plot=TRUE, print.auc=TRUE, main="ROC curve by BackPropNN") @@ -114,7 +110,7 @@ print.BackPropNN_back_propagation_training <- function(x) { Y = as.matrix(data[,ncol(data)]) nn_R <- nnet::nnet(X,Y,size=x$num_nodes[2], trace=FALSE) - nn_R_pred <- as.numeric(stats::predict(nn_R,X)) + nn_R_pred <- as.numeric(stats::predict(nn_R,X, type="raw")) nn_R_mse <- mean((Y - nn_R_pred)^2) my_nn_mse <- mean((Y - feed_forward(x$input_data,x)$pred)^2) diff --git a/main.R b/main.R deleted file mode 100644 index e69de29..0000000 diff --git a/man/BackPropNN.Rd b/man/BackPropNN.Rd index 8438b8a..029c3ed 100644 --- a/man/BackPropNN.Rd +++ b/man/BackPropNN.Rd @@ -5,7 +5,15 @@ \alias{BackPropNN} \title{BackPropNN} \description{ -We add stuff up... +This R-package will contain functions that will implement NN +training (via back-propagation) from scratch (using basic R packages). +The function will take data (X and Y) as an input and will ask user to specify +no. of input nodes, no. of hidden nodes, no. of output nodes, learning rate, and an +activation function to be used. The function will produce the NN model in terms +of matrices containing weights for each nodes. The user will be able to choose +activation function between ReLU and Sigmoid. The user will be able to compare +the performance of this R-package in comparison to existing R NN packages in +terms of accuracy and computational time. } \details{ A collection of functions diff --git a/man/back_propagation_training.Rd b/man/back_propagation_training.Rd index 63e18f3..478b3c9 100644 --- a/man/back_propagation_training.Rd +++ b/man/back_propagation_training.Rd @@ -41,12 +41,8 @@ Computes the weight and bias matrices for the nodes of the neural network with \ } \examples{ set.seed(100) -i <- 2 # number of input nodes -h <- 2 # number of hidden nodes -o <- 1 # number of output nodes -learning_rate <- 0.1 # The learning rate of the algorithm -activation_func <- "sigmoid" # the activation function data <- data.frame(X1 = 1:100, X2 = 2:101, Y = sample(c(0,1), 100, replace=TRUE)) -nn_model <- back_propagation_training(i, h, o, learning_rate, activation_func, data) +nn_model <- back_propagation_training(i=2, h=2, o=1, learning_rate=0.01, +activation_func="sigmoid", data=data) } diff --git a/vignettes/BackPropNN.Rmd b/vignettes/BackPropNN.Rmd index d810c2e..2bc81b2 100644 --- a/vignettes/BackPropNN.Rmd +++ b/vignettes/BackPropNN.Rmd @@ -18,13 +18,13 @@ knitr::opts_chunk$set( library(BackPropNN) ``` -# Simulated data - OR data. +# Simulated data - AND data. ```{r} -num_obs <- 10000 # Number of observations +num_obs <- 100000 # Number of observations X1 <- sample(c(0,1),num_obs, replace = TRUE) X2 <- sample(c(0,1),num_obs, replace = TRUE) -Y <- ifelse(X1==0 & X2==0, 0, 1) +Y <- ifelse(X1==1 & X2==1, 1, 0) data <- data.frame(X1,X2,Y) ```