RBMparallel.praat 3.7 KB

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