How to get a output of a hidden layer of a single-layer LSTM How can get the hidden layer outputs in a simple one-layer lstm?

cat("Building Model\n")                                                

model &lt;- keras_model_sequential() %&gt;% 
  layer_lstm(units = 64, dropout = 0.2, input_shape = c(seqlength, length(chars))) %&gt;% 
  layer_dense(units = length(chars), activation = "softmax") %&gt;% 


  compile(loss = 'categorical_crossentropy',
          optimizer = optimizer_sgd(lr = 0.001,
                                    decay = 1e-6,
                                    momentum = 0.9,
                                    nesterov = T),
          metrics = c('accuracy'))

summary(model)


cat("Training \n")                                                

history &lt;- model %&gt;%
  fit(train,
      trainLabels,
      epochs = 6,
      batch_size = 16,
      validation_split = 0.2)


I found this guid but I don't know how to fit it to this simple model and what is data.

model &lt;- ...  # create the original model

layer_name &lt;- 'my_layer' intermediate_layer_model &lt;- keras_model(inputs = 
                  model$input,outputs = get_layer(model, layer_name)$output) 
intermediate_output &lt;- predict(intermediate_layer_model, data)


Can anybody give a sample of this?
You'll definitely want to name the layer you want to observe first (otherwise you'll be doing guesswork with the sequentially generated layer names):

model &lt;- keras_model_sequential() %&gt;% 
  layer_lstm(units = 64, 
             dropout = 0.2, 
             input_shape = c(seqlength, length(chars)),
             name = "lstm") %&gt;% 
  ...


The rest is pretty straightforward:

lstm_layer_model &lt;- keras_model(
  inputs = model$input, 
  outputs = get_layer(model, "lstm")$output
) 
lstm_output &lt;- predict(lstm_layer_model, new_data)


So if you want to know the LSTM neuron values for a given case, define that case's features as new_data and run the last line.