| 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 the first and second layer in parallel. |
| 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 |
input2# = zero# (numberOfMiddleNodes) |
| 24 |
output2# = zero# (numberOfOutputNodes) |
| 25 |
inbias2# = zero# (numberOfMiddleNodes) |
| 26 |
outbias2# = zero# (numberOfOutputNodes) |
| 27 |
weight2## = zero## (numberOfMiddleNodes, numberOfOutputNodes) |
| 28 |
inrec2# = zero# (numberOfMiddleNodes) |
| 29 |
outrec2# = zero# (numberOfOutputNodes) |
| 30 |
for idatum to numberOfPatterns |
| 31 |
vowel = randomInteger (1, numberOfVowels) |
| 32 |
formant = randomGauss (mean# [vowel], sigma) |
| 33 |
input1# ~ 5 * exp (-0.5/sigma^2 * (col - formant) ^ 2) - 0.5 |
| 34 |
# |
| 35 |
# Spread up, with Bernoulli sampling. |
| 36 |
# |
| 37 |
output1# = sigmoid# (outbias1# + mul# (input1#, weight1##)) |
| 38 |
output1# = randomBernoulli# (output1#) |
| 39 |
input2# = output1# |
| 40 |
output2# = sigmoid# (outbias2# + mul# (input2#, weight2##)) |
| 41 |
output2# = randomBernoulli# (output2#) |
| 42 |
# |
| 43 |
# Spread down. |
| 44 |
# |
| 45 |
inrec1# = inbias1# + mul# (weight1##, output1#) ; Gaussian |
| 46 |
inrec2# = sigmoid# (inbias2# + mul# (weight2##, output2#)) |
| 47 |
# |
| 48 |
# Spread up. |
| 49 |
# |
| 50 |
outrec1# = sigmoid# (outbias1# + mul# (inrec1#, weight1##)) |
| 51 |
outrec2# = sigmoid# (outbias2# + mul# (inrec2#, weight2##)) |
| 52 |
# |
| 53 |
# Update. |
| 54 |
# |
| 55 |
inbias1# += learningRate * (input1# - inrec1#) |
| 56 |
outbias1# += learningRate * (output1# - outrec1#) |
| 57 |
weight1## += learningRate * (outer## (input1#, output1#) - outer## (inrec1#, outrec1#)) |
| 58 |
inbias2# += learningRate * (input2# - inrec2#) |
| 59 |
outbias2# += learningRate * (output2# - outrec2#) |
| 60 |
weight2## += learningRate * (outer## (input2#, output2#) - outer## (inrec2#, outrec2#)) |
| 61 |
endfor |
| 62 |
|
| 63 |
appendInfoLine: "Trained in ", stopwatch, " seconds" |
| 64 |
appendInfoLine: weight1##, newline$, newline$, weight2## |
| 65 |
|
| 66 |
numberOfTestPatterns = 15 |
| 67 |
Erase all |
| 68 |
Font size: 10 |
| 69 |
for itest to numberOfTestPatterns |
| 70 |
appendInfoLine: "Test pattern #", itest, ":" |
| 71 |
vowel = randomInteger (1, numberOfVowels) |
| 72 |
formant = randomGauss (mean# [vowel], sigma) |
| 73 |
input1# ~ 5 * exp (-0.5 * ((col - formant) / sigma) ^ 2) - 0.5 |
| 74 |
# |
| 75 |
# Draw input. |
| 76 |
# |
| 77 |
Select outer viewport: 0, 3, (itest - 1) * 0.6, (itest - 1) * 0.6 + 1.0 |
| 78 |
Create simple Matrix: "input", 1, numberOfInputNodes, ~ 5 * exp (-0.5 * ((col - formant) / sigma) ^ 2) - 0.5 |
| 79 |
stdev = Get standard deviation: 0, 0, 0, 0 |
| 80 |
appendInfoLine: " Energy in input layer: ", stdev |
| 81 |
Draw rows: 0, 0, 0, 0, -5, 5 |
| 82 |
Remove |
| 83 |
# |
| 84 |
# Spread up through first layer, without Bernoulli sampling. |
| 85 |
# |
| 86 |
output1# = sigmoid# (outbias1# + mul# (input1#, weight1##)) |
| 87 |
appendInfoLine: " Energy in middle layer: ", stdev (output1#) |
| 88 |
# |
| 89 |
# Copy output of first layer to input of second layer. |
| 90 |
# |
| 91 |
input2# = output1# |
| 92 |
# |
| 93 |
# Spread up through second layer, without Bernoulli sampling. |
| 94 |
# |
| 95 |
output2# = sigmoid# (outbias2# + mul# (input2#, weight2##)) |
| 96 |
appendInfoLine: " Energy in output layer: ", stdev (output2#) |
| 97 |
# |
| 98 |
# Spread down through second layer. |
| 99 |
# |
| 100 |
inrec2# = sigmoid# (inbias2# + mul# (weight2##, output2#)) |
| 101 |
appendInfoLine: " Energy in middle layer: ", stdev (inrec2#) |
| 102 |
# |
| 103 |
# Spread down through first layer. |
| 104 |
# |
| 105 |
inrec1# = inbias1# + mul# (weight1##, inrec2#) ; Gaussian |
| 106 |
# |
| 107 |
# Draw reflection. |
| 108 |
# |
| 109 |
Select outer viewport: 3, 6, (itest - 1) * 0.6, (itest - 1) * 0.6 + 1.0 |
| 110 |
Create simple Matrix: "reflection", 1, numberOfInputNodes, "inrec1# [col]" |
| 111 |
appendInfoLine: " Energy in reflection: ", stdev (inrec1#) |
| 112 |
Draw rows: 0, 0, 0, 0, -5, 5 |
| 113 |
Remove |
| 114 |
endfor |