|
| 1 | +package trainer |
| 2 | + |
| 3 | +import "fmt" |
| 4 | +import "os" |
| 5 | +import "math" |
| 6 | + |
| 7 | +import "github.com/neurlang/classifier/parallel" |
| 8 | +import "github.com/neurlang/classifier/net/feedforward" |
| 9 | + |
| 10 | +type dummy struct{} |
| 11 | + |
| 12 | +func (d dummy) MustPutUint16(n int, value uint16) {} |
| 13 | +func (d dummy) Sum() [32]byte { |
| 14 | +return [32]byte{} |
| 15 | +} |
| 16 | + |
| 17 | +type EvaluateFuncHasher interface { |
| 18 | +MustPutUint16(n int, value uint16) |
| 19 | +Sum() [32]byte |
| 20 | +} |
| 21 | + |
| 22 | +// sampleSize calculates the statistically sufficient sample size |
| 23 | +// for a given dataset size N and significance level (0–100). |
| 24 | +func sampleSize(N int, significance byte) int { |
| 25 | + |
| 26 | +// Convert significance level to Z-score |
| 27 | +z := zScoreFromAlpha(100 - significance) |
| 28 | + |
| 29 | +// Assume worst-case proportion p = 0.5 for max variability |
| 30 | +p := 0.5 |
| 31 | +e := float64(100 - significance) // Margin of error = 5% |
| 32 | + |
| 33 | +numerator := math.Pow(z, 2) * p * (1 - p) |
| 34 | +denominator := math.Pow(e, 2) |
| 35 | + |
| 36 | +// Initial sample size without population correction |
| 37 | +ss := numerator / denominator |
| 38 | + |
| 39 | +// Apply finite population correction |
| 40 | +correctedSS := ss * float64(N) / (float64(N) - 1 + ss) |
| 41 | + |
| 42 | +if int(correctedSS) > N { |
| 43 | +return N |
| 44 | +} |
| 45 | + |
| 46 | +return int(correctedSS) |
| 47 | +} |
| 48 | + |
| 49 | +// zScoreFromAlpha returns the Z-score for a given alpha level |
| 50 | +// Common: 90% => 1.645, 95% => 1.96, 99% => 2.576 |
| 51 | +func zScoreFromAlpha(alpha byte) float64 { |
| 52 | +switch { |
| 53 | +case alpha <= 1: |
| 54 | +return 2.576 // 99% confidence |
| 55 | +case alpha <= 5: |
| 56 | +return 1.96 // 95% confidence |
| 57 | +case alpha <= 10: |
| 58 | +return 1.645 // 90% confidence |
| 59 | +default: |
| 60 | +return 1.96 // default fallback |
| 61 | +} |
| 62 | +} |
| 63 | + |
| 64 | +func NewEvaluateFunc(net feedforward.FeedforwardNetwork, length int, significance byte, succ *int, dstmodel *string, |
| 65 | +testFunc func(portion int, h EvaluateFuncHasher) int) func() (int, [32]byte) { |
| 66 | + |
| 67 | +return func() (int, [32]byte) { |
| 68 | +var h dummy |
| 69 | +var ha EvaluateFuncHasher = h |
| 70 | +var success int |
| 71 | +if length != 0 { |
| 72 | +length = sampleSize(length, significance) |
| 73 | +hsh := parallel.NewUint16Hasher(length) |
| 74 | +ha = hsh |
| 75 | +success = testFunc(length, hsh) |
| 76 | +} else { |
| 77 | +success = testFunc(0, h) |
| 78 | +} |
| 79 | + |
| 80 | +if dstmodel == nil || *dstmodel == "" { |
| 81 | +err := net.WriteZlibWeightsToFile("output." + fmt.Sprint(success) + ".json.t.lzw") |
| 82 | +if err != nil { |
| 83 | +println(err.Error()) |
| 84 | +} |
| 85 | +} |
| 86 | + |
| 87 | +if dstmodel != nil && len(*dstmodel) > 0 && ((succ != nil && (*succ < success || success == 99)) || succ == nil) { |
| 88 | +if succ != nil && *succ > 0 { |
| 89 | +err := net.WriteZlibWeightsToFile(*dstmodel) |
| 90 | +if err != nil { |
| 91 | +println(err.Error()) |
| 92 | +} |
| 93 | +} |
| 94 | +if succ != nil { |
| 95 | +*succ = success |
| 96 | +} |
| 97 | +} else if dstmodel != nil && len(*dstmodel) > 0 { |
| 98 | +if succ != nil { |
| 99 | +*succ = success |
| 100 | +} |
| 101 | +} |
| 102 | + |
| 103 | +if success >= 100 { |
| 104 | +println("Max accuracy or wrong data. Exiting") |
| 105 | +os.Exit(0) |
| 106 | +} |
| 107 | +return success, ha.Sum() |
| 108 | +} |
| 109 | +} |
0 commit comments