Spaces:
Sleeping
Sleeping
Commit
·
59ae052
1
Parent(s):
6b47160
initializing weights and biases
Browse files- example/main.go +2 -0
- nn/main.go +38 -26
- nn/split.go +4 -4
- server.go +6 -1
example/main.go
CHANGED
@@ -16,6 +16,7 @@ type RequestPayload struct {
|
|
16 |
LearningRate float64 `json:"learning_rate"`
|
17 |
HiddenSize int `json:"hidden_size"`
|
18 |
ActivationFunc string `json:"activation"`
|
|
|
19 |
}
|
20 |
|
21 |
func main() {
|
@@ -42,6 +43,7 @@ func main() {
|
|
42 |
LearningRate: 0.01,
|
43 |
HiddenSize: 12,
|
44 |
ActivationFunc: "tanh",
|
|
|
45 |
}
|
46 |
|
47 |
jsonPayload, err := json.Marshal(payload)
|
|
|
16 |
LearningRate float64 `json:"learning_rate"`
|
17 |
HiddenSize int `json:"hidden_size"`
|
18 |
ActivationFunc string `json:"activation"`
|
19 |
+
TestSize float64 `json:"test_size"`
|
20 |
}
|
21 |
|
22 |
func main() {
|
|
|
43 |
LearningRate: 0.01,
|
44 |
HiddenSize: 12,
|
45 |
ActivationFunc: "tanh",
|
46 |
+
TestSize: 0.3,
|
47 |
}
|
48 |
|
49 |
jsonPayload, err := json.Marshal(payload)
|
nn/main.go
CHANGED
@@ -7,7 +7,6 @@ import (
|
|
7 |
|
8 |
"github.com/go-gota/gota/dataframe"
|
9 |
"github.com/gofiber/fiber/v2"
|
10 |
-
"gonum.org/v1/gonum/mat"
|
11 |
)
|
12 |
|
13 |
type NN struct {
|
@@ -25,6 +24,10 @@ type NN struct {
|
|
25 |
YTrain dataframe.DataFrame
|
26 |
XTest dataframe.DataFrame
|
27 |
YTest dataframe.DataFrame
|
|
|
|
|
|
|
|
|
28 |
}
|
29 |
|
30 |
func NewNN(c *fiber.Ctx) (*NN, error) {
|
@@ -40,13 +43,15 @@ func NewNN(c *fiber.Ctx) (*NN, error) {
|
|
40 |
|
41 |
func (nn *NN) Train() {
|
42 |
// train test split the data
|
43 |
-
|
44 |
|
45 |
-
|
46 |
|
47 |
// iterate n times where n = nn.Epochs
|
48 |
// use backprop algorithm on each iteration
|
49 |
// to fit the model to the data
|
|
|
|
|
50 |
|
51 |
}
|
52 |
|
@@ -54,31 +59,38 @@ func (nn *NN) InitWnB() {
|
|
54 |
// randomly initialize weights and biases to start
|
55 |
inputSize := len(nn.Features)
|
56 |
hiddenSize := nn.HiddenSize
|
57 |
-
outputSize := 1 // only predicting one thing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
return rand.Float64()*2 - 1
|
64 |
-
}, weightsInputHidden)
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
return rand.Float64()*2 - 1
|
77 |
-
}, weightsHiddenOutput)
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
}, biasesOutput)
|
84 |
}
|
|
|
7 |
|
8 |
"github.com/go-gota/gota/dataframe"
|
9 |
"github.com/gofiber/fiber/v2"
|
|
|
10 |
)
|
11 |
|
12 |
type NN struct {
|
|
|
24 |
YTrain dataframe.DataFrame
|
25 |
XTest dataframe.DataFrame
|
26 |
YTest dataframe.DataFrame
|
27 |
+
Wh [][]float64
|
28 |
+
Bh []float64
|
29 |
+
Wo [][]float64
|
30 |
+
Bo []float64
|
31 |
}
|
32 |
|
33 |
func NewNN(c *fiber.Ctx) (*NN, error) {
|
|
|
43 |
|
44 |
func (nn *NN) Train() {
|
45 |
// train test split the data
|
46 |
+
_, _, _, _ = nn.trainTestSplit()
|
47 |
|
48 |
+
nn.InitWnB()
|
49 |
|
50 |
// iterate n times where n = nn.Epochs
|
51 |
// use backprop algorithm on each iteration
|
52 |
// to fit the model to the data
|
53 |
+
for i := 0; i < nn.Epochs; i++ {
|
54 |
+
}
|
55 |
|
56 |
}
|
57 |
|
|
|
59 |
// randomly initialize weights and biases to start
|
60 |
inputSize := len(nn.Features)
|
61 |
hiddenSize := nn.HiddenSize
|
62 |
+
outputSize := 1 // only predicting one thing
|
63 |
+
|
64 |
+
// input hidden layer weights
|
65 |
+
wh := make([][]float64, inputSize)
|
66 |
+
for i := range wh {
|
67 |
+
wh[i] = make([]float64, hiddenSize)
|
68 |
+
for j := range wh[i] {
|
69 |
+
wh[i][j] = rand.Float64() - 0.5
|
70 |
+
}
|
71 |
+
}
|
72 |
|
73 |
+
bh := make([]float64, hiddenSize)
|
74 |
+
for i := range bh {
|
75 |
+
bh[i] = rand.Float64() - 0.5
|
76 |
+
}
|
|
|
|
|
77 |
|
78 |
+
// initialize weights and biases for hidden -> output layer
|
79 |
+
wo := make([][]float64, hiddenSize)
|
80 |
+
for i := range wo {
|
81 |
+
wo[i] = make([]float64, outputSize)
|
82 |
+
for j := range wo[i] {
|
83 |
+
wo[i][j] = rand.Float64() - 0.5
|
84 |
+
}
|
85 |
+
}
|
86 |
|
87 |
+
bo := make([]float64, outputSize)
|
88 |
+
for i := range bo {
|
89 |
+
bo[i] = rand.Float64() - 0.5
|
90 |
+
}
|
|
|
|
|
91 |
|
92 |
+
nn.Wh = wh
|
93 |
+
nn.Bh = bh
|
94 |
+
nn.Wo = wo
|
95 |
+
nn.Bo = bo
|
|
|
96 |
}
|
nn/split.go
CHANGED
@@ -31,10 +31,10 @@ func (nn *NN) trainTestSplit() (dataframe.DataFrame, dataframe.DataFrame, datafr
|
|
31 |
// Create the train DataFrame using the trainIndices
|
32 |
train := nn.Df.Subset(trainIndices)
|
33 |
|
34 |
-
XTrain
|
35 |
-
YTrain
|
36 |
-
XTest
|
37 |
-
YTest
|
38 |
|
39 |
return XTrain, XTest, YTrain, YTest
|
40 |
}
|
|
|
31 |
// Create the train DataFrame using the trainIndices
|
32 |
train := nn.Df.Subset(trainIndices)
|
33 |
|
34 |
+
XTrain := train.Select(nn.Features)
|
35 |
+
YTrain := train.Select(nn.Target)
|
36 |
+
XTest := test.Select(nn.Features)
|
37 |
+
YTest := test.Select(nn.Target)
|
38 |
|
39 |
return XTrain, XTest, YTrain, YTest
|
40 |
}
|
server.go
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
package main
|
2 |
|
3 |
import (
|
|
|
|
|
4 |
"github.com/Jensen-holm/ml-from-scratch/nn"
|
5 |
"github.com/gofiber/fiber/v2"
|
6 |
)
|
@@ -12,13 +14,16 @@ func main() {
|
|
12 |
// that we will be able to validate.
|
13 |
app.Post("/neural-network", func(c *fiber.Ctx) error {
|
14 |
|
15 |
-
|
16 |
if err != nil {
|
17 |
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
18 |
"error": err,
|
19 |
})
|
20 |
}
|
21 |
|
|
|
|
|
|
|
22 |
return c.SendString("No error")
|
23 |
})
|
24 |
|
|
|
1 |
package main
|
2 |
|
3 |
import (
|
4 |
+
"fmt"
|
5 |
+
|
6 |
"github.com/Jensen-holm/ml-from-scratch/nn"
|
7 |
"github.com/gofiber/fiber/v2"
|
8 |
)
|
|
|
14 |
// that we will be able to validate.
|
15 |
app.Post("/neural-network", func(c *fiber.Ctx) error {
|
16 |
|
17 |
+
nn, err := nn.NewNN(c)
|
18 |
if err != nil {
|
19 |
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
20 |
"error": err,
|
21 |
})
|
22 |
}
|
23 |
|
24 |
+
nn.Train()
|
25 |
+
fmt.Println(nn.Wo)
|
26 |
+
|
27 |
return c.SendString("No error")
|
28 |
})
|
29 |
|