Spaces:
Sleeping
Sleeping
Commit
·
6b47160
1
Parent(s):
c4864dc
working through Train() function
Browse files- nn/main.go +38 -0
- nn/split.go +9 -7
nn/main.go
CHANGED
@@ -2,10 +2,12 @@ package nn
|
|
2 |
|
3 |
import (
|
4 |
"fmt"
|
|
|
5 |
"strings"
|
6 |
|
7 |
"github.com/go-gota/gota/dataframe"
|
8 |
"github.com/gofiber/fiber/v2"
|
|
|
9 |
)
|
10 |
|
11 |
type NN struct {
|
@@ -38,9 +40,45 @@ func NewNN(c *fiber.Ctx) (*NN, error) {
|
|
38 |
|
39 |
func (nn *NN) Train() {
|
40 |
// train test split the data
|
|
|
|
|
|
|
41 |
|
42 |
// iterate n times where n = nn.Epochs
|
43 |
// use backprop algorithm on each iteration
|
44 |
// to fit the model to the data
|
45 |
|
46 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
import (
|
4 |
"fmt"
|
5 |
+
"math/rand"
|
6 |
"strings"
|
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 {
|
|
|
40 |
|
41 |
func (nn *NN) Train() {
|
42 |
// train test split the data
|
43 |
+
XTrain, XTest, YTrain, YTest := nn.trainTestSplit()
|
44 |
+
|
45 |
+
weights, biases := nn.InitWnB()
|
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 |
+
|
53 |
+
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 for now
|
58 |
+
|
59 |
+
// Initialize weights and biases for the input layer to hidden layer
|
60 |
+
weightsInputHidden := mat.NewDense(inputSize, hiddenSize, nil)
|
61 |
+
weightsInputHidden.Apply(func(_, _ int, v float64) float64 {
|
62 |
+
// Randomly initialize weights with values between -1 and 1
|
63 |
+
return rand.Float64()*2 - 1
|
64 |
+
}, weightsInputHidden)
|
65 |
+
|
66 |
+
biasesHidden := mat.NewVecDense(hiddenSize, nil)
|
67 |
+
biasesHidden.Apply(func(_, _ int, v float64) float64 {
|
68 |
+
// Randomly initialize biases
|
69 |
+
return rand.Float64()
|
70 |
+
}, biasesHidden)
|
71 |
+
|
72 |
+
// Initialize weights and biases for the hidden layer to output layer
|
73 |
+
weightsHiddenOutput := mat.NewDense(hiddenSize, outputSize, nil)
|
74 |
+
weightsHiddenOutput.Apply(func(_, _ int, v float64) float64 {
|
75 |
+
// Randomly initialize weights with values between -1 and 1
|
76 |
+
return rand.Float64()*2 - 1
|
77 |
+
}, weightsHiddenOutput)
|
78 |
+
|
79 |
+
biasesOutput := mat.NewVecDense(outputSize, nil)
|
80 |
+
biasesOutput.Apply(func(_, _ int, v float64) float64 {
|
81 |
+
// Randomly initialize biases
|
82 |
+
return rand.Float64()
|
83 |
+
}, biasesOutput)
|
84 |
+
}
|
nn/split.go
CHANGED
@@ -3,11 +3,11 @@ package nn
|
|
3 |
import (
|
4 |
"math"
|
5 |
"math/rand"
|
6 |
-
)
|
7 |
|
8 |
-
|
|
|
9 |
|
10 |
-
func (nn *NN) trainTestSplit() {
|
11 |
// now we split the data into training
|
12 |
// and testing based on user specified
|
13 |
// nn.TestSize.
|
@@ -31,8 +31,10 @@ func (nn *NN) trainTestSplit() {
|
|
31 |
// Create the train DataFrame using the trainIndices
|
32 |
train := nn.Df.Subset(trainIndices)
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
38 |
}
|
|
|
3 |
import (
|
4 |
"math"
|
5 |
"math/rand"
|
|
|
6 |
|
7 |
+
"github.com/go-gota/gota/dataframe"
|
8 |
+
)
|
9 |
|
10 |
+
func (nn *NN) trainTestSplit() (dataframe.DataFrame, dataframe.DataFrame, dataframe.DataFrame, dataframe.DataFrame) {
|
11 |
// now we split the data into training
|
12 |
// and testing based on user specified
|
13 |
// nn.TestSize.
|
|
|
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 |
}
|