forked from buckhx/gobert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (52 loc) · 1.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
"os"
"github.com/buckhx/gobert/tokenize"
"github.com/buckhx/gobert/tokenize/vocab"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
)
func main() {
modelPath := os.Getenv("MODEL_PATH")
vocabPath := "" + "/vocab.txt"
voc, err := vocab.FromFile(vocabPath)
if err != nil {
panic(err)
}
tkz := tokenize.NewTokenizer(voc)
ff := tokenize.FeatureFactory{Tokenizer: tkz, SeqLen: 120}
f := ff.Feature("the dog is hairy.")
m, err := tf.LoadSavedModel(modelPath, []string{"bert-pretrained"}, nil)
if err != nil {
panic(err)
}
tids, err := tf.NewTensor([][]int32{f.TokenIDs})
if err != nil {
panic(err)
}
mask, err := tf.NewTensor([][]int32{f.Mask})
if err != nil {
panic(err)
}
sids, err := tf.NewTensor([][]int32{f.TypeIDs})
if err != nil {
panic(err)
}
res, err := m.Session.Run(
map[tf.Output]*tf.Tensor{
m.Graph.Operation("input_ids").Output(0): tids,
m.Graph.Operation("input_mask").Output(0): mask,
m.Graph.Operation("input_type_ids").Output(0): sids,
},
[]tf.Output{
m.Graph.Operation("embedding").Output(0),
},
nil,
)
if err != nil {
panic(err)
}
fmt.Println("DataType", res[0].DataType())
fmt.Println("Shape", res[0].Shape())
fmt.Println("Value", res[0].Value().([][][]float32))
}