mindspore.dataset.text.SlidingWindow
- class mindspore.dataset.text.SlidingWindow(width, axis=0)[source]
Construct a tensor from given data (only support 1-D for now), where each element in the dimension axis is a slice of data starting at the corresponding position, with a specified width.
- Parameters
- Raises
TypeError – If width is not of type int.
ValueError – If value of width is not positive.
TypeError – If axis is not of type int.
- Supported Platforms:
CPU
Examples
>>> import mindspore.dataset as ds >>> import mindspore.dataset.text as text >>> >>> # Use the transform in dataset pipeline mode >>> numpy_slices_dataset = ds.NumpySlicesDataset(data=[[1, 2, 3, 4, 5]], column_names=["col1"]) >>> # Data before >>> # | col1 | >>> # +--------------+ >>> # | [[1, 2, 3, 4, 5]] | >>> # +--------------+ >>> numpy_slices_dataset = numpy_slices_dataset.map(operations=text.SlidingWindow(3, 0)) >>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True): ... print(item["col1"]) [[1 2 3] [2 3 4] [3 4 5]] >>> # Data after >>> # | col1 | >>> # +--------------+ >>> # | [[1, 2, 3], | >>> # | [2, 3, 4], | >>> # | [3, 4, 5]] | >>> # +--------------+ >>> >>> # Use the transform in eager mode >>> data = ["happy", "birthday", "to", "you"] >>> output = text.SlidingWindow(2, 0)(data) >>> print(output) [['happy' 'birthday'] ['birthday' 'to'] ['to' 'you']]
- Tutorial Examples: