Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีใช้ TensorFlow แปลง Token จาก Iliad เป็น Integer ด้วย Python


TensorFlow เฟรมเวิร์ก ML จาก Google Open Source ใช้กับ Python อัลกอริทึม Deep Learning งานวิจัยและ Production

ติดตั้ง tensorflow บน Windows:

pip install tensorflow

Tensor โครงสร้างข้อมูลใน TensorFlow (Data Flow Graph) เป็นอาร์เรย์หลายมิติ/ลิสต์ มี 3 แอตทริบิวต์: Rank (มิติ), Type (ชนิดข้อมูล), Shape (ขนาดแถว-คอลัมน์)

ใช้ชุดข้อมูล Iliad (ข้อความแปล 3 ชิ้น) โมเดลระบุผู้แปลจากบรรทัดเดี่ยว ข้อมูล Preprocessed แล้ว ใช้ Google Colab รันโค้ด (Python บนเบราว์เซอร์ ฟรี GPU บน Jupyter Notebook)

ตัวอย่างโค้ด

keys = vocab
values = range(2, len(vocab) + 2)  # reserve 0 for padding, 1 for OOV
print("Map the tokens to integers")
init = tf.lookup.KeyValueTensorInitializer(
    keys, values, key_dtype=tf.string, value_dtype=tf.int64)
num_oov_buckets = 1
vocab_table = tf.lookup.StaticVocabularyTable(init, num_oov_buckets)
print("A function has been defined to standardize, tokenize and vectorize the dataset using tokenizer and lookup table")
def preprocess_text(text, label):
    standardized = tf_text.case_fold_utf8(text)
    tokenized = tokenizer.tokenize(standardized)
    vectorized = vocab_table.lookup(tokenized)
    return vectorized, label

Credit: TensorFlow Tutorials

ผลลัพธ์

Map the tokens to integers
A function has been defined to standardize, tokenize and vectorize the dataset using tokenizer and lookup table

คำอธิบาย

  • Vocab Set ใช้สร้าง StaticVocabularyTable

  • Token ถูก Map เป็น Integer ในช่วง [2, vocab_size + 2]

  • 0 ใช้สำหรับ Padding, 1 ใช้สำหรับ Out-of-Vocabulary (OOV) Token