-
Notifications
You must be signed in to change notification settings - Fork 2
/
loadtf.go
193 lines (171 loc) · 4.23 KB
/
loadtf.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"fmt"
"os"
"bufio"
"strings"
"strconv"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
)
var pMap = map[int32]string{
0: "",
1 : ",",
2 : ".",
3 : "?",
4 : "!",
5 : ":",
6 : ";",
7 : "-",
}
const timesteps = 50
func main() {
// working directory must contain:
// model directory
modelDir := "m256/1"
// test file:
testFile := "test.txt"
// vocabulary
vocabFile = "vocabulary"
model, err := tf.LoadSavedModel(modelDir, []string{"serve"}, nil)
if err != nil {
fmt.Printf("Error loading saved model: %s\n", err.Error())
return
}
defer model.Session.Close()
fmt.Printf("Loaded model\n")
vocab, err := loadVocab(vocabFile)
if err != nil {
fmt.Printf("Error loading vocab: %s\n", err.Error())
return
}
fmt.Printf("Loaded vocab\n")
data, err := loadString(testFile)
if err != nil {
panic(err)
}
fmt.Printf("Loaded test data\n")
dataN := toNum(data, vocab)
fmt.Printf("Data: %s \n", toJSONString(dataN))
tensor, err := toTensor(dataN)
if err != nil {
panic(err)
}
result, err := model.Session.Run(
map[tf.Output]*tf.Tensor{
model.Graph.Operation("word_ids").Output(0): tensor, // Replace this with your input layer name
},
[]tf.Output{
model.Graph.Operation("lambda/strided_slice").Output(0), // Replace this with your output layer name
},
nil,
)
fmt.Printf("After run\n")
if err != nil {
fmt.Printf("Error running the session with input, err: %s\n", err.Error())
return
}
// fmt.Printf("Result value: %v \n", result[0].Value())
tn := result[0].Value().([][][]float32)
resN := toInt(tn[0])
fmt.Printf("Prediction: %v \n", resN)
resTxt := toResult(data, resN)
fmt.Printf("Result: %v \n", resTxt)
}
func loadString(fn string) ([]string, error) {
file, err := os.Open(fn)
if (err != nil){
return nil, err
}
defer file.Close()
result := make([]string, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
s := scanner.Text()
strs := strings.Split(s, " ")
for _, s := range strs {
s = strings.TrimSpace(s)
if (s != ""){
result = append(result, s)
}
}
}
return result, nil
}
func toNum(strs []string, vocab map[string]int32) ([]int32) {
l := len(strs)
result := make([]int32, 0)
for i := 0; i < timesteps; i++ {
s := strs[i % l]
k, f := vocab[s]
if !f {
k = vocab["<UNK>"]
}
result = append(result, k)
}
return result
}
func toResult(strs []string, res []int32) string {
to := len(strs)
result := ""
for i, v := range res {
if (i < to){
result = result + strs[i]
p, _ := pMap[v]
if (p != ""){
result = result + p
}
result = result + " "
}
}
return result
}
func toJSONString(inData []int32) string {
result := ""
for _, v := range inData {
result = result + strconv.Itoa(int(v)) + ","
}
return "[" + result + "]"
}
func toInt(res[][]float32) ([]int32) {
result := make([]int32, 0)
for _, s := range res {
result = append(result, argMax(s))
}
return result
}
func toTensor(inData []int32) (*tf.Tensor, error) {
data := [1][timesteps]int32{}
for i, v := range inData{
data[0][i] = v
}
return tf.NewTensor(data)
}
func loadVocab(vocabFile string) (map[string]int32, error) {
file, err := os.Open(vocabFile)
if (err != nil){
return nil, err
}
defer file.Close()
result := make(map[string]int32)
var i int32
i = 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
s := scanner.Text()
result[s] = i
i++
}
return result, nil
}
func argMax(tn []float32) int32 {
m := float32(-1.0)
var r int32
r = 0
for i, v := range tn{
if (v > m){
r = int32(i)
m = v
}
}
return r
}