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

Chuẩn bị dataset StackOverflow Questions với TensorFlow TextVectorization

TensorFlow TextVectorization layer thực hiện 3 bước: Standardize (chuẩn hóa), Tokenize (tách từ), Vectorize (chuyển thành số).

Cài đặt

pip install tensorflow

Ví dụ: Khởi tạo 2 layer vectorization


VOCAB_SIZE = 10000
MAX_SEQUENCE_LENGTH = 250

binary_vectorize_layer = TextVectorization(
    max_tokens=VOCAB_SIZE,
    output_mode='binary'
)

int_vectorize_layer = TextVectorization(
    max_tokens=VOCAB_SIZE,
    output_mode='int',
    output_sequence_length=MAX_SEQUENCE_LENGTH
)

Kết quả


The preprocessing of text begins

Giải thích

  • Standardize: lowercase, bỏ punctuation, HTML tags.
  • Tokenize: tách theo whitespace.
  • Vectorize: binary → multi-hot vector; int → sequence of token indices.
  • Binary mode phù hợp bag-of-words (linear model). Int mode phù hợp RNN/Transformer.

Nguồn: TensorFlow Text Tutorial