Helper method that subscribes a single tensor to a list of side_effects. Args: tensor: `tf.Tensor` side_effects: List of side_effect functions see subscribe for details. control_cache: `_ControlOutputCache` helper to get control_outputs faster. Returns: The modified replacement
(tensor, side_effects, control_cache)
| 106 | |
| 107 | |
| 108 | def _subscribe_new(tensor, side_effects, control_cache): |
| 109 | """Helper method that subscribes a single tensor to a list of side_effects. |
| 110 | |
| 111 | Args: |
| 112 | tensor: `tf.Tensor` |
| 113 | side_effects: List of side_effect functions see subscribe for details. |
| 114 | control_cache: `_ControlOutputCache` helper to get control_outputs faster. |
| 115 | |
| 116 | Returns: |
| 117 | The modified replacement to the passed in tensor which triggers the side |
| 118 | effects. |
| 119 | """ |
| 120 | update_input = [] |
| 121 | for consumer_op in list(tensor.consumers()): # explicit copy |
| 122 | update_input.append((consumer_op, list(consumer_op.inputs).index(tensor))) |
| 123 | |
| 124 | update_control_input = control_cache.get_control_outputs(tensor.op) |
| 125 | |
| 126 | # Trailing slash on name scope to replace the scope. |
| 127 | name_scope = tensor.op.name + '/subscription/' |
| 128 | with ops.name_scope(name_scope): |
| 129 | outs = [] |
| 130 | for s in side_effects: |
| 131 | outs += s(tensor) |
| 132 | |
| 133 | with ops.control_dependencies(outs): |
| 134 | out = array_ops.identity(tensor) |
| 135 | |
| 136 | for consumer_op, index in update_input: |
| 137 | consumer_op._update_input(index, out) # pylint: disable=protected-access |
| 138 | |
| 139 | for consumer_op in update_control_input: |
| 140 | # If an op has more than one output and two or more of its output tensors |
| 141 | # are subscribed at the same time, we remove the control dependency from |
| 142 | # the original op only once and we add the dependencies to all the |
| 143 | # new identities. |
| 144 | new_control_inputs = consumer_op.control_inputs |
| 145 | if tensor.op in new_control_inputs: |
| 146 | new_control_inputs.remove(tensor.op) |
| 147 | new_control_inputs.append(out.op) |
| 148 | # pylint: disable=protected-access |
| 149 | consumer_op._remove_all_control_inputs() |
| 150 | consumer_op._add_control_inputs(new_control_inputs) |
| 151 | # pylint: enable=protected-access |
| 152 | return out |
| 153 | |
| 154 | |
| 155 | def _subscribe_extend(tensor, side_effects): |
no test coverage detected