-
Notifications
You must be signed in to change notification settings - Fork 24
/
hypers.go
101 lines (87 loc) · 2.58 KB
/
hypers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2019, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package params
import (
"bytes"
"encoding/json"
"fmt"
"log"
"github.com/goki/ki/kit"
)
// HyperVals is a string-value map for storing hyperparameter values
type HyperVals map[string]string
// JSONString returns hyper values as a JSON formatted string
func (hv *HyperVals) JSONString() string {
var buf bytes.Buffer
b, _ := json.Marshal(hv)
buf.Write(b)
return buf.String()
}
// SetJSONString sets from a JSON_formatted string
func (hv *HyperVals) SetJSONString(str string) error {
return json.Unmarshal([]byte(str), hv)
}
// CopyFrom copies from another HyperVals
func (hv *HyperVals) CopyFrom(cp HyperVals) {
if *hv == nil {
*hv = make(HyperVals, len(cp))
}
for k, v := range cp {
(*hv)[k] = v
}
}
// Hypers is a parallel structure to Params which stores information relevant
// to hyperparameter search as well as the values.
// Use the key "Val" for the default value. This is equivalant to the value in
// Params. "Min" and "Max" guid the range, and "Sigma" describes a Gaussian.
type Hypers map[string]HyperVals
// ParamByNameTry returns given parameter, by name.
// Returns error if not found.
func (pr *Hypers) ParamByNameTry(name string) (map[string]string, error) {
vl, ok := (*pr)[name]
if !ok {
err := fmt.Errorf("params.Params: parameter named %v not found", name)
log.Println(err)
return map[string]string{}, err
}
return vl, nil
}
// ParamByName returns given parameter by name (just does the map access)
// Returns "" if not found -- use Try version for error
func (pr *Hypers) ParamByName(name string) map[string]string {
return (*pr)[name]
}
// SetByName sets given parameter by name to given value.
// (just a wrapper around map set function)
func (pr *Hypers) SetByName(name string, value map[string]string) {
(*pr)[name] = value
}
// CopyFrom copies hyper vals from source
func (pr *Hypers) CopyFrom(cp Hypers) {
if *pr == nil {
*pr = make(Hypers, len(cp))
}
for path, hv := range cp {
if shv, has := (*pr)[path]; has {
shv.CopyFrom(hv)
} else {
shv := HyperVals{}
shv.CopyFrom(hv)
(*pr)[path] = shv
}
}
}
// DeleteValOnly deletes entries that only have a "Val" entry.
// This happens when applying a param Sheet using Flex params
// to compile values using styling logic
func (pr *Hypers) DeleteValOnly() {
for path, hv := range *pr {
if len(hv) == 1 {
if _, has := (hv)["Val"]; has {
delete(*pr, path)
}
}
}
}
var KiT_Hypers = kit.Types.AddType(&Hypers{}, HypersProps)