Keras (trong TensorFlow) cung cấp tiện ích tải dataset lớn từ URL, giải nén, khám phá.
Cài đặt
pip install tensorflow
pip install tensorflow-text
Ví dụ: Tải dataset StackOverflow 16k
import pathlib
import tensorflow as tf
from tensorflow.keras import utils
data_url = 'https://storage.googleapis.com/download.tensorflow.org/data/stack_overflow_16k.tar.gz'
dataset = utils.get_file(
'stack_overflow_16k.tar.gz',
data_url,
untar=True,
cache_dir='stack_overflow',
cache_subdir=''
)
dataset_dir = pathlib.Path(dataset).parent
# Xem cấu trúc
print(list(dataset_dir.iterdir()))
train_dir = dataset_dir / 'train'
print(list(train_dir.iterdir()))
# Xem file mẫu
sample_file = train_dir / 'python/1755.txt'
with open(sample_file) as f:
print(f.read())
Kết quả
Downloading tensorflow-text
Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/stack_overflow_16k.tar.gz
6053888/6053168 [==============================] - 0s 0us/step
The files in the directory are listed out
The stackoverflow questions are present in the 'train/' directory
A sample file is displayed
why does this blank program print true x=true.def stupid():. x=false.stupid().print x
Giải thích
utils.get_filetự động tải, giải nén, cache.- Dataset tổ chức:
train/,/ .txt test/tương tự. - Mỗi file là một câu hỏi StackOverflow gán nhãn ngôn ngữ.
Nguồn: TensorFlow Text Tutorial