Datasets documentation
Hiểu rõ bộ dữ liệu của bạn
Hiểu rõ bộ dữ liệu của bạn
Có hai loại đối tượng dataset, một Dataset thông thường và một ✨ IterableDataset ✨. Một Dataset cung cấp khả năng truy cập ngẫu nhiên nhanh chóng vào các dòng, cùng với memory-mapping để việc tải các dataset lớn chỉ sử dụng một lượng bộ nhớ thiết bị tương đối nhỏ. Nhưng đối với những dataset thực sự, thực sự lớn đến mức không thể chứa vừa trên đĩa hoặc trong bộ nhớ, một IterableDataset cho phép bạn truy cập và sử dụng dataset mà không cần chờ nó tải xuống hoàn tất!
Hướng dẫn này sẽ cho bạn thấy cách tải và truy cập một Dataset và một IterableDataset.
Bộ dữ liệu
Khi bạn tải một phần chia của bộ dữ liệu, bạn sẽ nhận được một đối tượng Dataset. Bạn có thể thực hiện nhiều thao tác với một đối tượng Dataset, đó là lý do tại sao việc học cách thao tác và tương tác với dữ liệu được lưu trữ bên trong là rất quan trọng.
Hướng dẫn này sử dụng bộ dữ liệu rotten_tomatoes, nhưng bạn có thể tự do tải bất kỳ bộ dữ liệu nào bạn muốn và làm theo!
>>> from datasets import load_dataset
>>> dataset = load_dataset("cornell-movie-review-data/rotten_tomatoes", split="train")Chỉ mục
Một Dataset chứa các cột dữ liệu, và mỗi cột có thể là một kiểu dữ liệu khác nhau. index, hay nhãn trục, được dùng để truy cập các ví dụ từ tập dữ liệu. Ví dụ, chỉ mục theo hàng sẽ trả về một từ điển chứa một ví dụ từ tập dữ liệu:
# Get the first row in the dataset
>>> dataset[0]
{'label': 1,
'text': 'the rock is destined to be the 21st century\'s new " conan " and that he\'s going to make a splash even greater than arnold schwarzenegger , jean-claud van damme or steven segal .'}Sử dụng toán tử - để bắt đầu từ cuối bộ dữ liệu:
# Get the last row in the dataset
>>> dataset[-1]
{'label': 0,
'text': 'things really get weird , though not particularly scary : the movie is all portent and no content .'}Truy cập theo tên cột sẽ trả về một danh sách tất cả các giá trị trong cột:
>>> dataset["text"]
['the rock is destined to be the 21st century\'s new " conan " and that he\'s going to make a splash even greater than arnold schwarzenegger , jean-claud van damme or steven segal .',
'the gorgeously elaborate continuation of " the lord of the rings " trilogy is so huge that a column of words cannot adequately describe co-writer/director peter jackson\'s expanded vision of j . r . r . tolkien\'s middle-earth .',
'effective but too-tepid biopic',
...,
'things really get weird , though not particularly scary : the movie is all portent and no content .']Bạn có thể kết hợp chỉ mục tên hàng và tên cột để trả về một giá trị cụ thể tại một vị trí:
>>> dataset[0]["text"]
'the rock is destined to be the 21st century\'s new " conan " and that he\'s going to make a splash even greater than arnold schwarzenegger , jean-claud van damme or steven segal .'Thứ tự lập chỉ mục không quan trọng. Lập chỉ mục theo tên cột trước tiên sẽ trả về một đối tượng Column mà bạn có thể lập chỉ mục như bình thường bằng các chỉ số hàng:
>>> import time
>>> start_time = time.time()
>>> text = dataset[0]["text"]
>>> end_time = time.time()
>>> print(f"Elapsed time: {end_time - start_time:.4f} seconds")
Elapsed time: 0.0031 seconds
>>> start_time = time.time()
>>> text = dataset["text"][0]
>>> end_time = time.time()
>>> print(f"Elapsed time: {end_time - start_time:.4f} seconds")
Elapsed time: 0.0042 secondsCắt
Slicing trả về một lát cắt - hay tập con - của dataset, rất hữu ích để xem nhiều dòng cùng lúc. Để slice một dataset, hãy dùng toán tử : để chỉ định một khoảng vị trí.
# Get the first three rows
>>> dataset[:3]
{'label': [1, 1, 1],
'text': ['the rock is destined to be the 21st century\'s new " conan " and that he\'s going to make a splash even greater than arnold schwarzenegger , jean-claud van damme or steven segal .',
'the gorgeously elaborate continuation of " the lord of the rings " trilogy is so huge that a column of words cannot adequately describe co-writer/director peter jackson\'s expanded vision of j . r . r . tolkien\'s middle-earth .',
'effective but too-tepid biopic']}
# Get rows between three and six
>>> dataset[3:6]
{'label': [1, 1, 1],
'text': ['if you sometimes like to go to the movies to have fun , wasabi is a good place to start .',
"emerges as something rare , an issue movie that's so honest and keenly observed that it doesn't feel like one .",
'the film provides some great insight into the neurotic mindset of all comics -- even those who have reached the absolute top of the game .']}IterableDataset
Một IterableDataset được nạp khi bạn đặt tham số streaming thành True trong load_dataset():
>>> from datasets import load_dataset
>>> iterable_dataset = load_dataset("ethz/food101", split="train", streaming=True)
>>> for example in iterable_dataset:
... print(example)
... break
{'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=384x512 at 0x7F0681F5C520>, 'label': 6}Bạn cũng có thể tạo một IterableDataset từ một Dataset có sẵn, nhưng nó nhanh hơn chế độ streaming vì dataset được stream từ các tệp cục bộ:
>>> from datasets import load_dataset
>>> dataset = load_dataset("cornell-movie-review-data/rotten_tomatoes", split="train")
>>> iterable_dataset = dataset.to_iterable_dataset()Một IterableDataset lặp qua một tập dữ liệu theo từng mẫu một, nên bạn không cần phải chờ tải xuống toàn bộ tập dữ liệu trước khi có thể sử dụng nó. Như bạn có thể hình dung, điều này rất hữu ích cho các tập dữ liệu lớn mà bạn muốn sử dụng ngay lập tức!
Chỉ mục
Hành vi của IterableDataset khác với Dataset thông thường. Bạn không thể truy cập ngẫu nhiên các ví dụ trong IterableDataset. Thay vào đó, bạn nên lặp qua các phần tử của nó, ví dụ bằng cách gọi next(iter()) hoặc dùng vòng lặp for để lấy phần tử tiếp theo từ IterableDataset:
>>> next(iter(iterable_dataset))
{'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=384x512 at 0x7F0681F59B50>,
'label': 6}
>>> for example in iterable_dataset:
... print(example)
... break
{'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=384x512 at 0x7F7479DE82B0>, 'label': 6}Nhưng một IterableDataset hỗ trợ chỉ mục cột, trả về một đối tượng lặp được cho các giá trị của cột:
>>> next(iter(iterable_dataset["label"]))
6Tạo một tập con
Bạn có thể trả về một tập con của tập dữ liệu với một số lượng ví dụ cụ thể bằng cách sử dụng IterableDataset.take():
# Get first three examples
>>> list(iterable_dataset.take(3))
[{'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=384x512 at 0x7F7479DEE9D0>,
'label': 6},
{'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=512x512 at 0x7F7479DE8190>,
'label': 6},
{'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=512x383 at 0x7F7479DE8310>,
'label': 6}]Nhưng khác với chia nhỏ, IterableDataset.take() tạo ra một IterableDataset mới.
Các bước tiếp theo
Bạn quan tâm đến việc tìm hiểu thêm về sự khác biệt giữa hai loại tập dữ liệu này? Hãy tìm hiểu thêm về chúng trong hướng dẫn khái niệm Sự khác biệt giữa Dataset và IterableDataset.
Để thực hành nhiều hơn với các loại dataset này, hãy xem hướng dẫn Process để tìm hiểu cách tiền xử lý một Dataset hoặc hướng dẫn Stream để tìm hiểu cách tiền xử lý một IterableDataset.
Update on GitHub