Path: blob/master/examples/nlp/ipynb/text_classification_with_transformer.ipynb
3508 views
Kernel: Python 3
Text classification with Transformer
Author: Apoorv Nandan
Date created: 2020/05/10
Last modified: 2024/01/18
Description: Implement a Transformer block as a Keras layer and use it for text classification.
Setup
In [ ]:
Implement a Transformer block as a layer
In [ ]:
Implement embedding layer
Two embedding layers, one for tokens, one for token index (positions).
In [ ]:
Download and prepare dataset
In [ ]:
Create classifier model using transformer layer
Transformer layer outputs one vector for each time step of our input sequence. Here, we take the mean across all time steps and use a feed forward network on top of it to classify text.
In [ ]:
Train and Evaluate
In [ ]: