Returns whether the given tensor is trainable.
(tensor)
| 516 | |
| 517 | |
| 518 | def _is_trainable(tensor): |
| 519 | """Returns whether the given tensor is trainable.""" |
| 520 | if not gradients_util.IsTrainable(tensor): |
| 521 | return False |
| 522 | |
| 523 | # Special case: untrainable accumulator output. The gradients algorithm |
| 524 | # doesn't know about tensor lists of untrainable elements. In theory the |
| 525 | # tensor list gradient functions should return None as appropriate, but |
| 526 | # because we can't return None from the gradient function we filter out |
| 527 | # untrainable accumulator output here to avoid computing the gradient at all. |
| 528 | if tensor.op.type == "TensorListPopBack" and tensor.value_index == 0: |
| 529 | assert tensor.dtype == dtypes.variant |
| 530 | element_type = tensor.op.get_attr("element_dtype") |
| 531 | return gradients_util.IsTrainable(element_type) |
| 532 | |
| 533 | return True |
| 534 | |
| 535 | |
| 536 | # TODO(srbs): Pull this into common utils for cond_v2 and while_v2. |
no test coverage detected