-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dea27c8
commit 2c148e0
Showing
4 changed files
with
87 additions
and
29 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/nlpodyssey/spago/ag" | ||
"github.com/nlpodyssey/spago/mat" | ||
) | ||
|
||
func main() { | ||
// define the type of the elements in the tensors | ||
type T = float32 | ||
|
||
// create a new node of type variable with a scalar | ||
a := mat.Scalar(T(2.0), mat.WithGrad(true)) // create another node of type variable with a scalar | ||
b := mat.Scalar(T(5.0), mat.WithGrad(true)) // create an addition operator (the calculation is actually performed here) | ||
c := ag.Add(a, b) | ||
|
||
// print the result | ||
fmt.Printf("c = %v (float%d)\n", c.Value(), c.Value().Item().BitSize()) | ||
|
||
c.AccGrad(mat.Scalar(T(0.5))) | ||
|
||
if err := ag.Backward(c); err != nil { | ||
log.Fatalf("error during Backward(): %v", err) | ||
} | ||
|
||
fmt.Printf("ga = %v\n", a.Grad()) | ||
fmt.Printf("gb = %v\n", b.Grad()) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
. "github.com/nlpodyssey/spago/ag" | ||
"github.com/nlpodyssey/spago/mat" | ||
) | ||
|
||
func main() { | ||
x := mat.Scalar(-0.8) | ||
w := mat.Scalar(0.4) | ||
b := mat.Scalar(-0.2) | ||
|
||
y := Sigmoid(Add(Mul(w, x), b)) | ||
|
||
fmt.Printf("y = %0.3f\n", y.Value().Item()) | ||
} |
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