Examples
AND
OR
XOR, no hidden units
XOR
AND · OR · XOR
Agreement, adjacent
Agreement, at a distance
Now another noun can stand between subject and verb (the boy near the girls walks), so the last word is no longer enough. This network has context units, a copy of its own hidden layer one word earlier, and can carry the number of the subject along. Train it, then delete the context line from the architecture (or press Make it feed-forward): that network is attracted by the nearest noun. The last two test pairs have two nouns in between, which never happens in training: try a few seeds to see how far the network generalises.
The network runs in your browser: please turn JavaScript on to train and test it.
Network
Weights
13 input units + 8 context → 8 hidden → 13 output units · 293 weights to learn, biases included.
Each word has its own input unit (the word just read) and its own output unit (how strongly it is expected next).
A line is a weight: the thicker, the stronger; solid excites, dashed inhibits. A unit fills up as it becomes active;
the square marked 1 is the bias, always on. Point at a unit, or click it, to read its weights.
All weights as a Hinton diagram
Training
96 sentences .
One epoch shows the network each of them once; after every sentence or word the weights move a little in the direction that would have made the error smaller.
Train
1 epoch
1 sentence
Reset weights
Test
20 test sentences
Under each word: its surprisal in bits, −log2 of the probability the network gave it before reading it (0 = fully expected; 1 bit = a coin toss).
On the right, the sum for the sentence. Click a word to see what the network expected in its place.
How these networks work
A network is a set of simple units joined by weighted connections. Nothing in it is programmed to solve the task:
it starts with random weights and changes them, a little at a time, whenever its answer is wrong.
Unit
Adds up what it receives, each incoming activation multiplied by the weight of its connection, plus its own bias.
The sum is squashed into a value between 0 and 1 (sigmoid) or between −1 and 1 (tanh): the unit’s activation.
Forward pass
A pattern switches the input units on; activation flows upward, layer by layer, to the output units. Their activations are the network’s answer.
Error
The difference between that answer and the target. With numbers it is reported as the root mean square (RMS) error over all outputs and patterns;
with sentences as surprisal, the bits needed on average to encode the word that really came next.
Backpropagation
The error is sent back down the same connections, so that every weight is blamed for its share and changed accordingly:
by a lot if the learning rate is high, and with a memory of the previous change if there is momentum.
Hidden units
Units between input and output. Nobody tells them what to do; they end up encoding whatever helps the output units.
Without them a network can only solve problems where one straight cut separates the answers (AND, OR, but not XOR).
Context units
In a recurrent network the hidden layer is copied, after every step, into a set of context units that feed back into it at the next step.
What the network does with a word then depends on the words before it. An empty line between number patterns, or the start of a sentence, wipes the context.
Architecture
The architecture box describes the network one layer per line, from the input at the bottom to the output at the top:
input auto # as many units as the patterns need
hidden 4 tanh # a hidden layer of 4 tanh units
hidden 3 # a second one (sigmoid, the default)
context # context units: hidden layer 1, one step back
output auto
input / output. A number, or auto to let the patterns decide. A number that does not match the patterns is reported, so you can check your own count.
The output units are sigmoid with number patterns and softmax with sentences.
hidden. One line per hidden layer, up to three, each with its number of units and, optionally, its activation:
sigmoid (0 to 1), tanh (−1 to 1) or linear (no squashing at all: try it on XOR).
Every unit of a layer is connected to every unit of the layer above, and every unit above the input has a bias.
context. Makes the network recurrent (Elman 1990): after every step, the hidden layer is copied into as many context units,
which feed back into it at the next step. context 2 copies hidden layer 2 instead of the first. Delete the line and the network is feed-forward again.
Patterns
Numbers. 0 1 -> 1: two input units, one output unit, and the answer wanted. Every line needs the same number of values.
A first line with names instead of numbers (p q -> XOR) labels the units. Output units give values between 0 and 1, so targets should stay in that range.
Sentences. With no arrow anywhere, each line is a sentence and each different word becomes one input and one output unit.
The network reads a word and must predict the next one; its output units are probabilities that sum to 1.
Write the full stop as a word of its own if the end of the sentence should be predicted too.
Templates. the (boy|girl) (near the dog)? walks . stands for four sentences. Writing a sentence twice makes it twice as frequent.
Test pairs. A starred test sentence is paired with the unstarred one above it (templates pair up one by one).
The network passes the pair when the grammatical sentence is the less surprising of the two.
Things to try
Change the seed and reset: the same network, from a different starting point, may learn faster, slower or not at all.
Raise the learning rate until the error curve starts to jump; set momentum to 0 and see how much longer training takes.
Open Weights , type in weights of your own (for instance, wire AND by hand) and load them.
Train the agreement network with the error travelling back one step only, as in the original simple recurrent network, and compare.
Give XOR two linear hidden units: however long it trains, a stack of linear layers is still one straight line.
References
D. E. Rumelhart, G. E. Hinton & R. J. Williams (1986). Learning representations by back-propagating errors. Nature 323, 533–536.
J. L. Elman (1990). Finding structure in time. Cognitive Science 14, 179–211.
J. L. Elman (1991). Distributed representations, simple recurrent networks, and grammatical structure. Machine Learning 7, 195–225.
K. Plunkett & J. L. Elman (1997). Exercises in Rethinking Innateness: A Handbook for Connectionist Simulations . MIT Press. (The tlearn simulator.)
T. Linzen, E. Dupoux & Y. Goldberg (2016). Assessing the ability of LSTMs to learn syntax-sensitive dependencies. TACL 4, 521–535.
Close
The weights, as numbers
weights[k][i][j] is the connection from unit j of layer k (0 = input) to unit i of the layer above;
bias[k][i] is the bias of that same unit, and context[i][j] goes from context unit j to hidden unit i.
Change any number and press Load to try weights of your own, or save a trained network and load it again later.
Weights in JSON
Copy
Download nn-weights.json
Load these weights
Close