0
$\begingroup$

How can get the hidden layer outputs in a simple one-layer lstm?

cat("Building Model\n")                                                

model <- keras_model_sequential() %>% 
  layer_lstm(units = 64, dropout = 0.2, input_shape = c(seqlength, length(chars))) %>% 
  layer_dense(units = length(chars), activation = "softmax") %>% 


  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 <- model %>%
  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 <- ...  # 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)

Can anybody give a sample of this?

$\endgroup$

2 Answers 2

0
$\begingroup$

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 <- keras_model_sequential() %>% 
  layer_lstm(units = 64, 
             dropout = 0.2, 
             input_shape = c(seqlength, length(chars)),
             name = "lstm") %>% 
  ...

The rest is pretty straightforward:

lstm_layer_model <- keras_model(
  inputs = model$input, 
  outputs = get_layer(model, "lstm")$output
) 
lstm_output <- 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.

$\endgroup$
0
$\begingroup$

In TensorFlow for R there is a return_state parameter for the layer_lstm object (documentation). Setting it to True will make the object return the hidden states of the lstm.

I'm not well versed enough in R to give a bug-free code-snippet without testing, but I'm sure you can figure it out with the documentation.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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