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.

  • $\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 '19 at 9:23

You can get the answer here.

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)
| improve this answer | |

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.