forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
194 lines (159 loc) · 5.22 KB
/
example_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
package tensorflow_test
import (
"fmt"
"io/ioutil"
"github.com/tensorflow/tensorflow/tensorflow/contrib/go"
)
func ExampleGraph_Op() {
var out []*tensorflow.Tensor
additions := 10
inputSlice1 := []int32{1, 2, 3, 4}
inputSlice2 := []int32{5, 6, 7, 8}
graph := tensorflow.NewGraph()
input1, _ := graph.Variable("input1", inputSlice1)
input2, _ := graph.Constant("input2", inputSlice2)
add, _ := graph.Op("Add", "add_tensors", []*tensorflow.GraphNode{input1, input2}, "", map[string]interface{}{})
graph.Op("Assign", "assign_inp1", []*tensorflow.GraphNode{input1, add}, "", map[string]interface{}{})
s, _ := tensorflow.NewSession()
s.ExtendAndInitializeAllVariables(graph)
for i := 0; i < additions; i++ {
out, _ = s.Run(nil, []string{"input1"}, []string{"assign_inp1"})
}
for i := 0; i < len(inputSlice1); i++ {
val, _ := out[0].GetVal(int64(i))
fmt.Printf("The result of: %d + (%d*%d) is: %d\n", inputSlice1[i], inputSlice2[i], additions, val)
}
}
func ExampleNewTensor_slice() {
tensorflow.NewTensor([][]int64{
{1, 2, 3, 4},
{5, 6, 7, 8},
})
}
func ExampleNewTensor_scalar() {
tensorflow.NewTensor("Hello TensorFlow")
}
func ExampleNewGraphFromString() {
graph, err := tensorflow.NewGraphFromString(`
node {
name: "output"
op: "Const"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_FLOAT
tensor_shape {
}
float_val: 1.5
}
}
}
}
version: 5`)
if err != nil {
return
}
fmt.Print(graph)
}
func ExampleGraph_Constant() {
graph := tensorflow.NewGraph()
// Add a scalar string node named 'const1' to the Graph.
graph.Constant("const1", "this is a test...")
// Add bidimensional Constant named 'const2' to the Graph.
graph.Constant("const2", [][]int64{
{1, 2},
{3, 4},
})
}
func ExampleGraph_Placeholder() {
graph := tensorflow.NewGraph()
// Add Placeholder named 'input1' that must allocate a three element
// DTInt32 tensor.
graph.Placeholder("input1", tensorflow.DTInt32, []int64{3})
}
func ExampleGraph_Variable() {
var out []*tensorflow.Tensor
graph := tensorflow.NewGraph()
// Create Variable that will be used as input and also as storage of
// the result after every execution.
input1, _ := graph.Variable("input1", []int32{1, 2, 3, 4})
input2, _ := graph.Constant("input2", []int32{5, 6, 7, 8})
// Add the two inputs.
add, _ := graph.Op("Add", "add_tensors", []*tensorflow.GraphNode{input1, input2}, "", map[string]interface{}{})
// Store the result on input1 Varable.
graph.Op("Assign", "assign_inp1", []*tensorflow.GraphNode{input1, add}, "", map[string]interface{}{})
s, _ := tensorflow.NewSession()
// Initialize all the Variables in memory, in this case only the
// 'input1' Variable.
s.ExtendAndInitializeAllVariables(graph)
// Run ten times the 'assign_inp1"' that will run also the 'Add'
// operation since it input depends on the result of the 'Add'
// operation.
// The variable 'input1' will be returned and printed on each
// execution.
for i := 0; i < 10; i++ {
out, _ = s.Run(nil, []string{"input1"}, []string{"assign_inp1"})
fmt.Println(out[0].Int32s())
}
}
func ExampleSession_ExtendAndInitializeAllVariables() {
graph := tensorflow.NewGraph()
// Create Variable that will be initialized with the values []int32{1, 2, 3, 4} .
graph.Variable("input1", []int32{1, 2, 3, 4})
s, _ := tensorflow.NewSession()
// Initialize all the Variables in memory, on this case only the
// 'input1' variable.
s.ExtendAndInitializeAllVariables(graph)
}
func ExampleSession_ExtendGraph() {
graph := tensorflow.NewGraph()
// Add a Placeholder named 'input1' that must allocate a three element
// DTInt32 tensor.
graph.Placeholder("placeholder", tensorflow.DTInt32, []int64{3})
// Create the Session and extend the Graph on it.
s, _ := tensorflow.NewSession()
s.ExtendGraph(graph)
}
func ExampleNewGraphFromBuffer() {
// Load the Graph from from a file containing a serialized
// Graph.
b, _ := ioutil.ReadFile("/tmp/graph/test_graph.pb")
graph, _ := tensorflow.NewGraphFromBuffer(b)
// Create the Session and extend the Graph on it.
s, _ := tensorflow.NewSession()
s.ExtendGraph(graph)
}
func ExampleSession_Run() {
graph := tensorflow.NewGraph()
input1, _ := graph.Variable("input1", []int32{1, 2, 3, 4})
input2, _ := graph.Constant("input2", []int32{5, 6, 7, 8})
add, _ := graph.Op("Add", "add_tensors", []*tensorflow.GraphNode{input1, input2}, "", map[string]interface{}{})
graph.Op("Assign", "assign_inp1", []*tensorflow.GraphNode{input1, add}, "", map[string]interface{}{})
s, _ := tensorflow.NewSession()
s.ExtendAndInitializeAllVariables(graph)
out, _ := s.Run(nil, []string{"input1"}, []string{"assign_inp1"})
// The first of the output corresponds to the node 'input1' specified
// on the second param.
fmt.Println(out[0])
}
func ExampleNewTensorWithShape() {
// Create Tensor with a single dimension of 3.
t2, _ := tensorflow.NewTensorWithShape([]int64{3}, []int64{3, 4, 5})
fmt.Println(t2.Int64s())
}
func ExampleTensor_GetVal() {
t, _ := tensorflow.NewTensor([][]int64{
{1, 2, 3, 4},
{5, 6, 7, 8},
})
// Print the number 8 that is in the second position of the first
// dimension and the third of the second dimension.
fmt.Println(t.GetVal(1, 3))
}