forked from olivia-ai/olivia
/
data.go
70 lines (58 loc) 路 1.9 KB
/
data.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
package server
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/NerdDoc/server/network"
)
// Dashboard contains the data sent for the dashboard
type Dashboard struct {
Layers Layers `json:"layers"`
Training Training `json:"training"`
}
// Layers contains the data of the network's layers
type Layers struct {
InputNodes int `json:"input"`
HiddenLayers int `json:"hidden"`
OutputNodes int `json:"output"`
}
// Training contains the data related to the training of the network
type Training struct {
Rate float64 `json:"rate"`
Errors []float64 `json:"errors"`
Time float64 `json:"time"`
}
// GetDashboardData encodes the json for the dashboard data
func GetDashboardData(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
data := mux.Vars(r)
dashboard := Dashboard{
Layers: GetLayers(data["locale"]),
Training: GetTraining(data["locale"]),
}
err := json.NewEncoder(w).Encode(dashboard)
if err != nil {
log.Fatal(err)
}
}
// GetLayers returns the number of input, hidden and output layers of the network
func GetLayers(locale string) Layers {
return Layers{
// Get the number of rows of the first layer to get the count of input nodes
InputNodes: network.Rows(neuralNetworks[locale].Layers[0]),
// Get the number of hidden layers by removing the count of the input and output layers
HiddenLayers: len(neuralNetworks[locale].Layers) - 2,
// Get the number of rows of the latest layer to get the count of output nodes
OutputNodes: network.Columns(neuralNetworks[locale].Output),
}
}
// GetTraining returns the learning rate, training date and error loss for the network
func GetTraining(locale string) Training {
// Retrieve the information from the neural network
return Training{
Rate: neuralNetworks[locale].Rate,
Errors: neuralNetworks[locale].Errors,
Time: neuralNetworks[locale].Time,
}
}