Spaces:
Build error
Build error
Commit ·
59ae052
1
Parent(s): 6b47160
initializing weights and biases
Browse filesFiles changed (4)
- example/main.go+2
- nn/main.go+38-26
- nn/split.go+4-4
- server.go+6-1
15 unmodified lines16171819202120 unmodified lines42434445464715 unmodified linesLearningRate float64 `json:"learning_rate"`HiddenSize int `json:"hidden_size"`ActivationFunc string `json:"activation"`}func main() {20 unmodified linesLearningRate: 0.01,HiddenSize: 12,ActivationFunc: "tanh",}jsonPayload, err := json.Marshal(payload)15 unmodified lines1617181920212220 unmodified lines4344454647484915 unmodified linesLearningRate float64 `json:"learning_rate"`HiddenSize int `json:"hidden_size"`ActivationFunc string `json:"activation"`TestSize float64 `json:"test_size"`}func main() {20 unmodified linesLearningRate: 0.01,HiddenSize: 12,ActivationFunc: "tanh",TestSize: 0.3,}jsonPayload, err := json.Marshal(payload)
6 unmodified lines7891011121311 unmodified lines2526272829309 unmodified lines404142434445464748495051521 unmodified line545556575859606162636465666768697071727374757677787980818283846 unmodified lines"github.com/go-gota/gota/dataframe""github.com/gofiber/fiber/v2""gonum.org/v1/gonum/mat")type NN struct {11 unmodified linesYTrain dataframe.DataFrameXTest dataframe.DataFrameYTest dataframe.DataFrame}func NewNN(c *fiber.Ctx) (*NN, error) {9 unmodified linesfunc (nn *NN) Train() {// train test split the dataXTrain, XTest, YTrain, YTest := nn.trainTestSplit()weights, biases := nn.InitWnB()// iterate n times where n = nn.Epochs// use backprop algorithm on each iteration// to fit the model to the data}1 unmodified line// randomly initialize weights and biases to startinputSize := len(nn.Features)hiddenSize := nn.HiddenSizeoutputSize := 1 // only predicting one thing for now// Initialize weights and biases for the input layer to hidden layerweightsInputHidden := mat.NewDense(inputSize, hiddenSize, nil)weightsInputHidden.Apply(func(_, _ int, v float64) float64 {// Randomly initialize weights with values between -1 and 1return rand.Float64()*2 - 1}, weightsInputHidden)biasesHidden := mat.NewVecDense(hiddenSize, nil)biasesHidden.Apply(func(_, _ int, v float64) float64 {// Randomly initialize biasesreturn rand.Float64()}, biasesHidden)// Initialize weights and biases for the hidden layer to output layerweightsHiddenOutput := mat.NewDense(hiddenSize, outputSize, nil)weightsHiddenOutput.Apply(func(_, _ int, v float64) float64 {// Randomly initialize weights with values between -1 and 1return rand.Float64()*2 - 1}, weightsHiddenOutput)biasesOutput := mat.NewVecDense(outputSize, nil)biasesOutput.Apply(func(_, _ int, v float64) float64 {// Randomly initialize biasesreturn rand.Float64()}, biasesOutput)}6 unmodified lines78910111211 unmodified lines242526272829303132339 unmodified lines4344454647484950515253545556571 unmodified line59606162636465666768697071727374757677787980818283848586878889909192939495966 unmodified lines"github.com/go-gota/gota/dataframe""github.com/gofiber/fiber/v2")type NN struct {11 unmodified linesYTrain dataframe.DataFrameXTest dataframe.DataFrameYTest dataframe.DataFrameWh [][]float64Bh []float64Wo [][]float64Bo []float64}func NewNN(c *fiber.Ctx) (*NN, error) {9 unmodified linesfunc (nn *NN) Train() {// train test split the data_, _, _, _ = nn.trainTestSplit()nn.InitWnB()// iterate n times where n = nn.Epochs// use backprop algorithm on each iteration// to fit the model to the datafor i := 0; i < nn.Epochs; i++ {}}1 unmodified line// randomly initialize weights and biases to startinputSize := len(nn.Features)hiddenSize := nn.HiddenSizeoutputSize := 1 // only predicting one thing// input hidden layer weightswh := make([][]float64, inputSize)for i := range wh {wh[i] = make([]float64, hiddenSize)for j := range wh[i] {wh[i][j] = rand.Float64() - 0.5}}bh := make([]float64, hiddenSize)for i := range bh {bh[i] = rand.Float64() - 0.5}// initialize weights and biases for hidden -> output layerwo := make([][]float64, hiddenSize)for i := range wo {wo[i] = make([]float64, outputSize)for j := range wo[i] {wo[i][j] = rand.Float64() - 0.5}}bo := make([]float64, outputSize)for i := range bo {bo[i] = rand.Float64() - 0.5}nn.Wh = whnn.Bh = bhnn.Wo = wonn.Bo = bo}
30 unmodified lines3132333435363738394030 unmodified lines// Create the train DataFrame using the trainIndicestrain := nn.Df.Subset(trainIndices)XTrain = train.Select(nn.Features)YTrain = train.Select(nn.Target)XTest = test.Select(nn.Features)YTest = test.Select(nn.Target)return XTrain, XTest, YTrain, YTest}30 unmodified lines3132333435363738394030 unmodified lines// Create the train DataFrame using the trainIndicestrain := nn.Df.Subset(trainIndices)XTrain := train.Select(nn.Features)YTrain := train.Select(nn.Target)XTest := test.Select(nn.Features)YTest := test.Select(nn.Target)return XTrain, XTest, YTrain, YTest}
1234565 unmodified lines12131415161718192021222324package mainimport ("github.com/Jensen-holm/ml-from-scratch/nn""github.com/gofiber/fiber/v2")5 unmodified lines// that we will be able to validate.app.Post("/neural-network", func(c *fiber.Ctx) error {_, err := nn.NewNN(c)if err != nil {return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err,})}return c.SendString("No error")})123456785 unmodified lines14151617181920212223242526272829package mainimport ("fmt""github.com/Jensen-holm/ml-from-scratch/nn""github.com/gofiber/fiber/v2")5 unmodified lines// that we will be able to validate.app.Post("/neural-network", func(c *fiber.Ctx) error {nn, err := nn.NewNN(c)if err != nil {return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err,})}nn.Train()fmt.Println(nn.Wo)return c.SendString("No error")})
example/main.go+2
nn/main.go+38-26
nn/split.go+4-4
server.go+6-1