Description Usage Arguments Value See Also Examples
This function transforms a dataset into the activations of the neurons of the hidden layer of a neural network. This is done by training a neural network and then computing the activations of the neural network for each input pattern. It uses the nnet package under the hood.
1 |
keep_nn |
(default TRUE) Keep NN model. In most cases, the actual NN and associated results obtained by nnet is not required |
repetitions |
(default 1) Repeat nnet several times with different random seeds and select the best run using nnet's minimum value. This is useful because some random initialization of weights may lead to local minima. |
random_seed |
(default NULL) |
... |
See
|
list of:
trf: a function that transforms the input dataset using the weights of the hidden layer. This function has three arguments:
x: the input numeric matrix or data.frame to be transformed. Only numeric values.
use_sigmoid (default TRUE): Whether the sigmoid function should be used for the transformation. nnet uses the sigmoid in the hidden layer, but in some cases better results could be obtained with use_sigmoid=FALSE.
norm (default FALSE): If TRUE, this function's output is normalized (scaled) to range 0-1.
mod: values returned by nnet (this includes the neural network trained by nnet). If keep_nn=FALSE, then NULL is returned here.
matrix1: weights of hidden layer
matrix2: weights of output layer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | data("doughnutRandRotated")
rd <- doughnutRandRotated
# Make sure it is a classification problem
rd[,ncol(rd)] <- as.factor(rd[,ncol(rd)])
n <- nrow(rd)
# Split data into training and test
set.seed(0)
training_index <- sample(1:n, round(0.6*n))
train <- rd[training_index,]
test <- rd[-training_index,]
x_train <- train[,-ncol(train)]
y_train <- train[,ncol(train)]
x_test <- test[,-ncol(test)]
y_test <- test[,ncol(test)]
# Train nntrf transformation
set.seed(0)
nnpo <- nntrf(formula=V11~.,
data=train,
size=4, maxit=50, trace=TRUE)
# Apply nntrf transformation to the train and test splits
trf_x_train <- nnpo$trf(x=x_train, use_sigmoid=FALSE)
trf_x_test <- nnpo$trf(x=x_test, use_sigmoid=FALSE)
# Compute the success rate of KNN on the transformed feature space
outputs <- FNN::knn(trf_x_train, trf_x_test, y_train)
success <- mean(outputs == y_test)
print(success)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.