params_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2020 gorse Project Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package model
  15. import (
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func TestParams_Copy(t *testing.T) {
  20. // Create parameters
  21. a := Params{
  22. NFactors: 1,
  23. Lr: 0.1,
  24. RandomState: 0,
  25. }
  26. // Create copy
  27. b := a.Copy()
  28. b[NFactors] = 2
  29. b[Lr] = 0.2
  30. b[RandomState] = 1
  31. // Check original parameters
  32. assert.Equal(t, 1, a.GetInt(NFactors, -1))
  33. assert.Equal(t, float32(0.1), a.GetFloat32(Lr, -0.1))
  34. assert.Equal(t, int64(0), a.GetInt64(RandomState, -1))
  35. // Check copy parameters
  36. assert.Equal(t, 2, b.GetInt(NFactors, -1))
  37. assert.Equal(t, float32(0.2), b.GetFloat32(Lr, -0.1))
  38. assert.Equal(t, int64(1), b.GetInt64(RandomState, -1))
  39. }
  40. func TestParams_GetFloat32(t *testing.T) {
  41. p := Params{}
  42. // Empty case
  43. assert.Equal(t, float32(0.1), p.GetFloat32(Lr, 0.1))
  44. // Normal case
  45. p[Lr] = float32(1.0)
  46. assert.Equal(t, float32(1.0), p.GetFloat32(Lr, 0.1))
  47. // Convertible case
  48. p[Lr] = 2.0
  49. assert.Equal(t, float32(2.0), p.GetFloat32(Lr, 0.1))
  50. p[Lr] = int(3)
  51. assert.Equal(t, float32(3.0), p.GetFloat32(Lr, 0.1))
  52. // Wrong type case
  53. p[Lr] = 1
  54. assert.Equal(t, float32(1.0), p.GetFloat32(Lr, 0.1))
  55. p[Lr] = "hello"
  56. assert.Equal(t, float32(0.1), p.GetFloat32(Lr, 0.1))
  57. }
  58. func TestParams_GetBool(t *testing.T) {
  59. p := Params{}
  60. // Empty case
  61. assert.True(t, p.GetBool(UseFeature, true))
  62. // Normal case
  63. p[UseFeature] = false
  64. assert.False(t, p.GetBool(UseFeature, true))
  65. // Wrong type case
  66. p[UseFeature] = 1
  67. assert.True(t, p.GetBool(UseFeature, true))
  68. }
  69. func TestParams_GetInt(t *testing.T) {
  70. p := Params{}
  71. // Empty case
  72. assert.Equal(t, -1, p.GetInt(NFactors, -1))
  73. // Normal case
  74. p[NFactors] = 0
  75. assert.Equal(t, 0, p.GetInt(NFactors, -1))
  76. // Wrong type case
  77. p[NFactors] = "hello"
  78. assert.Equal(t, -1, p.GetInt(NFactors, -1))
  79. }
  80. func TestParams_GetInt64(t *testing.T) {
  81. p := Params{}
  82. // Empty case
  83. assert.Equal(t, int64(-1), p.GetInt64(RandomState, -1))
  84. // Normal case
  85. p[RandomState] = int64(0)
  86. assert.Equal(t, int64(0), p.GetInt64(RandomState, -1))
  87. // Wrong type case
  88. p[RandomState] = 0
  89. assert.Equal(t, int64(0), p.GetInt64(RandomState, -1))
  90. p[RandomState] = "hello"
  91. assert.Equal(t, int64(-1), p.GetInt64(RandomState, -1))
  92. }
  93. func TestParams_GetString(t *testing.T) {
  94. p := Params{}
  95. // Empty case
  96. assert.Equal(t, "xyz", p.GetString(Similarity, "xyz"))
  97. // Normal case
  98. p[Similarity] = "abc"
  99. assert.Equal(t, "abc", p.GetString(Similarity, "abc"))
  100. }
  101. func TestParams_GetIntSlice(t *testing.T) {
  102. p := Params{}
  103. // Empty case
  104. assert.Equal(t, []int{1, 2, 3}, p.GetIntSlice(HiddenLayers, []int{1, 2, 3}))
  105. // Normal case
  106. p[HiddenLayers] = []int{4, 5, 6}
  107. assert.Equal(t, []int{4, 5, 6}, p.GetIntSlice(HiddenLayers, []int{1, 2, 3}))
  108. // Wrong type case
  109. p[HiddenLayers] = []string{"hello"}
  110. assert.Equal(t, []int{1, 2, 3}, p.GetIntSlice(HiddenLayers, []int{1, 2, 3}))
  111. }
  112. func TestParams_Overwrite(t *testing.T) {
  113. a := Params{
  114. NFactors: 10,
  115. Lr: 0.5,
  116. }
  117. b := Params{
  118. NEpochs: 100,
  119. NFactors: 20,
  120. }
  121. c := a.Overwrite(b)
  122. assert.Equal(t, 20, c[NFactors])
  123. assert.Equal(t, 0.5, c[Lr])
  124. assert.Equal(t, 100, c[NEpochs])
  125. }
  126. func TestParamsGrid(t *testing.T) {
  127. grid := ParamsGrid{}
  128. grid["a"] = []interface{}{0, 1}
  129. defaultGrid := ParamsGrid{}
  130. defaultGrid["a"] = []interface{}{2, 3}
  131. defaultGrid["b"] = []interface{}{4, 5}
  132. assert.Equal(t, 1, grid.Len())
  133. grid.Fill(defaultGrid)
  134. assert.Equal(t, []interface{}{0, 1}, grid["a"])
  135. assert.Equal(t, []interface{}{4, 5}, grid["b"])
  136. assert.Equal(t, 4, grid.NumCombinations())
  137. }