Forwards `data` to an output determined by `pred`. If `pred` is false, the `data` input is forwarded to the first output. Otherwise, the data goes to the second output. This op handles `Tensor`s and `IndexedSlices`. Args: data: The tensor to be forwarded to the appropriate output.
(data, pred, dtype=None, name=None)
| 284 | |
| 285 | |
| 286 | def switch(data, pred, dtype=None, name=None): |
| 287 | """Forwards `data` to an output determined by `pred`. |
| 288 | |
| 289 | If `pred` is false, the `data` input is forwarded to the first output. |
| 290 | Otherwise, the data goes to the second output. |
| 291 | |
| 292 | This op handles `Tensor`s and `IndexedSlices`. |
| 293 | |
| 294 | Args: |
| 295 | data: The tensor to be forwarded to the appropriate output. |
| 296 | pred: A scalar that specifies which output port will receive data. |
| 297 | dtype: Optional element type for the returned tensor. If missing, the type |
| 298 | is inferred from the type of `value`. |
| 299 | name: A name for this operation (optional). |
| 300 | |
| 301 | Returns: |
| 302 | `(output_false, output_true)`: If `pred` is true, data will be forwarded |
| 303 | to `output_true`, otherwise it goes to `output_false`. |
| 304 | """ |
| 305 | with ops.name_scope(name, "Switch", [data, pred]) as name: |
| 306 | data = ops.internal_convert_to_tensor_or_composite( |
| 307 | data, dtype=dtype, name="data", as_ref=True) |
| 308 | pred = ops.convert_to_tensor(pred, name="pred") |
| 309 | if isinstance(data, ops.Tensor): |
| 310 | return gen_control_flow_ops.switch(data, pred, name=name) |
| 311 | else: |
| 312 | if not isinstance(data, composite_tensor.CompositeTensor): |
| 313 | raise TypeError("Type %s not supported" % type(data)) |
| 314 | tensors = nest.flatten(data, expand_composites=True) |
| 315 | mapped = [gen_control_flow_ops.switch(tensor, pred) for tensor in tensors] |
| 316 | mapped_f, mapped_t = zip(*mapped) |
| 317 | return (nest.pack_sequence_as(data, mapped_f, expand_composites=True), |
| 318 | nest.pack_sequence_as(data, mapped_t, expand_composites=True)) |
| 319 | |
| 320 | |
| 321 | def _SwitchRefOrTensor(data, pred, name="Switch"): |
no test coverage detected