Wolfram Computation Meets Knowledge

Wolfram LaTeX Character-Level Language Model V1

Generate LaTeX code

This language model is based on a simple stack of gated recurrent layers. It was trained by Wolfram Research in 2018 using teacher forcing on sequences of length 100.

Number of layers: 7 | Parameter count: 7,896,330 | Trained size: 32 MB

Training Set Information

Examples

Resource retrieval

Retrieve the resource object:

In[1]:=
ResourceObject["Wolfram LaTeX Character-Level Language Model V1"]
Out[1]=

Get the pre-trained network:

In[2]:=
NetModel["Wolfram LaTeX Character-Level Language Model V1"]
Out[2]=

Basic usage

Predict the next character of a given sequence:

In[3]:=
NetModel["Wolfram LaTeX Character-Level Language Model V1"]["\\begin"]
Out[3]=

Get the top 15 probabilities:

In[4]:=
topProbs = 
 NetModel["Wolfram LaTeX Character-Level Language Model V1"][
  "\\begin", {"TopProbabilities", 15}]
Out[4]=

Plot the top 15 probabilities:

In[5]:=
BarChart[Thread@
  Labeled[Values@topProbs, 
   Keys[topProbs] /. {"\n" -> "\\n", "\t" -> "\\t"}], 
 ScalingFunctions -> "Log"]
Out[5]=

Generation

Generate text with temperature sampling. First split the net into two parts:

In[6]:=
net = NetTake[
   NetModel["Wolfram LaTeX Character-Level Language Model V1"], 6];
In[7]:=
sampler = 
  NetTake[NetModel[
    "Wolfram LaTeX Character-Level Language Model V1"], -1];

Define a function for efficient generation using NetStateObject:

In[8]:=
generateSample[start_, len_, temp_: 1] := With[
  {obj = NetStateObject[net]},
  StringJoin@
   NestList[sampler[obj[#]/temp, "RandomSample"] &, start, len]
  ]

Generate for 100 steps using “\begin” as an initial string:

In[9]:=
generateSample["\\begin", 100]
Out[9]=

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

In[10]:=
generateSample["\\begin", 100, 1.1]
Out[10]=

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

In[11]:=
generateSample["\\begin", 100, 0.4]
Out[11]=

Very high temperature settings are equivalent to random sampling:

In[12]:=
generateSample["\\begin", 100, 10]
Out[12]=

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[13]:=
generateSample["\\begin", 200, 0.01]
Out[13]=

Inspection of predictions

Define a function that takes a string and guesses the next character as it reads, showing the predictions in a grid. The input string is shown on top, while the top 5 predictions are aligned below each character, starting from more likely guesses. For each prediction, the intensity of the color is proportional to the probability:

In[14]:=
inspectPredictions[string_] := Block[
  {obj, chars, pred, predItems, charItems},
  obj = NetStateObject[
    NetModel["Wolfram LaTeX Character-Level Language Model V1"]];
  chars = Characters[string];
  pred = Map[obj[#, {"TopProbabilities", 5}] &, 
     chars] /. {"\n" -> "\\n", "\t" -> "\\t"};
  predItems = 
   Map[Item[First[#], 
      Background -> Opacity[Last[#], Darker[Green]]] &, pred, {2}];
  predItems = 
   Prepend[Most[predItems], Table[Item["", Background -> Gray], 5]];
  charItems = 
   Item[#, Background -> LightBlue] & /@ (chars /. {"\n" -> "\\n", 
       "\t" -> "\\t"});
  Grid[
   Prepend[Transpose[predItems], charItems],
   Spacings -> {0.6, 0.2}, Dividers -> All, FrameStyle -> Gray
   ]
  ]
In[15]:=
inspectPredictions["\\begin{equation}\n\\lambda_{t+1} = \\lambda_t + \
h_t\n\\end{equation}"]
Out[15]=

Net information

Inspect the sizes of all arrays in the net:

In[16]:=
NetInformation[
 NetModel["Wolfram LaTeX Character-Level Language Model V1"], \
"ArraysElementCounts"]
Out[16]=

Obtain the total number of parameters:

In[17]:=
NetInformation[
 NetModel["Wolfram LaTeX Character-Level Language Model V1"], \
"ArraysTotalElementCount"]
Out[17]=

Obtain the layer type counts:

In[18]:=
NetInformation[
 NetModel["Wolfram LaTeX Character-Level Language Model V1"], \
"LayerTypeCounts"]
Out[18]=

Display the summary graphic:

In[19]:=
NetInformation[
 NetModel["Wolfram LaTeX Character-Level Language Model V1"], \
"FullSummaryGraphic"]
Out[19]=

Export to MXNet

Export the net into a format that can be opened in MXNet:

In[20]:=
jsonPath = 
 Export[FileNameJoin[{$TemporaryDirectory, "net.json"}], 
  NetModel["Wolfram LaTeX Character-Level Language Model V1"], 
  "MXNet"]
Out[20]=

Export also creates a net.params file containing parameters:

In[21]:=
paramPath = FileNameJoin[{DirectoryName[jsonPath], "net.params"}]
Out[21]=

Get the size of the parameter file:

In[22]:=
FileByteCount[paramPath]
Out[22]=

The size is similar to the byte count of the resource object:

In[23]:=
ResourceObject[
  "Wolfram LaTeX Character-Level Language Model V1"]["ByteCount"]
Out[23]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

Reference

  • Wolfram Research (2018)