Adds `bias` to `value`. This is (mostly) a special case of `tf.add` where `bias` is restricted to 1-D. Broadcasting is supported, so `value` may have any number of dimensions. Unlike `tf.add`, the type of `bias` is allowed to differ from `value` in the case where both types are quantized.
(value, bias, data_format=None, name=None)
| 2738 | |
| 2739 | @tf_export("nn.bias_add") |
| 2740 | def bias_add(value, bias, data_format=None, name=None): |
| 2741 | """Adds `bias` to `value`. |
| 2742 | |
| 2743 | This is (mostly) a special case of `tf.add` where `bias` is restricted to 1-D. |
| 2744 | Broadcasting is supported, so `value` may have any number of dimensions. |
| 2745 | Unlike `tf.add`, the type of `bias` is allowed to differ from `value` in the |
| 2746 | case where both types are quantized. |
| 2747 | |
| 2748 | Args: |
| 2749 | value: A `Tensor` with type `float`, `double`, `int64`, `int32`, `uint8`, |
| 2750 | `int16`, `int8`, `complex64`, or `complex128`. |
| 2751 | bias: A 1-D `Tensor` with size matching the channel dimension of `value`. |
| 2752 | Must be the same type as `value` unless `value` is a quantized type, |
| 2753 | in which case a different quantized type may be used. |
| 2754 | data_format: A string. 'N...C' and 'NC...' are supported. If `None` (the |
| 2755 | default) is specified then 'N..C' is assumed. |
| 2756 | name: A name for the operation (optional). |
| 2757 | |
| 2758 | Returns: |
| 2759 | A `Tensor` with the same type as `value`. |
| 2760 | |
| 2761 | Raises: |
| 2762 | ValueError if data format is unrecognized, if `value` has less than two |
| 2763 | dimensions when `data_format` is 'N..C'/`None` or `value` has less |
| 2764 | then three dimensions when `data_format` is `NC..`, if `bias` does not |
| 2765 | have exactly one dimension (is a vector), or if the size of `bias` |
| 2766 | does not match the size of the channel dimension of `value`. |
| 2767 | """ |
| 2768 | with ops.name_scope(name, "BiasAdd", [value, bias]) as name: |
| 2769 | if data_format is not None: |
| 2770 | if data_format.startswith("NC"): |
| 2771 | data_format = "NCHW" |
| 2772 | elif data_format.startswith("N") and data_format.endswith("C"): |
| 2773 | data_format = "NHWC" |
| 2774 | else: |
| 2775 | raise ValueError("data_format must be of the form `N...C` or `NC...`") |
| 2776 | |
| 2777 | if not context.executing_eagerly(): |
| 2778 | value = ops.convert_to_tensor(value, name="input") |
| 2779 | bias = ops.convert_to_tensor(bias, dtype=value.dtype, name="bias") |
| 2780 | |
| 2781 | # TODO(duncanriach): Implement deterministic functionality at CUDA kernel |
| 2782 | # level. |
| 2783 | if _tf_deterministic_ops(): |
| 2784 | # Note that this code does not implement the same error checks as the |
| 2785 | # pre-existing C++ ops. |
| 2786 | if data_format == 'NCHW': |
| 2787 | broadcast_shape_head = [1, array_ops.size(bias)] |
| 2788 | broadcast_shape_tail = array_ops.ones(array_ops.rank(value) - 2, |
| 2789 | dtype=dtypes.int32) |
| 2790 | broadcast_shape = array_ops.concat( |
| 2791 | [broadcast_shape_head, broadcast_shape_tail], 0) |
| 2792 | return math_ops.add( |
| 2793 | value, array_ops.reshape(bias, broadcast_shape), name=name) |
| 2794 | else: # data_format == 'NHWC' or data_format == None |
| 2795 | return math_ops.add(value, bias, name=name) |
| 2796 | else: |
| 2797 | return gen_nn_ops.bias_add(value, bias, data_format=data_format, |