8
$\begingroup$

I am using package Keras in R to do a neural network. How may I extract the output from a hidden layer? I found an example in python, but it is just I have no idea how to do that in R.

$\endgroup$
  • $\begingroup$ I'm curious as to why you need to do this? Are you looking to do something with the weights? $\endgroup$ – I_Play_With_Data Mar 6 '18 at 13:04
  • $\begingroup$ @I_Play_With_Data This is particularly useful when you train an auto-encoder and you want to extract the last layer of the encoder for dimensionality reduction. $\endgroup$ – David Arenburg Jan 24 at 9:23
5
$\begingroup$

You can get the answer at https://keras.rstudio.com/articles/faq.html

Here it is:

model <- ...  # create the original model
layer_name <- 'my_layer'
intermediate_layer_model <- keras_model(inputs = model$input,
                                    outputs = get_layer(model, layer_name)$output)
intermediate_output <- predict(intermediate_layer_model, data)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.