Returns the set of tensors reachable from `inputs`. Stops if all targets have been found (target is optional). Only valid in Symbolic mode, not Eager mode. Args: inputs: List of tensors. targets: List of tensors. Returns: A set of tensors reachable from the inputs (includes t
(inputs, targets=None)
| 245 | |
| 246 | |
| 247 | def get_reachable_from_inputs(inputs, targets=None): |
| 248 | """Returns the set of tensors reachable from `inputs`. |
| 249 | |
| 250 | Stops if all targets have been found (target is optional). |
| 251 | |
| 252 | Only valid in Symbolic mode, not Eager mode. |
| 253 | |
| 254 | Args: |
| 255 | inputs: List of tensors. |
| 256 | targets: List of tensors. |
| 257 | |
| 258 | Returns: |
| 259 | A set of tensors reachable from the inputs (includes the inputs themselves). |
| 260 | """ |
| 261 | reachable = set(inputs) |
| 262 | if targets: |
| 263 | targets = set(targets) |
| 264 | queue = inputs[:] |
| 265 | |
| 266 | while queue: |
| 267 | x = queue.pop() |
| 268 | outputs = [] |
| 269 | try: |
| 270 | consumers = x.consumers() |
| 271 | except AttributeError: |
| 272 | # Case where x is a variable type |
| 273 | consumers = [x.op] |
| 274 | for z in consumers: |
| 275 | consumer_outputs = z.outputs |
| 276 | if consumer_outputs: # May be None |
| 277 | outputs += consumer_outputs |
| 278 | |
| 279 | for y in outputs: |
| 280 | if y not in reachable: |
| 281 | reachable.add(y) |
| 282 | queue.insert(0, y) |
| 283 | |
| 284 | if targets and targets.issubset(reachable): |
| 285 | return reachable |
| 286 | return reachable |