Define a binary operation that broadcasts labeled tensors. Args: op_name: string name of the TensorFlow op. elementwise_function: function to call to evaluate the op on tf.Tensor objects. This function must accept three arguments: two tf.Tensor objects, and an optional `name
(op_name, elementwise_function)
| 1127 | @tc.returns(types.FunctionType) |
| 1128 | @tc.accepts(string_types, collections_abc.Callable) |
| 1129 | def define_binary_op(op_name, elementwise_function): |
| 1130 | """Define a binary operation that broadcasts labeled tensors. |
| 1131 | |
| 1132 | Args: |
| 1133 | op_name: string name of the TensorFlow op. |
| 1134 | elementwise_function: function to call to evaluate the op on tf.Tensor |
| 1135 | objects. This function must accept three arguments: two tf.Tensor objects, |
| 1136 | and an optional `name`. |
| 1137 | |
| 1138 | Returns: |
| 1139 | Function defining the given op that acts on LabeledTensors. |
| 1140 | """ |
| 1141 | |
| 1142 | default_name = 'lt_%s' % op_name |
| 1143 | |
| 1144 | @tc.returns(LabeledTensor) |
| 1145 | @tc.accepts(LabeledTensorLike, LabeledTensorLike, tc.Optional(string_types)) |
| 1146 | def op(labeled_tensor_0, labeled_tensor_1, name=None): |
| 1147 | """LabeledTensor version of `tf.{op_name}` with label based alignment. |
| 1148 | |
| 1149 | See `tf.{op_name}` for full details. |
| 1150 | |
| 1151 | Args: |
| 1152 | labeled_tensor_0: Input tensor. |
| 1153 | labeled_tensor_1: Input tensor. |
| 1154 | name: Optional op name. |
| 1155 | |
| 1156 | Returns: |
| 1157 | A LabeledTensor with result of applying `tf.{op_name}` elementwise. |
| 1158 | """ |
| 1159 | with ops.name_scope(name, default_name, |
| 1160 | [labeled_tensor_0, labeled_tensor_1]) as scope: |
| 1161 | |
| 1162 | align_0, align_1, broadcast_axes = align(labeled_tensor_0, |
| 1163 | labeled_tensor_1) |
| 1164 | |
| 1165 | tensor = elementwise_function(align_0.tensor, align_1.tensor, name=scope) |
| 1166 | |
| 1167 | return LabeledTensor(tensor, broadcast_axes) |
| 1168 | |
| 1169 | op.__doc__ = op.__doc__.format(op_name=op_name) |
| 1170 | op.__name__ = op_name |
| 1171 | |
| 1172 | return op |
| 1173 | |
| 1174 | |
| 1175 | add = define_binary_op('add', math_ops.add) |