bflammers / ANN2

Neural Networks package for R with a fast C++ back-end and special support for unsupervised anomaly detection using autoencoders
GNU Lesser General Public License v3.0
13 stars 3 forks source link

Activations in each hidden layer #26

Closed XiangLiu-github closed 3 years ago

XiangLiu-github commented 3 years ago

Hi

Thanks for this package, I am trying to interpret the artificial neural network using a method called 'layer-wise relevance propagation', I knew python can do it, but in R I need to do the manual calculations that need the individual activations of each unit in each hidden layer. Do you have any idea how I can obtain the activations from the 'ann' object?

Thanks.

bflammers commented 3 years ago

Hi, I'm glad you like the package. You can use the method "partialForward" for this. See the below snippet

library(ANN2)

X <- iris[,1:4]
y <- iris$Species

nn <- neuralnetwork(X, y, hidden.layers = c(3, 4, 5))

row <- as.matrix(X[1,])

for (layer_idx in 1:nn$Rcpp_ANN$getMeta()$n_hidden) {
  act <- nn$Rcpp_ANN$partialForward(row, 0, layer_idx)
  print(act)
}
XiangLiu-github commented 3 years ago

Thanks! I noticed that there are other functions under nn$Rcpp_ANN, it might be helpful if you could provide more description about it.

XiangLiu-github commented 3 years ago

I have another question that can I obtain the 'presoftmax' results from a muticlassification model? The predict function only returns the probability that is scaled by softmax.

bflammers commented 3 years ago

There is no method for it. But you can obtain the outputs of the preceding layer, then multiply with the weights and add the biases. The weights and biases you obtain from the ANN object using NN$Rcpp_ANN$getParams()