Last active
November 12, 2020 13:07
-
-
Save mattn/674dbe01598ec37430598374daff01af to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"log" | |
. "gorgonia.org/gorgonia" | |
"gorgonia.org/tensor" | |
) | |
func main() { | |
g := NewGraph() | |
x := []float64{1, 3, 1, 3, 1, 3} | |
xA := tensor.New(tensor.Of(tensor.Float64), tensor.WithShape(3, 2), tensor.WithBacking(x)) | |
a := NewMatrix(g, tensor.Float64, WithName("a"), WithValue(xA)) | |
b := a.CloneTo(g) | |
b, err := Transpose(b, 1, 0) | |
if err != nil { | |
log.Fatal(err) | |
} | |
c, err := BatchedMatMul(b, a) | |
if err != nil { | |
log.Fatal(err) | |
} | |
m := NewTapeMachine(g) | |
if err := m.RunAll(); err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(c.Value()) | |
} |
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 ( | |
"fmt" | |
"log" | |
tf "github.com/tensorflow/tensorflow/tensorflow/go" | |
"github.com/tensorflow/tensorflow/tensorflow/go/op" | |
) | |
func fatalIf(err error) { | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func main() { | |
s := op.NewScope() | |
a := op.Const(s, [][]float32{{1.0, 3.0}, {1.0, 3.0}, {1.0, 3.0}}) | |
b := op.MatMul(s, op.Transpose(s, a, op.Const(s.SubScope("perm"), [2]int32{1, 0})), a) | |
graph, err := s.Finalize() | |
fatalIf(err) | |
session, err := tf.NewSession(graph, nil) | |
fatalIf(err) | |
defer session.Close() | |
output, err := session.Run(nil, []tf.Output{b}, nil) | |
fatalIf(err) | |
fmt.Println(output[0].Value().([][]float32)) | |
} |
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
import tensorflow as tf | |
import numpy as np | |
tf.compat.v1.disable_eager_execution() | |
a = np.array([[1.0, 3.0], [1.0, 3.0], [1.0, 3.0]], dtype=np.float32) | |
b = tf.matmul(tf.transpose(a), a) | |
ses = tf.compat.v1.Session() | |
print(ses.run(b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment