test_FFNet.praat 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # test_FFNet.praat
  2. # djmw 20110512, 20180918
  3. printline test FFNet
  4. Create iris example... 0 0
  5. ffnet = selected ("FFNet")
  6. pattern = selected ("Pattern")
  7. cat = selected ("Categories")
  8. selectObject: ffnet
  9. @testInterface: 0
  10. @testInterface: 1
  11. @testInterface: 2
  12. selectObject: pattern, cat
  13. tab = To TableOfReal
  14. To Pattern and Categories: 0, 0, 0, 0
  15. Remove
  16. removeObject: tab
  17. ntries = 10
  18. selectObject: ffnet, pattern, cat
  19. error1 = Get total costs... Minimum-squared-error
  20. tab = Create Table with column names... results ntries costsb costsa fc
  21. for i to ntries
  22. select ffnet
  23. Reset... 0.1
  24. plus pattern
  25. plus cat
  26. costsb = Get total costs... Minimum-squared-error
  27. Learn... 200 1e-7 Minimum-squared-error
  28. costsa = Get total costs... Minimum-squared-error
  29. select ffnet
  30. plus pattern
  31. cati = To Categories... Winner-takes-all
  32. plus cat
  33. fd = Get fraction different
  34. fc = 1 -fd
  35. select tab
  36. Set numeric value... i costsb costsb
  37. Set numeric value... i costsa costsa
  38. Set numeric value... i fc fc
  39. select cati
  40. Remove
  41. endfor
  42. select tab
  43. mean = Get mean... fc
  44. assert mean > 0.97
  45. plus ffnet
  46. plus pattern
  47. plus cat
  48. Remove
  49. @test_openSave
  50. printline FFNet ok
  51. procedure test_openSave
  52. .ffnet_read= Read from file: "iris_4-2-3-3.FFNet"
  53. Create iris example: 2, 3
  54. .ffnet = selected ("FFNet")
  55. .pattern = selected ("Pattern")
  56. .categories = selected ("Categories")
  57. selectObject: .ffnet
  58. Save as binary file: "kanweg.FFNet"
  59. .ffnet_read2 = Read from file: "kanweg.FFNet"
  60. # are they the same ??
  61. selectObject: .ffnet_read, .pattern, .categories
  62. .costs[1] = Get total costs: "Minimum-squared-error"
  63. selectObject: .ffnet_read, .pattern, .categories
  64. Learn: 100, 1e-7, "Minimum-squared-error"
  65. .costs[2] = Get total costs: "Minimum-squared-error"
  66. assert .costs[1] >= .costs[2]
  67. removeObject: .ffnet, .ffnet_read, .ffnet_read2, .pattern, .categories
  68. endproc
  69. procedure testInterface: .numberOfHiddenLayers
  70. .numberOfInputs_set = randomInteger (2, 5)
  71. .numberOfOutputs_set = randomInteger (2, 5)
  72. .name$ = string$ (.numberOfInputs_set)
  73. for .layer to 2
  74. .numberOfHiddenUnits_set [.layer] = 0
  75. .numberOfUnits = randomInteger (2, 10)
  76. if .layer <= .numberOfHiddenLayers
  77. .numberOfHiddenUnits_set [.layer] = .numberOfUnits
  78. .name$ = .name$ + "-" + string$ (.numberOfUnits)
  79. endif
  80. endfor
  81. .name$ = .name$ + "-" + string$ (.numberOfOutputs_set)
  82. .ffnet = Create FFNet: .name$, .numberOfInputs_set, .numberOfOutputs_set, .numberOfHiddenUnits_set [1], .numberOfHiddenUnits_set [2]
  83. appendInfoLine: tab$, "interface test for topology " + .name$
  84. appendInfoLine: tab$, tab$, "Query & Modify"
  85. .numberOfLayers = Get number of layers
  86. appendInfoLine: .numberOfLayers
  87. .numberOfOutputs = Get number of outputs
  88. .numberOfUnitsInPreviousLayer = Get number of inputs
  89. for .ilayer to .numberOfLayers - 1
  90. .numberOfHiddenUnits = Get number of hidden units: .ilayer
  91. assert .numberOfHiddenUnits == .numberOfHiddenUnits_set [.ilayer]; "layer = "'.ilayer'
  92. for .iunit to .numberOfHiddenUnits
  93. .value_set = randomUniform (-0.1, 0.1)
  94. Set bias: .ilayer, .iunit, .value_set
  95. .value_get = Get bias: .ilayer, .iunit
  96. assert .value_get == .value_set; bias '.ilayer', '.iunit', '.value_set'
  97. for .punit to .numberOfUnitsInPreviousLayer
  98. .value_set = randomUniform (-0.1, 0.1)
  99. Set weight: .ilayer, .iunit, .punit, .value_set
  100. .value_get = Get weight: .ilayer, .iunit, .punit
  101. assert .value_get == .value_set; '.ilayer', '.iunit', '.punit', '.value_set'
  102. endfor
  103. endfor
  104. .numberOfUnitsInPreviousLayer = .numberOfHiddenUnits
  105. .numberOfHiddenWeights = Get number of hidden weights: .ilayer
  106. Select biases: .ilayer
  107. Select all weights
  108. endfor
  109. .numberOfOutputWeights = Get number of output weights
  110. for .iunit to .numberOfOutputs
  111. .outputCategory$ = Get category of output unit: .iunit
  112. .outputUnit = Get output unit of category: "xpiepeledokie"
  113. assert .outputUnit == 0; '.outputUnit'
  114. endfor
  115. .minimum = Get minimum
  116. appendInfoLine: tab$, tab$, "Extract"
  117. for .ilayer to .numberOfLayers - 1
  118. selectObject: .ffnet
  119. .tableOfReal = Extract weights: .ilayer
  120. removeObject: .tableOfReal
  121. endfor
  122. removeObject: .ffnet
  123. endproc