File size: 1,336 Bytes
dfd3544
 
a4aa491
 
 
 
 
 
dfd3544
 
 
a4aa491
dfd3544
 
a4aa491
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfd3544
 
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
package nn

import (
	"fmt"

	"github.com/Jensen-holm/ml-from-scratch/alg"
	"github.com/Jensen-holm/ml-from-scratch/request"
)

type NN struct {
	alg.Alg
	args *NNArgs
}

func New(rp *request.Payload) (alg.Alg, error) {
	// parse the args and make a new NN struct
	nnArgs := make(map[string]interface{})
	params := []string{
		"epochs",
		"activation",
		"hidden_size",
		"learning_rate",
	}

	for _, param := range params {
		i, isIn := rp.Args[param]
		if !isIn {
			return nil, fmt.Errorf("user must specify %s", param)
		}

		switch param {
		case "epochs":
			if val, ok := i.(int); ok {
				nnArgs[param] = val
			} else {
				return nil, fmt.Errorf("expected %s to be an int", param)
			}
		case "activation":
			if val, ok := i.(string); ok {
				nnArgs[param] = ActivationMap[val]
			} else {
				return nil, fmt.Errorf("expected %s to be a string", param)
			}
		case "hidden_size":
			if val, ok := i.(int); ok {
				nnArgs[param] = val
			} else {
				return nil, fmt.Errorf("expected %s to be an int", param)
			}
		case "learning_rate":
			if val, ok := i.(float64); ok {
				nnArgs[param] = val
			} else {
				return nil, fmt.Errorf("expected %s to be a float64", param)
			}
		default:
			return nil, fmt.Errorf("unsupported parameter: %s", param)
		}
	}

	args := NewArgs(nnArgs)
	return &NN{
		args: args,
	}, nil

}