บทนำ
TensorFlow ใช้ TextVectorization layer แปลงข้อความเป็น Tensor (ตัวเลข) เพื่อป้อนเข้า Neural Network รองรับโหมด Binary และ Int
การติดตั้ง
pip install tensorflow
ตัวอย่างโค้ด
def int_vectorize_text(text, label):
text = tf.expand_dims(text, -1)
return int_vectorize_layer(text), label
# ดึง batch ตัวอย่าง
text_batch, label_batch = next(iter(raw_train_ds))
first_question, first_label = text_batch[0], label_batch[0]
print("Question:", first_question)
print("Label:", first_label)
print("'binary' vectorized:", binary_vectorize_text(first_question, first_label)[0])
print("'int' vectorized:", int_vectorize_text(first_question, first_label)[0])ผลลัพธ์ตัวอย่าง
Question: tf.Tensor(b'"function expected error...', shape=(), dtype=string) Label: tf.Tensor(2, shape=(), dtype=int32) 'binary' vectorized: tf.Tensor([[1. 1. 1. ... 0. 0. 0.]], shape=(1, 10000), dtype=float32) 'int' vectorized: tf.Tensor([[37 464 65 ... 0 0 0]], shape=(1, 250), dtype=int64)
คำอธิบาย
Binary mode: Multi-hot vector ขนาด Vocab (10000)
Int mode: Sequence of token indices ขนาดคงที่ (250) รักษาลำดับ
ใช้
get_vocabulary()ดูคำศัพท์