elman

package
v0.0.0-...-be65bb2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2016 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Test

func Test()

Test runs @numEpochs iterations of introducing the Abstract Time Series data to an Elman RNN and prints the network's output after the last iteration.

Types

type Acts

type Acts struct {
	Out *m.Vector
	Hid *m.Vector
	Inp *m.Vector
}

Acts is used to keep activations of each neuron of output and hidden layers. So, Acts.Hid.At(2, 0) is the activation of the 3rd neuron in hidden layer.

type Args

type Args struct {
	Eta    float64
	NumInp int
	NumHid int
	NumOut int
	Depth  int
}

Args holds names parameters to the NewElman() constructor.

type Elman

type Elman struct {
	NumInp int
	NumHid int
	NumOut int

	Depth int      // Number of steps down the unfolded network
	IH    *m.Dense // Weights from input to hidden layer
	HH    *m.Dense // Weights from hidden to hidden layer
	HO    *m.Dense // Weights from hidden to output layer
	// contains filtered or unexported fields
}

Elman is a simple Recursive Neuron Network which has recurrent connections from hidden layer neurons at time step (t-1) to hidden layer neurons at time step (t). We use this simplified model (without the possibility to add arbitrary number of hidden layers) to reduce the number of obscure indices and to use only named entities. We also use no biases (again, for simplicity).

func NewElman

func NewElman(args *Args) *Elman

NewElman is a constructor for Elman. Initializes weight matrices.

func (*Elman) BPTT

func (n *Elman) BPTT(input, expected *m.Dense) (
	dErrdIH, dErrdHH, dErrdHO *m.Dense)

BPTT implements the Backpropagation Through Time algorithm to learn the network's weight. As BPTT is a variation of standard Backpropagation, it might be useful to look at basicNN code and look for similarities. Note that we don't have a separate Update() method; all weights are updated "on the go".

func (*Elman) Forward

func (n *Elman) Forward(input *m.Dense) (sums []*Sums, acts []*Acts)

Forward accumulates sums and activations for each layer for each training sample.

func (*Elman) GetError

func (n *Elman) GetError(prevErrs, currSums *m.Vector, w *m.Dense) *m.Vector

GetError returns errors for each neuron in any single layer (L) using the errors in the layer just after it (L+1). The errors of (L+1) are propagated backwards to (L) using the same (L-to-L+1) weights that we used when passing (L)-activations to (L+1). Of course, we need to get a transposed version of (L-to-L+1) weights to make the matrix operations possible. After this backward-pass we multiply the (L)-errors by sigmoidPrime(L-sums), just as in GetOutError().

func (*Elman) GetHidden

func (n *Elman) GetHidden(prevHidden, sample *m.Vector) (sums, acts *m.Vector)

GetHidden calculates current hidden state as follows:

  1. Multiplies inputToHidden matrix by the input sample (same as getting a weighted sum of inputs for each hidden neuron);
  2. Multiplies hiddenToHidden matrix by previous hidden layer (same as getting a weighted sum of inputs for each hidden neuron);
  3. Sums the results from steps 1, 2 and applies activation function (hyperbolic tanhent in this case).

func (*Elman) GetOutError

func (n *Elman) GetOutError(outActs, outSums, expected *m.Vector) *m.Vector

GetOutError returns the output layer error as (output activations − expected activations) ⊙ sigmoidPrime(output sums).

func (*Elman) GetOutput

func (n *Elman) GetOutput(currHidden *m.Vector) (sums, acts *m.Vector)

GetOutput just multiplies hiddenToHidden matrix by previous hidden layer (same as getting a weighted sum of inputs for each output neuron).

func (*Elman) RunEpochs

func (n *Elman) RunEpochs(numEpochs int, input, expected *m.Dense)

RunEpochs runs the BPTT algorithm for @input @numEpochs times.

type Sums

type Sums struct {
	Out *m.Vector
	Hid *m.Vector
}

Sums is used to keep weighted sums received by each neuron of output and hidden layers. So, Sums.Hid.At(2, 0) is the weighted sum received by the 3rd neuron in hidden layer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL