บทนำ
tf.RaggedTensor เหมาะสำหรับข้อมูลขนาดไม่เท่ากัน เช่น จำนวนคำต่อประโยค, จำนวนตัวอักษรต่อคำ ซึ่งแตกต่างจาก dense tensor ที่ต้อง padding
บทความนี้สาธิตการสร้าง RaggedTensor จาก row_starts (ตำแหน่งเริ่มต้นของแต่ละคำในลิสต์ตัวอักษรแบน)
โค้ดตัวอย่าง
print("Get the code point of every character in every word")
word_char_codepoint = tf.RaggedTensor.from_row_starts(
values=sentence_char_codepoint.values,
row_starts=word_starts)
print(word_char_codepoint)
print("Get the number of words in the specific sentence")
sentence_num_words = tf.reduce_sum(tf.cast(sentence_char_starts_word, tf.int64), axis=1)ผลลัพธ์
Get the code point of every character in every wordGet the number of words in the specific sentence
คำอธิบาย
from_row_starts(values, row_starts)สร้าง RaggedTensor โดยrow_startsคือ index ในvaluesที่แต่ละแถว (คำ) เริ่มต้นsentence_char_codepoint.valuesคือ tensor แบน (flattened) ของ code points ทุกตัวในทุกประโยคword_startsคำนวณจาก script boundaries (ดูบทความก่อนหน้า)sentence_num_wordsนับจำนวนคำต่อประโยคจาก boolean masksentence_char_starts_word
ไฟล์งาน
ขั้นตอนนี้มักอยู่ใน pipeline: decode UTF-8 → detect script boundaries → หา word_starts → สร้าง word-level RaggedTensor → ประมวลผลต่อ (เช่น embedding, segmentation)