Getting output of any hidden layer 
Author: katarina-cavar
Pull request: no
Labels: name: wontfix

# 🚀 Feature request

<!-- A clear and concise description of the feature proposal.
     Please provide a link to the paper and code in case they exist. -->

Is there a way to get output from any hidden layer of the model? 


## Motivation

<!-- Please outline the motivation for the proposal. Is your feature request
     related to a problem? e.g., I'm always frustrated when [...]. If this is related
     to another GitHub issue, please link here too. -->

I'm working on  the ALBERT transformer (more specifically `AlbertForSequenceClassification`, and when I print the model, this is the model's architecture: 

```py
AlbertForSequenceClassification(
  (albert): AlbertModel(
    (embeddings): AlbertEmbeddings(
      (word_embeddings): Embedding(30000, 128, padding_idx=0)
      (position_embeddings): Embedding(512, 128)
      (token_type_embeddings): Embedding(2, 128)
      (LayerNorm): LayerNorm((128,), eps=1e-12, elementwise_affine=True)
      (dropout): Dropout(p=0, inplace=False)
    )
    (encoder): AlbertTransformer(
      (embedding_hidden_mapping_in): Linear(in_features=128, out_features=768, bias=True)
      (albert_layer_groups): ModuleList(
        (0): AlbertLayerGroup(
          (albert_layers): ModuleList(
            (0): AlbertLayer(
              (full_layer_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
              (attention): AlbertAttention(
                (query): Linear(in_features=768, out_features=768, bias=True)
                (key): Linear(in_features=768, out_features=768, bias=True)
                (value): Linear(in_features=768, out_features=768, bias=True)
                (dropout): Dropout(p=0, inplace=False)
                (dense): Linear(in_features=768, out_features=768, bias=True)
                (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
              )
              (ffn): Linear(in_features=768, out_features=3072, bias=True)
              (ffn_output): Linear(in_features=3072, out_features=768, bias=True)
            )
          )
        )
      )
    )
    (pooler): Linear(in_features=768, out_features=768, bias=True)
    (pooler_activation): Tanh()
  )
  (dropout): Dropout(p=0, inplace=False)
  (classifier): Linear(in_features=768, out_features=2, bias=True)
)
```

I would like to get the outputs of middle / hidden layers, for example of layers `ffn_output` or `pooler`, but I'm not sure if that option exists. I've tried extracting `hidden_states` by setting `output_hidden_states` to True in AlbertConfig, but that doesn't bring me the result that I want. 

I believe tf-hub has a method  or attribute for this called by `model.get_layer(layer_name)`. 

Is there a way to extract hidden layers? 


Comments:
Yes, this will be quite hard and is not a feature that is implemented at the moment nor a feature that we plan on implementing soon. 

An easy way to get what you want though, will be to clone the repo and adapt the code. You can easily add the layer outputs (e.g. `ffn_output`) you want to the `return` functions of the different Albert layers (you will probalby have to return and retrieve it multiple times until you have it in the `AlbertForSequenceClassification.formard() `function

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.