/
types.go
117 lines (102 loc) · 5.15 KB
/
types.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Package types contains most of the objects that the API reads or writes
package types
const (
BipolarSigmoid = "bipolar-sigmoid"
MultilayerPerceptron = "mlp"
)
var activationFuncs = []string{BipolarSigmoid}
var nets = []string{MultilayerPerceptron}
// ActivationFuncs returns the list of supported neuron activation functions
func ActivationFuncs() []string {
return activationFuncs
}
// Nets returns the list of supported network types
func Nets() []string {
return nets
}
// PagedRes is a wrapper for a paged response where next can be provided as offset for the subsequent request and last
// can be used to determine when there is nothing left to read
type PagedRes struct {
Last bool `json:"last"`
Next int `json:"next"`
Results interface{} `json:"results"`
}
// SimpleRes is used for errors and those cases where the response code would be sufficient but a JSON response helps
// consistency and user friendliness
type SimpleRes struct {
Result string `json:"result"` // Possible values are "error" and "ok"
Msg string `json:"message"`
}
// NewOkRes is a shortcut for building a SimpleRes for a successful result
func NewOkRes(msg string) *SimpleRes {
return &SimpleRes{Result: "ok", Msg: msg}
}
// NewErrorRes is a shortcut for building a SimpleRes for a failed result
func NewErrorRes(msg string) *SimpleRes {
return &SimpleRes{Result: "error", Msg: msg}
}
// BriefNet is a lightweight and standardized representation for neural network parameters
type BriefNet struct {
Accuracy float32 `json:"accuracy" example:"0.9"` // Fraction of patterns that were predicted correctly during testing
ActivationFunc string `json:"activationFunc"` // Function used to calculate the output of a neuron based on its inputs
Averages map[string]float32 `json:"averages"` // Averages of each value in the patterns that were used for training
Deviations map[string]float32 `json:"deviations"` // Standard deviation of each value in the patterns that were used for training
ErrMargin float32 `json:"errMargin"` // Maximum difference between the expected and produced result to still be considered correct during testing
HLayers int `json:"hLayers"` // Number of hidden layers
ID string `json:"id"`
Inputs []string `json:"inputs"`
LearningRate float32 `json:"learningRate"` // How much new inputs altered the network during training
Outputs []string `json:"outputs"`
Type string `json:"type"`
}
// TrainRequest as its name implies, is used to ask the training service to create or update a net
type TrainRequest struct {
ErrMargin float32 `json:"errMargin"` // Maximum difference between the expected and produced result to still be considered correct during testing
Inputs []string `json:"inputs"` // Which of the series values should be treated as inputs
Outputs []string `json:"outputs"` // Which of the series values should be treated as outputs
Required int `json:"required"` // Number of points from the series that should be used to train and test
SeriesID string `json:"seriesID"`
}
// BriefSeries is a lightweight representation of a time series
type BriefSeries struct {
Name string `json:"name"`
Count int `json:"count"`
}
// CategorizedPoint is similar to pointstores.Point but has its values divided into inputs and outputs (this separation
// isn't important when it comes to storing it but allows producers to state their intentions so that, when enough
// points are available, a training request can be automatically generated)
type CategorizedPoint struct {
Inputs map[string]float32 `json:"inputs"`
Outputs map[string]float32 `json:"outputs"`
TimeStamp int64 `json:"timestamp"`
}
// HasChanged checks that at least one input and one output is different from the given point. As we're going to be
// using these to train a neural network it's important that we don't effectively just train it to recognize one value
// very well or that the inputs have 0 influence on the outputs
func (cp *CategorizedPoint) HasChanged(other CategorizedPoint) bool {
inDiff := false
for label, value := range cp.Inputs {
if other.Inputs[label] != value {
inDiff = true
break
}
}
outDiff := false
for label, value := range cp.Outputs {
if other.Outputs[label] != value {
outDiff = true
break
}
}
return inDiff && outDiff
}
// MetricsUpdate bundles snapshots of a set of values that share some relation that could be used to predict each
// other in a system. This relation doesn't need to be understood (otherwise implement a specific tool for it) but it
// should be there for everything to work
type MetricsUpdate struct {
SeriesID string `json:"seriesID"`
ErrMargin float32 `json:"errMargin"` // How much the predicted value of the net can differ from the actual but still be considered acceptable
Labels map[string]string `json:"labels"`
Points []CategorizedPoint `json:"points"`
Stage string `json:"stage"` // Valid stages are test or production
}