-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtfvariable_test.go
271 lines (236 loc) · 6.78 KB
/
tfvariable_test.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package tfutil
import (
"fmt"
"testing"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
"github.com/tensorflow/tensorflow/tensorflow/go/op"
)
//测试tf.ArgMax,返回行或列的最大值下标向量
func TestArgMax(t *testing.T) {
// import tensorflow as tf
// a = tf.constant( [[1, 2, 3], [4, 5, 6]] )
// #a = tf.constant( [[2, 2, 2], [2, 2, 2]] )
// b=tf.argmax(input=a,axis=0)
// c=tf.argmax(input=a,dimension=1) #此处用dimesion或用axis是一样的
// # Start tf session
// sess = tf.Session()
// print(sess.run(b))
// #[1 1 1]
// print(sess.run(c))
// #[2 2]
var (
root = op.NewScope()
axisRow = op.Const(root.SubScope("input"), int32(1)) //axis:0表示按列,1表示按行
axisColum = op.Const(root.SubScope("input"), int32(0))
)
testdata := [][][]int32{{{1, 2, 3}, {4, 5, 6}}, {{2, 2, 2}, {2, 2, 2}}}
rowOutputlist := make([]tf.Output, 0)
columOutputlist := make([]tf.Output, 0)
fmt.Println("orig testdata:")
for i, test := range testdata {
fmt.Println(" ", i, " testdata:", test)
x := op.ArgMax(root.SubScope("input"), op.Const(root.SubScope("input"), test), axisRow)
rowOutputlist = append(rowOutputlist, x)
y := op.ArgMax(root.SubScope("input"), op.Const(root.SubScope("input"), test), axisColum)
columOutputlist = append(columOutputlist, y)
}
graph, err := root.Finalize()
if err != nil {
t.Fatal(err)
}
sess, err := tf.NewSession(graph, nil)
if err != nil {
t.Fatal(err)
}
fmt.Println("ArgMax by row:")
if output, err := sess.Run(nil, rowOutputlist, nil); err != nil {
panic(err)
} else {
for i, j := range output {
fmt.Println(" ", i, " value:", j.Value())
}
}
fmt.Println("ArgMax by colum:")
if output, err := sess.Run(nil, columOutputlist, nil); err != nil {
panic(err)
} else {
for i, j := range output {
fmt.Println(" ", i, " value:", j.Value())
}
}
}
//测试golang variable
func TestVariable(t *testing.T) {
// import tensorflow as tf
// a = tf.Variable(tf.random_normal([2,2],seed=1))
// b = tf.Variable(tf.truncated_normal([2,2],seed=2))
// init = tf.global_variables_initializer()
// with tf.Session() as sess:
// sess.run(init)
// print(sess.run(a))
// print(sess.run(b))
// #[[-0.8113182 1.4845988 ] [ 0.06532937 -2.4427042 ]]
// #[[-0.85811085 -0.19662298] [ 0.13895045 -1.2212768 ]]
var (
root = op.NewScope()
)
testdata := []tf.Output{
op.Const(root.SubScope("input"), []int32{2, 2}),
op.Const(root.SubScope("input"), [][]int32{{1, 2, 3}, {4, 5, 6}}),
}
var vars = make([]*Variable, 0)
for i, j := range testdata {
v := NewVariable(root, &j, fmt.Sprintf("init_%d", i))
vars = append(vars, v)
}
graph, err := root.Finalize()
if err != nil {
t.Fatal(err)
}
sess, err := tf.NewSession(graph, nil)
if err != nil {
t.Fatal(err)
}
fmt.Println("init variables:")
for _, j := range vars {
if j != nil {
j.Init(sess)
}
}
fmt.Println("variable init values:")
if output, err := sess.Run(nil, testdata, nil); err != nil {
panic(err)
} else {
for i, j := range output {
fmt.Println(" ", i, " value:", j.Value())
}
}
fmt.Println("read variables values:")
for i, j := range vars {
if j != nil {
fmt.Println(" ", i, ":", j.Get(sess))
}
}
}
//测试tf.TruncatedNormal,返回行或列的最大值下标向量
func TestTruncatedNormal(t *testing.T) {
// import tensorflow as tf
// a = tf.Variable(tf.random_normal([2,2],seed=1))
// b = tf.Variable(tf.truncated_normal([2,2],seed=2))
// init = tf.global_variables_initializer()
// with tf.Session() as sess:
// sess.run(init)
// print(sess.run(a))
// print(sess.run(b))
// #[[-0.8113182 1.4845988 ] [ 0.06532937 -2.4427042 ]]
// #[[-0.85811085 -0.19662298] [ 0.13895045 -1.2212768 ]]
var (
root = op.NewScope()
)
testdataWithoutSeed := []tf.Output{
op.RandomStandardNormal(root.SubScope("input"),
op.Const(root.SubScope("input"), []int32{2, 2}),
tf.Float /* , op.RandomStandardNormalSeed(1) */),
op.RandomStandardNormal(root.SubScope("input"),
op.Const(root.SubScope("input"), []int32{2, 2}),
tf.Float),
}
testdataWithSeed := []tf.Output{
op.RandomStandardNormal(root.SubScope("input"),
op.Const(root.SubScope("input"), []int32{2, 2}),
tf.Float, op.RandomStandardNormalSeed(1)),
op.RandomStandardNormal(root.SubScope("input"),
op.Const(root.SubScope("input"), []int32{2, 2}),
tf.Float, op.RandomStandardNormalSeed(2)),
op.RandomStandardNormal(root.SubScope("input"),
op.Const(root.SubScope("input"), []int32{2, 2}),
tf.Float, op.RandomStandardNormalSeed(3)),
}
graph, err := root.Finalize()
if err != nil {
t.Fatal(err)
}
sess, err := tf.NewSession(graph, nil)
if err != nil {
t.Fatal(err)
}
fmt.Println("RandomStandardNormal values without seeds:")
if output, err := sess.Run(nil, testdataWithoutSeed, nil); err != nil {
panic(err)
} else {
for i, j := range output {
fmt.Println(" ", i, " value:", j.Value())
}
}
fmt.Println("RandomStandardNormal values with seeds:")
if output, err := sess.Run(nil, testdataWithSeed, nil); err != nil {
panic(err)
} else {
for i, j := range output {
fmt.Println(" ", i, " value:", j.Value())
}
}
}
func TestVariable_Get(t *testing.T) {
var (
root = op.NewScope()
)
a := op.Const(root.SubScope("input"), [][]int32{{2, 2}, {2, 2}})
b := op.Const(root.SubScope("input"), [][]int32{{3, 3}, {3, 3}})
aa := NewVariable(root, &a, "aa")
bb := NewVariable(root, &b, "bb")
fmt.Println(aa.Handle().Op.Name(), bb.Handle().Op.Name())
A := op.Placeholder(root.SubScope("input"), tf.Int32 /*, op.PlaceholderShape(tf.MakeShape(2, 2))*/)
B := op.Placeholder(root.SubScope("input"), tf.Int32 /* , op.PlaceholderShape(tf.MakeShape(2, 1)) */)
product1 := op.MatMul(root, A, B)
graph, err := root.Finalize()
if err != nil {
t.Fatal(err)
}
sess, err := tf.NewSession(graph, nil)
if err != nil {
t.Fatal(err)
}
defer sess.Close()
if aa.Init(sess) && bb.Init(sess) {
fmt.Println(aa.Get(sess).Value(), bb.Get(sess).Value())
}
fmt.Println("values:")
if output, err := sess.Run(nil, []tf.Output{a, b}, nil); err != nil {
panic(err)
} else {
for i, j := range output {
fmt.Println(" ", i, " value:", j.Value())
}
}
fmt.Println("matual values:")
if output, err := sess.Run(
map[tf.Output]*tf.Tensor{
A: aa.Get(sess),
B: bb.Get(sess),
}, []tf.Output{product1}, nil); err != nil {
panic(err)
} else {
for i, j := range output {
fmt.Println(" ", i, " value:", j.Value())
}
}
// type args struct {
// sess *tf.Session
// }
// tests := []struct {
// name string
// v *Variable
// args args
// want tf.Output
// }{
// // TODO: Add test cases.
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// if got := tt.v.Handle(tt.args.sess); !reflect.DeepEqual(got, tt.want) {
// t.Errorf("Variable.Handle() = %v, want %v", got, tt.want)
// }
// })
// }
}