Created
August 17, 2017 16:14
-
-
Save sergiorykov/c3c859b72bdc21d72acdebba26197b88 to your computer and use it in GitHub Desktop.
ML gortex example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
g "github.com/vseledkin/gortex" | |
"fmt" | |
) | |
func main() { | |
X := float32(1) | |
Y := float32(1) | |
X1 := float32(0) | |
Y1 := float32(2) | |
Y_vector := g.Mat(1, 1) | |
Y_vector.W[0] = Y | |
X_vector := g.Mat(1, 1) | |
X_vector.W[0] = X | |
Y_vector1 := g.Mat(1, 1) | |
Y_vector1.W[0] = Y1 | |
X_vector1 := g.Mat(1, 1) | |
X_vector1.W[0] = X1 | |
parameter := g.Mat(1, 1) | |
parameter.W[0] = 0.5 | |
optimizer := g.NewOptimizer(g.OpOp{Method: g.SGD, LearningRate: 0.01, Momentum: 0.95}) | |
step := 0 | |
for true { | |
cost := float32(0) | |
graph := &g.Graph{NeedsBackprop: true} | |
result := graph.Mul(parameter, X_vector) | |
cost += graph.MSE(result, Y_vector) | |
graph.Backward() | |
graph1 := &g.Graph{NeedsBackprop: true} | |
result1 := graph1.Mul(parameter, X_vector1) | |
cost += graph1.MSE(result1, Y_vector1) | |
graph1.Backward() | |
optimizer.Step(map[string]*g.Matrix{"parameters": parameter}) | |
fmt.Printf("%d Current a*x=y %f*%f=%f abs error=%f\n", step, parameter.W[0], X_vector.W[0], result.W[0], cost) | |
step++ | |
if cost/2 < 1e-4 { // /2 because we have two samples | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment