| 1 |
writeInfoLine: "Training two RBM's with a three-peaked distribution" |
| 2 |
stopwatch |
| 3 |
|
| 4 |
numberOfInputNodes = 30 |
| 5 |
numberOfMiddleNodes = 50 |
| 6 |
numberOfOutputNodes = 20 |
| 7 |
numberOfVowels = 3 |
| 8 |
mean# = { 8, 16, 23 } |
| 9 |
sigma = 1.8 |
| 10 |
numberOfPatterns = 10000 |
| 11 |
learningRate = 0.001 |
| 12 |
|
| 13 |
# |
| 14 |
# Train first layer. |
| 15 |
# |
| 16 |
input1# = zero# (numberOfInputNodes) |
| 17 |
output1# = zero# (numberOfMiddleNodes) |
| 18 |
inbias1# = zero# (numberOfInputNodes) |
| 19 |
outbias1# = zero# (numberOfMiddleNodes) |
| 20 |
weight1## = zero## (numberOfInputNodes, numberOfMiddleNodes) |
| 21 |
inrec1# = zero# (numberOfInputNodes) |
| 22 |
outrec1# = zero# (numberOfMiddleNodes) |
| 23 |
for idatum to numberOfPatterns |
| 24 |
vowel = randomInteger (1, numberOfVowels) |
| 25 |
formant = randomGauss (mean# [vowel], sigma) |
| 26 |
input1# ~ 5 * exp (-0.5/sigma^2 * (col - formant) ^ 2) - 0.5 |
| 27 |
# |
| 28 |
# Spread up, with Bernoulli sampling. |
| 29 |
# |
| 30 |
output1# = sigmoid# (outbias1# + mul# (input1#, weight1##)) |
| 31 |
output1# = randomBernoulli# (output1#) |
| 32 |
# |
| 33 |
# Spread down. |
| 34 |
# |
| 35 |
inrec1# = inbias1# + mul# (weight1##, output1#) ; Gaussian |
| 36 |
# |
| 37 |
# Spread up. |
| 38 |
# |
| 39 |
outrec1# = sigmoid# (outbias1# + mul# (inrec1#, weight1##)) |
| 40 |
# |
| 41 |
# Update. |
| 42 |
# |
| 43 |
inbias1# += learningRate * (input1# - inrec1#) |
| 44 |
outbias1# += learningRate * (output1# - outrec1#) |
| 45 |
weight1## += learningRate * (outer## (input1#, output1#) - outer## (inrec1#, outrec1#)) |
| 46 |
endfor |
| 47 |
|
| 48 |
# |
| 49 |
# Train second layer. |
| 50 |
# |
| 51 |
input2# = zero# (numberOfMiddleNodes) |
| 52 |
output2# = zero# (numberOfOutputNodes) |
| 53 |
inbias2# = zero# (numberOfMiddleNodes) |
| 54 |
outbias2# = zero# (numberOfOutputNodes) |
| 55 |
weight2## = zero## (numberOfMiddleNodes, numberOfOutputNodes) |
| 56 |
inrec2# = zero# (numberOfMiddleNodes) |
| 57 |
outrec2# = zero# (numberOfOutputNodes) |
| 58 |
for idatum to numberOfPatterns |
| 59 |
vowel = randomInteger (1, numberOfVowels) |
| 60 |
formant = randomGauss (mean# [vowel], sigma) |
| 61 |
input1# ~ 5 * exp (-0.5/sigma^2 * (col - formant) ^ 2) - 0.5 |
| 62 |
# |
| 63 |
# Spread up through first layer, with Bernoulli sampling. |
| 64 |
# |
| 65 |
output1# = sigmoid# (outbias1# + mul# (input1#, weight1##)) |
| 66 |
output1# = randomBernoulli# (output1#) |
| 67 |
;; output1# owned and target |
| 68 |
;; output1# by reference (and target) |
| 69 |
;; randomBernoulli# into output1# (because no use of output1# further on) |
| 70 |
;; assignment is no-op |
| 71 |
# |
| 72 |
# Copy output of first layer to input of second layer. |
| 73 |
# |
| 74 |
input2# = output1# |
| 75 |
# |
| 76 |
# Spread up through second layer, with Bernoulli sampling. |
| 77 |
# |
| 78 |
output2# = sigmoid# (outbias2# + mul# (input2#, weight2##)) |
| 79 |
output2# = randomBernoulli# (output2#) |
| 80 |
# |
| 81 |
# Spread down. |
| 82 |
# |
| 83 |
inrec2# = sigmoid# (inbias2# + mul# (weight2##, output2#)) |
| 84 |
; inrec2# owned and target TODO |
| 85 |
; inbias2# by reference OK |
| 86 |
; weight2## by reference OK |
| 87 |
; output2# by reference OK |
| 88 |
; mul# ref and ref into target (size matches, and inrec# does not occur further on) TODO |
| 89 |
; + ref into target TODO |
| 90 |
; sigmoid# into target TODO |
| 91 |
|
| 92 |
# |
| 93 |
# Spread up. |
| 94 |
# |
| 95 |
outrec2# = sigmoid# (outbias2# + mul# (inrec2#, weight2##)) |
| 96 |
# |
| 97 |
# Update. |
| 98 |
# |
| 99 |
inbias2# += learningRate * (input2# - inrec2#) |
| 100 |
outbias2# += learningRate * (output2# - outrec2#) |
| 101 |
weight2## += learningRate * (outer## (input2#, output2#) - outer## (inrec2#, outrec2#)) |
| 102 |
endfor |
| 103 |
|
| 104 |
appendInfoLine: "Trained in ", stopwatch, " seconds" |
| 105 |
|
| 106 |
numberOfTestPatterns = 15 |
| 107 |
Erase all |
| 108 |
Font size: 10 |
| 109 |
for itest to numberOfTestPatterns |
| 110 |
appendInfoLine: "Test pattern #", itest, ":" |
| 111 |
vowel = randomInteger (1, numberOfVowels) |
| 112 |
formant = randomGauss (mean# [vowel], sigma) |
| 113 |
input1# ~ 5 * exp (-0.5 * ((col - formant) / sigma) ^ 2) - 0.5 |
| 114 |
# |
| 115 |
# Draw input. |
| 116 |
# |
| 117 |
Select outer viewport: 0, 3, (itest - 1) * 0.6, (itest - 1) * 0.6 + 1.0 |
| 118 |
Create simple Matrix: "input", 1, numberOfInputNodes, ~ 5 * exp (-0.5 * ((col - formant) / sigma) ^ 2) - 0.5 |
| 119 |
stdev = Get standard deviation: 0, 0, 0, 0 |
| 120 |
appendInfoLine: " Energy in input layer: ", stdev |
| 121 |
Draw rows: 0, 0, 0, 0, -5, 5 |
| 122 |
Remove |
| 123 |
# |
| 124 |
# Spread up through first layer, without Bernoulli sampling. |
| 125 |
# |
| 126 |
output1# = sigmoid# (outbias1# + mul# (input1#, weight1##)) |
| 127 |
appendInfoLine: " Energy in middle layer: ", stdev (output1#) |
| 128 |
# |
| 129 |
# Copy output of first layer to input of second layer. |
| 130 |
# |
| 131 |
input2# = output1# |
| 132 |
# |
| 133 |
# Spread up through second layer, without Bernoulli sampling. |
| 134 |
# |
| 135 |
output2# = sigmoid# (outbias2# + mul# (input2#, weight2##)) |
| 136 |
appendInfoLine: " Energy in output layer: ", stdev (output2#) |
| 137 |
# |
| 138 |
# Spread down through second layer. |
| 139 |
# |
| 140 |
inrec2# = sigmoid# (inbias2# + mul# (weight2##, output2#)) |
| 141 |
appendInfoLine: " Energy in middle layer: ", stdev (inrec2#) |
| 142 |
# |
| 143 |
# Spread down through first layer. |
| 144 |
# |
| 145 |
inrec1# = inbias1# + mul# (weight1##, inrec2#) ; Gaussian |
| 146 |
# |
| 147 |
# Draw reflection. |
| 148 |
# |
| 149 |
Select outer viewport: 3, 6, (itest - 1) * 0.6, (itest - 1) * 0.6 + 1.0 |
| 150 |
Create simple Matrix: "reflection", 1, numberOfInputNodes, "inrec1# [col]" |
| 151 |
Draw rows: 0, 0, 0, 0, -5, 5 |
| 152 |
Remove |
| 153 |
endfor |