()
| 185 | } |
| 186 | |
| 187 | func ExamplePartialRun() { |
| 188 | var ( |
| 189 | // Create a graph: a + 2 + 3 + b. |
| 190 | // |
| 191 | // Skipping error handling for brevity of this example. |
| 192 | // The 'op' package can be used to make graph construction code |
| 193 | // with error handling more succinct. |
| 194 | g = NewGraph() |
| 195 | a, _ = Placeholder(g, "a", Int32) |
| 196 | b, _ = Placeholder(g, "b", Int32) |
| 197 | two, _ = Const(g, "Two", int32(2)) |
| 198 | three, _ = Const(g, "Three", int32(3)) |
| 199 | |
| 200 | plus2, _ = Add(g, "plus2", a, two) // a + 2 |
| 201 | plus3, _ = Add(g, "plus3", plus2, three) // (a + 2) + 3 |
| 202 | plusB, _ = Add(g, "plusB", plus3, b) // ((a + 2) + 3) + b |
| 203 | |
| 204 | ) |
| 205 | sess, err := NewSession(g, nil) |
| 206 | if err != nil { |
| 207 | panic(err) |
| 208 | } |
| 209 | defer sess.Close() |
| 210 | |
| 211 | // All the feeds, fetches and targets for subsequent PartialRun.Run |
| 212 | // calls must be provided at setup. |
| 213 | pr, err := sess.NewPartialRun( |
| 214 | []Output{a, b}, |
| 215 | []Output{plus2, plusB}, |
| 216 | []*Operation{plus3.Op}, |
| 217 | ) |
| 218 | if err != nil { |
| 219 | panic(err) |
| 220 | } |
| 221 | |
| 222 | // Feed 'a=1', fetch 'plus2', and compute (but do not fetch) 'plus3'. |
| 223 | // Imagine this to be the forward pass of unsupervised neural network |
| 224 | // training of a robot. |
| 225 | val, _ := NewTensor(int32(1)) |
| 226 | fetches, err := pr.Run( |
| 227 | map[Output]*Tensor{a: val}, |
| 228 | []Output{plus2}, |
| 229 | nil) |
| 230 | if err != nil { |
| 231 | panic(err) |
| 232 | } |
| 233 | v1 := fetches[0].Value().(int32) |
| 234 | |
| 235 | // Now, feed 'b=4', fetch 'plusB=a+2+3+b' |
| 236 | // Imagine this to be the result of actuating the robot to determine |
| 237 | // the error produced by the current state of the neural network. |
| 238 | val, _ = NewTensor(int32(4)) |
| 239 | fetches, err = pr.Run( |
| 240 | map[Output]*Tensor{b: val}, |
| 241 | []Output{plusB}, |
| 242 | nil) |
| 243 | if err != nil { |
| 244 | panic(err) |
nothing calls this directly
no test coverage detected