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

Áp dụng Text Vectorization cho dataset StackOverflow trong TensorFlow

TensorFlow TextVectorization layer chuyển văn bản thô thành tensor số (vector hóa) để feed vào mô hình.

Ví dụ: Vector hóa binary & int mode


print("1234 ---> ", int_vectorize_layer.get_vocabulary()[1289])
print("321 ---> ", int_vectorize_layer.get_vocabulary()[313])
print("Vocabulary size: {}".format(len(int_vectorize_layer.get_vocabulary())))

# Áp dụng lên train/val/test
binary_train_ds = raw_train_ds.map(binary_vectorize_text)
binary_val_ds   = raw_val_ds.map(binary_vectorize_text)
binary_test_ds  = raw_test_ds.map(binary_vectorize_text)

int_train_ds    = raw_train_ds.map(int_vectorize_text)
int_val_ds      = raw_val_ds.map(int_vectorize_text)
int_test_ds     = raw_test_ds.map(int_vectorize_text)

Kết quả


1234 ---> substring
321 ---> 20
Vocabulary size: 10000
The text vectorization is applied to the training dataset
The text vectorization is applied to the validation dataset
The text vectorization is applied to the test dataset

Giải thích

  • binary mode: vector one-hot/multi-hot kích thước vocab (10000), chỉ đánh dấu từ có xuất hiện.
  • int mode: mỗi từ thay bằng index trong vocab, giữ thứ tự, độ dài cố định output_sequence_length.
  • get_vocabulary() tra cứu từ theo index.

Nguồn: TensorFlow Text Tutorial