Define a unary operation for labeled tensors. Args: op_name: string name of the TensorFlow op. elementwise_function: function to call to evaluate the op on a single tf.Tensor object. This function must accept two arguments: a tf.Tensor object, and an optional `name`. Retu
(op_name, elementwise_function)
| 1058 | @tc.returns(types.FunctionType) |
| 1059 | @tc.accepts(string_types, collections_abc.Callable) |
| 1060 | def define_unary_op(op_name, elementwise_function): |
| 1061 | """Define a unary operation for labeled tensors. |
| 1062 | |
| 1063 | Args: |
| 1064 | op_name: string name of the TensorFlow op. |
| 1065 | elementwise_function: function to call to evaluate the op on a single |
| 1066 | tf.Tensor object. This function must accept two arguments: a tf.Tensor |
| 1067 | object, and an optional `name`. |
| 1068 | |
| 1069 | Returns: |
| 1070 | Function defining the given op that acts on LabeledTensors. |
| 1071 | """ |
| 1072 | |
| 1073 | default_name = 'lt_%s' % op_name |
| 1074 | |
| 1075 | @tc.returns(LabeledTensor) |
| 1076 | @tc.accepts(LabeledTensorLike, tc.Optional(string_types)) |
| 1077 | def op(labeled_tensor, name=None): |
| 1078 | """LabeledTensor version of `tf.{op_name}`. |
| 1079 | |
| 1080 | See `tf.{op_name}` for full details. |
| 1081 | |
| 1082 | Args: |
| 1083 | labeled_tensor: Input tensor. |
| 1084 | name: Optional op name. |
| 1085 | |
| 1086 | Returns: |
| 1087 | A LabeledTensor with result of applying `tf.{op_name}` elementwise. |
| 1088 | """ |
| 1089 | with ops.name_scope(name, default_name, [labeled_tensor]) as scope: |
| 1090 | labeled_tensor = convert_to_labeled_tensor(labeled_tensor) |
| 1091 | result_tensor = elementwise_function(labeled_tensor.tensor, name=scope) |
| 1092 | return LabeledTensor(result_tensor, labeled_tensor.axes) |
| 1093 | |
| 1094 | op.__doc__ = op.__doc__.format(op_name=op_name) |
| 1095 | op.__name__ = op_name |
| 1096 | |
| 1097 | return op |
| 1098 | |
| 1099 | |
| 1100 | abs_function = define_unary_op('abs', math_ops.abs) |