R, keras: How to get output of a hidden layer?

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.

• I'm curious as to why you need to do this? Are you looking to do something with the weights? – I_Play_With_Data Mar 6 '18 at 13:04
• @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. – David Arenburg Jan 24 '19 at 9:23

model <- ...  # create the original model
intermediate_layer_model <- keras_model(inputs = model$$input, outputs = get_layer(model, layer_name)$$output)