#
Wolfram Neural Net Repository

Immediate Computable Access to Neural Net Models

Generate text in English and represent text as a sequence of vectors

Released in 2018, this Generative Pre-Training Transformer (GPT) model is pre-trained in an unsupervised fashion on a large corpus of English text. This model can be further fine-tuned with additional output layers to create highly accurate NLP models for a wide range of tasks. It uses bi-directional causal self-attention, often referred to as a transformer decoder.

Number of models: 2

- BookCorpus, a dataset consisting of 11,038 unpublished books from 16 different genres.

The model fine-tuned on various datasets obtains the following accuracy on various natural language inference tasks: 82.1%, 81.4%, 89.9%, 88.3%, 88.1% and 56% accuracy on MNLI-m, MNLI-mm, SNLI, SciTail, QNLI, and RTE datasets respectively.

For question answering and commonsense reasoning, the fine-tuned model obtains the following accuracies: 86.5%, 62.9%, 57.4%, and 59.0% accuracy on Story Cloze, RACE-m, RACE-h, and RACE datasets respectively

Get the pre-trained net:

In[1]:= |

Out[1]= |

This model consists of a family of individual nets, each identified by a specific parameter combination. Inspect the available parameters:

In[2]:= |

Out[2]= |

Pick a non-default net by specifying the parameters:

In[3]:= |

Out[3]= |

Pick a non-default uninitialized net:

In[4]:= |

Out[4]= |

Given a piece of text, the GPT net produces a sequence of feature vectors of size 768, which correspond to the sequence of input words or subwords:

In[5]:= |

Out[5]= |

Obtain dimensions of the embeddings:

In[6]:= |

Out[6]= |

Visualize the embeddings:

In[7]:= |

Out[7]= |

The input string is first normalized and then tokenized, or split into words or subwords. This two-step process is accomplished using the NetEncoder "Function":

In[8]:= |

Out[9]= |

The tokenization step is performed using the NetEncoder "BPESubwordTokens" and can be extracted using the following steps:

In[10]:= |

The encoder produces an integer index for each subword token that corresponds to the position in the vocabulary:

In[11]:= |

Out[11]= |

Each subword token is also assigned a positional index:

In[12]:= |

Out[12]= |

A lookup is done to map these indices to numeric vectors of size 768:

In[13]:= |

Out[14]= |

For each subword token, these two embeddings are combined by summing elements with ThreadingLayer:

In[15]:= |

Out[15]= |

The transformer architecture then processes the vectors using 12 structurally identical self-attention blocks stacked in a chain:

In[16]:= |

Out[16]= |

The key part of these blocks is the attention module consisting of 12 parallel self-attention transformations, also called “attention heads”:

In[17]:= |

Out[17]= |

Each head uses an AttentionLayer at its core:

In[18]:= |

Out[18]= |

Attention is done with causal masking, which means that the embedding of a given subword token depends on the previous subword tokens and not on the subsequent ones. This is a prerequisite to be able to generate text with the language model. The following figures compare causal attention to other forms of connectivity between input tokens:

Retrieve the language model by specifying the "Task" parameter:

In[19]:= |

Out[19]= |

Predict the next word in a given sequence:

In[20]:= |

Out[20]= |

Obtain the top 15 probabilities:

In[21]:= |

Out[21]= |

Plot the top 15 probabilities:

In[22]:= |

Out[22]= |

Modify the language model so that it accepts the encoded token indices as input and creates the token indices as output:

In[23]:= |

Create a new decoder that performs a lookup to find the corresponding string, followed by some text cleaning:

In[24]:= |

Define a function to predict the next token using the modified language model:

In[25]:= |

Get an input:

In[26]:= |

Out[26]= |

Generate the next 20 tokens by using it on the input:

In[27]:= |

Out[27]= |

The third optional argument is a “temperature” parameter that scales the input to the final softmax. A high temperature flattens the distribution from which tokens are sampled, increasing the probability of extracting less likely tokens:

In[28]:= |

Out[28]= |

Decreasing the temperature sharpens the peaks of the sampling distribution, further decreasing the probability of extracting less likely tokens:

In[29]:= |

Out[29]= |

Very high temperature settings are equivalent to random sampling:

In[30]:= |

Out[30]= |

Very low temperature settings are equivalent to always picking the character with maximum probability. It is typical for sampling to “get stuck in a loop”:

In[31]:= |

Out[31]= |

Define a sentence embedding that consists of the last subword embedding of GPT (this choice is justified by the fact that GPT is a forward causal model):

In[32]:= |

Out[32]= |

Define some sentences in two broad categories for comparison:

In[33]:= |

Precompute the embeddings for a list of sentences:

In[34]:= |

Visualize the similarity between the sentences using the net as a feature extractor:

In[35]:= |

Out[35]= |

Get a text-processing dataset:

In[36]:= |

View a random sample of the dataset:

In[37]:= |

Out[37]= |

Define a sentence embedding that consists of the last subword embedding of GPT (this choice is justified by the fact that GPT is a forward causal model):

In[38]:= |

Out[38]= |

Precompute the GPT vectors for the training and the validation datasets (if available, GPU is highly recommended):

In[39]:= |

Define a simple network for classification:

In[40]:= |

Out[41]= |

Train the network on the precomputed GPT vectors:

In[42]:= |

Out[42]= |

Check the classification error rate on the validation data:

In[43]:= |

Out[43]= |

Compare the results with the performance of a classifier trained on context-independent word embeddings. Precompute the GloVe vectors for the training and the validation datasets (if available, GPU is recommended):

In[44]:= |

In[45]:= |

Define a simple network for classification, using a max-pooling strategy:

In[46]:= |

Out[46]= |

Train the classifier on the precomputed GloVe vectors:

In[47]:= |

Out[47]= |

Compare the results obtained with GPT and with GloVe:

In[48]:= |

Out[48]= |

Inspect the number of parameters of all arrays in the net:

In[49]:= |

Out[49]= |

Obtain the total number of parameters:

In[50]:= |

Out[50]= |

Obtain the layer type counts:

In[51]:= |

Out[51]= |

Display the summary graphic:

In[52]:= |

Out[52]= |

Wolfram Language 12.0 (April 2019) or above

- A. Radford, K. Narasimhan, T. Salimans, I. Sutskever, "Improving language understanding by generative pre-training," preprint (2018)
- (available from https://github.com/openai/finetune-transformer-lm)
- Rights: MIT License