Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Tải và khám phá bộ dữ liệu Iliad từ Google Cloud Storage

Bước đầu tiên trong pipeline NLP là tải dữ liệu. TensorFlow cung cấp tf.keras.utils.get_file để tải và cache dataset từ URL.

Mã tải dữ liệu

print("Tải bộ dữ liệu Iliad")
DIRECTORY_URL = 'https://storage.googleapis.com/download.tensorflow.org/data/illiad/'
FILE_NAMES = ['cowper.txt', 'derby.txt', 'butler.txt']

print("Duyệt qua tên file")
for name in FILE_NAMES:
    text_dir = utils.get_file(name, origin=DIRECTORY_URL + name)
parent_dir = pathlib.Path(text_dir).parent

print("Danh sách file trong thư mục")
print(list(parent_dir.iterdir()))

Kết quả

Tải bộ dữ liệu Iliad
Duyệt qua tên file
Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/illiad/cowper.txt 819200/815980 [==============================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/illiad/derby.txt 811008/809730 [==============================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/illiad/butler.txt 811008/807992 [==============================] - 0s 0us/step
Danh sách file trong thư mục
[PosixPath('/root/.keras/datasets/derby.txt'), PosixPath('/root/.keras/datasets/cowper.txt'), PosixPath('/root/.keras/datasets/butler.txt')]

Giải thích

  • utils.get_file() tải file từ URL, cache tại ~/.keras/datasets/, tránh tải lại lần sau.
  • 3 file tương ứng 3 bản dịch: Cowper, Derby, Butler - tổng cộng ~2.4MB.
  • pathlib.Path dùng để thao tác đường dẫn file hiện đại, cross-platform.
  • Sau khi tải, dữ liệu sẵn sàng cho bước TextLineDataset đọc từng dòng.

Nguồn: TensorFlow Text Loading Tutorial