• Stars
    star
    199
  • Rank 196,105 (Top 4 %)
  • Language
    MATLAB
  • License
    Other
  • Created over 4 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Deep Learning Transformer models in MATLAB

Transformer Models for MATLAB

CircleCI

This repository implements deep learning transformer models in MATLAB.

Translations

Requirements

BERT and FinBERT

  • MATLAB R2021a or later
  • Deep Learning Toolbox
  • Text Analytics Toolbox

GPT-2

  • MATLAB R2020a or later
  • Deep Learning Toolbox

Getting Started

Download or clone this repository to your machine and open it in MATLAB.

Functions

bert

mdl = bert loads a pretrained BERT transformer model and if necessary, downloads the model weights. The output mdl is structure with fields Tokenizer and Parameters that contain the BERT tokenizer and the model parameters, respectively.

mdl = bert("Model",modelName) specifies which BERT model variant to use:

  • "base" (default) - A 12 layer model with hidden size 768.
  • "multilingual-cased" - A 12 layer model with hidden size 768. The tokenizer is case-sensitive. This model was trained on multi-lingual data.
  • "medium" - An 8 layer model with hidden size 512.
  • "small" - A 4 layer model with hidden size 512.
  • "mini" - A 4 layer model with hidden size 256.
  • "tiny" - A 2 layer model with hidden size 128.
  • "japanese-base" - A 12 layer model with hidden size 768, pretrained on texts in the Japanese language.
  • "japanese-base-wwm" - A 12 layer model with hidden size 768, pretrained on texts in the Japanese language. Additionally, the model is trained with the whole word masking enabled for the masked language modeling (MLM) objective.

bert.model

Z = bert.model(X,parameters) performs inference with a BERT model on the input 1-by-numInputTokens-by-numObservations array of encoded tokens with the specified parameters. The output Z is an array of size (NumHeads*HeadSize)-by-numInputTokens-by-numObservations. The element Z(:,i,j) corresponds to the BERT embedding of input token X(1,i,j).

Z = bert.model(X,parameters,Name,Value) specifies additional options using one or more name-value pairs:

  • "PaddingCode" - Positive integer corresponding to the padding token. The default is 1.
  • "InputMask" - Mask indicating which elements to include for computation, specified as a logical array the same size as X or as an empty array. The mask must be false at indices positions corresponds to padding, and true elsewhere. If the mask is [], then the function determines padding according to the PaddingCode name-value pair. The default is [].
  • "DropoutProb" - Probability of dropout for the output activation. The default is 0.
  • "AttentionDropoutProb" - Probability of dropout used in the attention layer. The default is 0.
  • "Outputs" - Indices of the layers to return outputs from, specified as a vector of positive integers, or "last". If "Outputs" is "last", then the function returns outputs from the final encoder layer only. The default is "last".
  • "SeparatorCode" - Separator token specified as a positive integer. The default is 103.

finbert

mdl = finbert loads a pretrained BERT transformer model for sentiment analysis of financial text. The output mdl is structure with fields Tokenizer and Parameters that contain the BERT tokenizer and the model parameters, respectively.

mdl = finbert("Model",modelName) specifies which FinBERT model variant to use:

  • "sentiment-model" (default) - The fine-tuned sentiment classifier model.
  • "language-model" - The FinBERT pretrained language model, which uses a BERT-Base architecture.

finbert.sentimentModel

sentiment = finbert.sentimentModel(X,parameters) classifies the sentiment of the input 1-by-numInputTokens-by-numObservations array of encoded tokens with the specified parameters. The output sentiment is a categorical array with categories "positive", "neutral", or "negative".

[sentiment, scores] = finbert.sentimentModel(X,parameters) also returns the corresponding sentiment scores in the range [-1 1].

gpt2

mdl = gpt2 loads a pretrained GPT-2 transformer model and if necessary, downloads the model weights.

generateSummary

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.

summary = generateSummary(mdl,text,Name,Value) specifies additional options using one or more name-value pairs.

  • "MaxSummaryLength" - The maximum number of tokens in the generated summary. The default is 50.
  • "TopK" - The number of tokens to sample from when generating the summary. The default is 2.
  • "Temperature" - Temperature applied to the GPT-2 output probability distribution. The default is 1.
  • "StopCharacter" - Character to indicate that the summary is complete. The default is ".".

Example: Classify Text Data Using BERT

The simplest use of a pretrained BERT model is to use it as a feature extractor. In particular, you can use the BERT model to convert documents to feature vectors which you can then use as inputs to train a deep learning classification network.

The example ClassifyTextDataUsingBERT.m shows how to use a pretrained BERT model to classify failure events given a data set of factory reports. This example requires the factoryReports.csv data set from the Text Analytics Toolbox example Prepare Text Data for Analysis.

Example: Fine-Tune Pretrained BERT Model

To get the most out of a pretrained BERT model, you can retrain and fine tune the BERT parameters weights for your task.

The example FineTuneBERT.m shows how to fine-tune a pretrained BERT model to classify failure events given a data set of factory reports. This example requires the factoryReports.csv data set from the Text Analytics Toolbox example Prepare Text Data for Analysis.

The example FineTuneBERTJapanese.m shows the same workflow using a pretrained Japanese-BERT model. This example requires the factoryReportsJP.csv data set from the Text Analytics Toolbox example Analyze Japanese Text Data, available in R2023a or later.

Example: Analyze Sentiment with FinBERT

FinBERT is a sentiment analysis model trained on financial text data and fine-tuned for sentiment analysis.

The example SentimentAnalysisWithFinBERT.m shows how to classify the sentiment of financial news reports using a pretrained FinBERT model.

Example: Predict Masked Tokens Using BERT and FinBERT

BERT models are trained to perform various tasks. One of the tasks is known as masked language modeling which is the task of predicting tokens in text that have been replaced by a mask value.

The example PredictMaskedTokensUsingBERT.m shows how to predict masked tokens and calculate the token probabilities using a pretrained BERT model.

The example PredictMaskedTokensUsingFinBERT.m shows how to predict masked tokens for financial text using and calculate the token probabilities using a pretrained FinBERT model.

Example: Summarize Text Using GPT-2

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.

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.

The example SummarizeTextUsingTransformersExample.m shows how to summarize a piece of text using GPT-2.

More Repositories

1

MATLAB-Deep-Learning-Model-Hub

Discover pretrained models for deep learning in MATLAB
MATLAB
447
star
2

reinforcement_learning_financial_trading

MATLAB example on how to use Reinforcement Learning for developing a financial trading model
MATLAB
153
star
3

llms-with-matlab

Connect MATLAB to LLM APIs, including OpenAI® Chat Completions, Azure® OpenAI Services, and Ollama™
MATLAB
104
star
4

Fault-Detection-Using-Deep-Learning-Classification

This demo shows how to prepare, model, and deploy a deep learning LSTM based classification algorithm to identify the condition or output of a mechanical air compressor.
C++
75
star
5

Image-Classification-in-MATLAB-Using-TensorFlow

This example shows how to call a TensorFlow model from MATLAB using co-execution with Python.
MATLAB
49
star
6

Abnormal-EEG-Signal-Classification-Using-CNNs

This example shows how to build and train a convolutional neural network (CNN) from scratch to perform a classification task with an EEG dataset.
MATLAB
47
star
7

pretrained-yolo-v4

Object detection and transfer learning using pretrained YOLO v4 models in MATLAB.
MATLAB
46
star
8

rl-agent-based-traffic-control

Develop agent-based traffic management system by model-free reinforcement learning
MATLAB
45
star
9

Industrial-Machinery-Anomaly-Detection

Extract features and detect anomalies in industrial machinery vibration data using a biLSTM autoencoder
MATLAB
44
star
10

pose-estimation-3d-with-stereo-camera

This demo uses a deep neural network and two generic cameras to perform 3D pose estimation.
MATLAB
43
star
11

constrained-deep-learning

Constrained deep learning is an advanced approach to training deep neural networks by incorporating domain-specific constraints into the learning process.
MATLAB
36
star
12

Object-Detection-Using-YOLO-v2-Deep-Learning

MATLAB example of deep learning based object detection using Yolo v2 with ResNet50 Base Network
Cuda
35
star
13

playing-Pong-with-deep-reinforcement-learning

Train a reinforcement learning agent to play a variation of Pong®
MATLAB
34
star
14

Explore-Deep-Network-Explainability-Using-an-App

This repository provides an app for exploring the predictions of an image classification network using several deep learning visualization techniques. Using the app, you can: explore network predictions with occlusion sensitivity, Grad-CAM, and gradient attribution methods, investigate misclassifications using confusion and t-SNE plots, visualize layer activations, and many more techniques to help you understand and explain your deep network’s predictions.
MATLAB
34
star
15

Brain-MRI-Age-Classification-using-Deep-Learning

MATLAB example using deep learning to classify chronological age from brain MRI images
MATLAB
33
star
16

transformer-networks-for-time-series-prediction

Deep Learning in Quantitative Finance: Transformer Networks for Time Series Prediction
MATLAB
33
star
17

Inverse-Problems-using-Physics-Informed-Neural-Networks-PINNs

MATLAB
32
star
18

COVID19-Face-Mask-Detection-using-deep-learning

The entire workflow of developing deep learning model for detecting face mask.
MATLAB
30
star
19

mask-rcnn

Mask-RCNN training and prediction in MATLAB for Instance Segmentation
MATLAB
29
star
20

pix2pix

Image to Image Translation Using Generative Adversarial Networks
MATLAB
28
star
21

Human-Pose-Estimation-with-Deep-Learning

MATLAB example of deep learning based human pose estimation.
HTML
27
star
22

mtcnn-face-detection

Face detection and alignment using deep learning
HTML
26
star
23

resnet-50

Repo for ResNet-50
MATLAB
26
star
24

pretrained-spatial-CNN

Spatial-CNN for lane detection in MATLAB.
MATLAB
24
star
25

Pretrained-YOLOX-Network-For-Object-Detection

YOLOX inference in MATLAB for Object Detection with yolox_s, yolox_m & yolox_l networks
24
star
26

Pretrained-YOLOv8-Network-For-Object-Detection

YOLO v8 training and inference in MATLAB for Object Detection with yolov8n, yolov8s, yolov8m, yolov8l, yolov8x, networks
MATLAB
24
star
27

pretrained-deeplabv3plus

DeepLabv3+ inference and training in MATLAB for Semantic Segmentation
MATLAB
23
star
28

googlenet

Repo for GoogLeNet
MATLAB
22
star
29

deep-sudoku-solver

Find and solve sudoku puzzles in images using deep learning and computer vision
MATLAB
22
star
30

Lidar-object-detection-using-complex-yolov4

Object detection and transfer learning on point clouds using pretrained Complex-YOLOv4 models in MATLAB
MATLAB
21
star
31

2D-Differentiable-Rendering-Example

MATLAB
18
star
32

resnet-18

Repo for ResNet-18
MATLAB
17
star
33

Object-Detection-Using-Pretrained-YOLO-v2

YOLO v2 prediction and training in MATLAB for Object Detection with darknet19 & tinyYOLOv2 base networks
17
star
34

Image-domain-conversion-using-CycleGAN

MATLAB example of deep learning for image domain conversion
MATLAB
16
star
35

fourier-neural-operator

MATLAB
15
star
36

Hyperparameter-Tuning-in-MATLAB-using-Experiment-Manager-and-TensorFlow

This example shows how to use MATLAB to train a TensorFlow model and tune it's hyperparameters using co-execution with Python.
MATLAB
15
star
37

pretrained-efficientdet-d0

Object detection and transfer learning using pretrained EfficientDet-D0 model in MATLAB.
MATLAB
15
star
38

pretrained-salsanext

Semantic segmentation and transfer learning using pretrained SalsaNext model in MATLAB
MATLAB
14
star
39

Social-Distancing-Monitoring-System

AI-enabled social distancing detection tool that can detect if people are keeping a safe distance from each other by analyzing real-time video streams from the camera. - Idea Credit : Landing AI (https://landing.ai/)
MATLAB
14
star
40

mobilenet-v2

Repo for MobileNet-v2
MATLAB
13
star
41

Text-Detection-using-Deep-Learning

Text Detection using Pretrained CRAFT model in MATLAB
12
star
42

Image-Classification-in-MATLAB-Using-Converted-TensorFlow-Model

This repository shows how to import a pretrained TensorFlow model in the SavedModel format, and use the imported network to classify an image.
MATLAB
12
star
43

Automate-Labeling-in-Image-Labeler-using-a-Pretrained-TensorFlow-Object-Detector

This example shows how to automate object labeling in the Image Labeler app using a TensorFlow object detector model trained in Python.
MATLAB
12
star
44

resnet-101

Repo for ResNet-101 model
MATLAB
11
star
45

compare-PyTorch-models-from-MATLAB

Compare PyTorch models from MATLAB using co-execution
MATLAB
11
star
46

Quantized-Deep-Neural-Network-on-Jetson-AGX-Xavier

How to create, train and quantize network, then integrate it into pre/post image processing and generate CUDA C++ code for targeting Jetson AGX Xavier
MATLAB
11
star
47

Hamiltonian-Neural-Network

MATLAB
10
star
48

pillQC

A pill quality control dataset and associated anomaly detection example
MATLAB
10
star
49

CSINet-Channel-Compression-in-MATLAB-Using-Keras

This example shows how to co-execute MATLAB and Python to simulate the effect of channel estimate compression on precoding in a MIMO OFDM channel.
MATLAB
10
star
50

neuron-coverage-for-deep-learning

Compute the neuron coverage of a deep learning network in MATLAB.
MATLAB
9
star
51

quantization-aware-training

This example shows how to perform quantization aware training for transfer learned MobileNet-v2 network.
HTML
9
star
52

nerf

NeRF - Neural Radiance Fileds in MATLAB
MATLAB
9
star
53

stride-measurement-of-runner

This sample script is to measure the stride of a runner in video by using pretrained deep learning model and simple signal processing.
MATLAB
9
star
54

coexecution_speech_command

PyTorch and TensorFlow Co-Execution for Speech Command Recognition
Python
8
star
55

Classification-of-SARS-COVID-19-and-Other-Lung-Infections-from-Chest-X-Ray-Scan-Images-with-DenseNet

This example shows how to train a deep neural network to classify SARS COVID-19 and other lung infections using chest X-ray (CXR) images.
MATLAB
8
star
56

Physical-Concepts-Scinet

This repository provides implementation of SciNet network described in arXiv:1807.10300v3
MATLAB
7
star
57

deepspeech

This repo provides the pretrained DeepSpeech model in MATLAB. The model is compatible with transfer learning and C/C++ code generation.
MATLAB
7
star
58

Seven-Segment-Digit-Recognition

Seven Segment Digit Recognition in MATLAB
6
star
59

Deep_Learning_Poker_Player_using_MATLAB_and_Raspberry_Pi

This example shows how to use automatic code generation to deploy a deep learning model from MATLAB to Raspberry Pi
MATLAB
6
star
60

wav2vec-2.0

This repo provides the pretrained baseline 960 hours wav2vec 2.0 model in MATLAB.
6
star
61

convmixer-patches-are-all-you-need

ConvMixer - Patches Are All You Need?
MATLAB
6
star
62

artistic-style-transfer

Artistic fast style transfer with a webcam
MATLAB
5
star
63

Biomass-Estimation-Starter-Code

5
star
64

speech-based-information-retrieval

Retrieve answers from a knowledge base via speech recognition and information retrieval.
MATLAB
5
star
65

Graph-Neural-Network-for-Heat-Transfer-Problem

5
star
66

Use-a-Python-Speech-Command-Recognition-System-to-MATLAB

Use a Python speech command recognition system in MATLAB
MATLAB
5
star
67

satellite-image-semantic-segmentation

Semantic Segmentation of Large Satellite Images with blockedImage Datastores
MATLAB
5
star
68

compare-gradient-attribution-maps

Sanity checks for comparing gradient attribution maps
MATLAB
4
star
69

Pretrained-RESA-Network-For-Road-Boundary-Detection

Pretrained RESA model for road boundary detection in MATLAB
MATLAB
3
star
70

Physics-Informed-Neural-Networks-for-Heat-Transfer

3
star
71

Pretrained-Yolov9-Network-For-Object-Detection

YOLO v9 inference in MATLAB for Object Detection with yolov9t, yolov9s, yolov9m, yolov9c and yolov9e networks
MATLAB
3
star
72

pretrained-dlCHOMP

Pretrained networks for Deep-Learning-Based Covariant Hamiltonian Optimization for Motion Planning (DLCHOMP) of robotic manipulators for MATLAB®
MATLAB
2
star
73

Animated-Adaptive-Linear-Neuron

Animating how Adaline classification works by minimizing cost. Showing comparison of three kinds of gradient descent.
MATLAB
2
star
74

object-detection-and-explainability-for-tensorflow-and-pytorch-models

MATLAB
2
star
75

Convert-librosa-Audio-Feature-Extraction-To-MATLAB

Convert librosa Audio Feature Extraction To MATLAB
MATLAB
2
star
76

physics-informed-neural-networks-with-matlab-live-coding-session

Solve mass spring damper system with phyics-informed neural networks in MATLAB
MATLAB
2
star