You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repository implements deep learning transformer models in MATLAB.
5
3
6
4
## Requirements
7
-
* MATLAB R2020a or later
5
+
* MATLAB R2020a or later for GPT-2
6
+
* MATLAB R2021a or later for BERT and FinBERT
8
7
* Deep Learning Toolbox
8
+
* Text Analytics Toolbox for BERT and FinBERT
9
9
10
10
## Getting Started
11
11
Download or [clone](https://www.mathworks.com/help/matlab/matlab_prog/use-source-control-with-projects.html#mw_4cc18625-9e78-4586-9cc4-66e191ae1c2c) this repository to your machine and open it in MATLAB.
12
12
13
13
## Functions
14
-
### gpt2
14
+
### [bert](./bert.m)
15
+
`mdl = bert` loads a pretrained BERT transformer model and if necessary, downloads the model weights. The `mdl` struct has fields `Tokenizer` containing the BERT tokenizer and `Parameters` to be passed to `bert.model(x,mdl.Parameters)` where `x` can be `seq{1}` where `seq = mdl.Tokenizer.encode("hello world!");`
16
+
17
+
`mdl = bert('Model',modelName)` specifies an optional model. All models besides `"multilingual-cased"` are case-insensitve. The choices for `modelName` are:
18
+
*`"base"` (default) - A 12 layer model with hidden size 768.
19
+
*`"multilingual-cased"` - A 12 layer model with hidden size 768. The tokenizer is case-sensitive. This model was trained on multi-lingual data.
20
+
*`"medium"` - An 8 layer model with hidden size 512.
21
+
*`"small"` - A 4 layer model with hidden size 512.
22
+
*`"mini"` - A 4 layer model with hidden size 256.
23
+
*`"tiny"` - A 2 layer model with hidden size 128.
24
+
25
+
The model parameters match those found on the [original BERT repo](https://github.com/google-research/bert/). The BERT-Base parameters are from the original release, not the update from where the smaller models are sourced.
26
+
27
+
### [gpt2](./gpt2.m)
15
28
`mdl = gpt2` loads a pretrained GPT-2 transformer model and if necessary, downloads the model weights.
16
29
17
-
### generateSummary
30
+
### [generateSummary](./generateSummary.m)
18
31
`summary = generateSummary(mdl,text)` generates a summary of the string or `char` array `text` using the transformer model `mdl`. The output summary is a char array.
19
32
20
33
`summary = generateSummary(mdl,text,Name,Value)` specifies additional options using one or more name-value pairs.
@@ -24,15 +37,27 @@ Download or [clone](https://www.mathworks.com/help/matlab/matlab_prog/use-source
24
37
*`'Temperature'` - Temperature applied to the GPT-2 output probability distribution. The default is 1.
25
38
*`'StopCharacter'` - Character to indicate that the summary is complete. The default is `'.'`.
26
39
40
+
### [finbert](./finbert.m)
41
+
`mdl = finbert` loads a pretrained and fine-tuned BERT transformer model for classifying sentiment of financial text. The `mdl` struct is similar to the BERT model struct with additional weights for the sentiment classifier head. The sentiment analysis functionaliy is accessed through `[sentimentClass,sentimentScore] = finbert.sentimentModel(x,mdl.Parameters)` where `seq = mdl.Tokenizer.encode("The FTSE100 suffers dramatic losses on the back of the pandemic."); x = dlarray(seq{1});`.
42
+
43
+
`mdl = finbert('Model',modelName)` specifies an optional model from the choices:
44
+
*`"sentiment-model"` - The fine-tuned sentiment classifier model.
45
+
*`"language-model"` - The FinBERT pre-trained language model, which uses a BERT-Base architecture.
46
+
47
+
The parameters match those found on the [original FinBERT repo](https://github.com/ProsusAI/finBERT).
48
+
49
+
## Example: Classify Text Data Using BERT
50
+
The example [`ClassifyTextDataUsingBERT.m`](./ClassifyTextDataUsingBERT.m) is [this existing example](https://www.mathworks.com/help/textanalytics/ug/classify-text-data-using-deep-learning.html) reinterpreted to use BERT as an embedding.
51
+
27
52
## Example: Summarize Text Using GPT-2
28
-
The example `SummarizeTextUsingTransformersExample.m` shows how to summarize a piece of text using GPT-2.
53
+
The example [`SummarizeTextUsingTransformersExample.m`](./SummarizeTextUsingTransformersExample.m) shows how to summarize a piece of text using GPT-2.
29
54
30
55
Transformer networks such as GPT-2 can be used to summarize a piece of text. The trained GPT-2 transformer can generate text given an initial sequence of words as input. The model was trained on comments left on various web pages and internet forums.
31
56
32
57
Because lots of these comments themselves contain a summary indicated by the statement "TL;DR" (Too long, didn't read), you can use the transformer model to generate a summary by appending "TL;DR" to the input text. The `generateSummary` function takes the input text, automatically appends the string `"TL;DR"` and generates the summary.
33
58
34
59
### Load Transformer Model
35
-
Load the GPT-2 transformer model using the `gpt2` function.
60
+
Load the GPT-2 transformer model using the [`gpt2`](./gpt2.m) function.
36
61
37
62
```matlab:Code
38
63
mdl = gpt2;
@@ -46,7 +71,7 @@ inputText = help('eigs');
46
71
```
47
72
48
73
### Generate Summary
49
-
Summarize the text using the `generateSummary` function.
74
+
Summarize the text using the [`generateSummary`](./generateSummary.m) function.
' EIGS(AFUN,N,FLAG) returns a vector of AFUN's n smallest magnitude eigenvalues'
60
-
```
85
+
```
86
+
87
+
## Example: Classify Sentiment with FinBERT
88
+
The example [`SentimentAnalysisWithFinBERT.m`](./SentimentAnalysisWithFinBERT.m) uses the FinBERT sentiment analysis model to classify sentiment for a handful of example financial sentences.
89
+
90
+
## Example: Masked Token Prediction with BERT and FinBERT
91
+
The examples [`LanguageModelingWithBERT.m`](./LanguageModelingWithBERT.m) and [`LanguageModelingWithFinBERT.m`](./LanguageModelingWithFinBERT.m) demonstrate the language models predicting masked tokens.
0 commit comments