Extracts a slice from a tensor. This operation extracts a slice of size `size` from a tensor `input_` starting at the location specified by `begin`. The slice `size` is represented as a tensor shape, where `size[i]` is the number of elements of the 'i'th dimension of `input_` that you want
(input_, begin, size, name=None)
| 805 | # pylint: disable=undefined-variable,protected-access,redefined-outer-name |
| 806 | @tf_export("slice") |
| 807 | def slice(input_, begin, size, name=None): |
| 808 | # pylint: disable=redefined-builtin |
| 809 | """Extracts a slice from a tensor. |
| 810 | |
| 811 | This operation extracts a slice of size `size` from a tensor `input_` starting |
| 812 | at the location specified by `begin`. The slice `size` is represented as a |
| 813 | tensor shape, where `size[i]` is the number of elements of the 'i'th dimension |
| 814 | of `input_` that you want to slice. The starting location (`begin`) for the |
| 815 | slice is represented as an offset in each dimension of `input_`. In other |
| 816 | words, `begin[i]` is the offset into the i'th dimension of `input_` that you |
| 817 | want to slice from. |
| 818 | |
| 819 | Note that `tf.Tensor.__getitem__` is typically a more pythonic way to |
| 820 | perform slices, as it allows you to write `foo[3:7, :-2]` instead of |
| 821 | `tf.slice(foo, [3, 0], [4, foo.get_shape()[1]-2])`. |
| 822 | |
| 823 | `begin` is zero-based; `size` is one-based. If `size[i]` is -1, |
| 824 | all remaining elements in dimension i are included in the |
| 825 | slice. In other words, this is equivalent to setting: |
| 826 | |
| 827 | `size[i] = input_.dim_size(i) - begin[i]` |
| 828 | |
| 829 | This operation requires that: |
| 830 | |
| 831 | `0 <= begin[i] <= begin[i] + size[i] <= Di for i in [0, n]` |
| 832 | |
| 833 | For example: |
| 834 | |
| 835 | ```python |
| 836 | t = tf.constant([[[1, 1, 1], [2, 2, 2]], |
| 837 | [[3, 3, 3], [4, 4, 4]], |
| 838 | [[5, 5, 5], [6, 6, 6]]]) |
| 839 | tf.slice(t, [1, 0, 0], [1, 1, 3]) # [[[3, 3, 3]]] |
| 840 | tf.slice(t, [1, 0, 0], [1, 2, 3]) # [[[3, 3, 3], |
| 841 | # [4, 4, 4]]] |
| 842 | tf.slice(t, [1, 0, 0], [2, 1, 3]) # [[[3, 3, 3]], |
| 843 | # [[5, 5, 5]]] |
| 844 | ``` |
| 845 | |
| 846 | Args: |
| 847 | input_: A `Tensor`. |
| 848 | begin: An `int32` or `int64` `Tensor`. |
| 849 | size: An `int32` or `int64` `Tensor`. |
| 850 | name: A name for the operation (optional). |
| 851 | |
| 852 | Returns: |
| 853 | A `Tensor` the same type as `input_`. |
| 854 | """ |
| 855 | return gen_array_ops._slice(input_, begin, size, name=name) |
| 856 | |
| 857 | |
| 858 | # pylint: disable=invalid-name |
no outgoing calls