-
Notifications
You must be signed in to change notification settings - Fork 2
/
lstm.go
69 lines (58 loc) · 2.42 KB
/
lstm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package automata
// NewLSTM creates a new Long-Short Term Memory network. 'inputSize' is the number of neurons in the input layer.
// 'outputSize' is the number of neurons in the output layer. The length of 'memoryBlocks' determines how many
// memory blocks the network will have, whilst the elements determine the number of memory cells in that block.
func NewLSTM(table *LookupTable, inputSize int, memoryBlocks []int, outputSize int) *Network {
inputLayer := NewLayer(table, inputSize)
outputLayer := NewLayer(table, outputSize)
var hiddenLayers []Layer
var prevMemoryBlock *Layer
for _, cellSize := range memoryBlocks {
inputGate := NewLayer(table, cellSize)
inputGate.SetBias(1)
forgetGate := NewLayer(table, cellSize)
forgetGate.SetBias(1)
outputGate := NewLayer(table, cellSize)
outputGate.SetBias(1)
memoryCell := NewLayer(table, cellSize)
hiddenLayers = append(hiddenLayers, inputGate)
hiddenLayers = append(hiddenLayers, forgetGate)
hiddenLayers = append(hiddenLayers, memoryCell)
hiddenLayers = append(hiddenLayers, outputGate)
// connections from input
input := inputLayer.Project(&memoryCell, LayerTypeAuto)
inputLayer.Project(&inputGate, LayerTypeAuto)
inputLayer.Project(&forgetGate, LayerTypeAuto)
inputLayer.Project(&outputGate, LayerTypeAuto)
// connections from prev memory block
var cell *LayerConnection
if prevMemoryBlock != nil {
cell = prevMemoryBlock.Project(&memoryCell, LayerTypeAuto)
prevMemoryBlock.Project(&inputGate, LayerTypeAuto)
prevMemoryBlock.Project(&forgetGate, LayerTypeAuto)
prevMemoryBlock.Project(&outputGate, LayerTypeAuto)
}
// connections from memory cell
output := memoryCell.Project(&outputLayer, LayerTypeAuto)
self := memoryCell.Project(&memoryCell, LayerTypeAuto)
// peepholes
memoryCell.Project(&inputGate, LayerTypeAllToAll)
memoryCell.Project(&forgetGate, LayerTypeAllToAll)
memoryCell.Project(&outputGate, LayerTypeAllToAll)
// gates
inputGate.Gate(input, GateTypeInput)
forgetGate.Gate(self, GateTypeOneToOne)
outputGate.Gate(output, GateTypeOutput)
if cell != nil {
inputGate.Gate(cell, GateTypeInput)
}
prevMemoryBlock = &memoryCell
}
// connect input layer to output layer (TODO: customise these conns?)
inputLayer.Project(&outputLayer, LayerTypeAuto)
return &Network{
Input: &inputLayer,
Hidden: hiddenLayers,
Output: &outputLayer,
}
}