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

วิธีใช้ TensorFlow สร้าง Vocabulary จาก Token ของ Iliad ด้วย Python


TensorFlow เฟรมเวิร์ก ML จาก Google Open Source ใช้กับ Python อัลกอริทึม Deep Learning งานวิจัยและ Production มีเทคนิคเพิ่มประสิทธิภาพคำนวณคณิตศาสตร์ซับซ้อน ใช้ NumPy และ Multi-dimensional Arrays (Tensors) รองรับ Deep Neural Networks

Tensor โครงสร้างข้อมูลใน TensorFlow (Data Flow Graph) เป็นอาร์เรย์หลายมิติ/ลิสต์

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

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

print("Build a vocabulary using the tokens")
tokenized_ds = configure_dataset(tokenized_ds)
vocab_dict = collections.defaultdict(lambda: 0)
for toks in tokenized_ds.as_numpy_iterator():
    for tok in toks:
        vocab_dict[tok] += 1
print("Sort the vocabulary")
vocab = sorted(vocab_dict.items(), key=lambda x: x[1], reverse=True)
vocab = [token for token, count in vocab]
vocab = vocab[:VOCAB_SIZE]
vocab_size = len(vocab)
print("The vocabulary size is : ", vocab_size)
print("First six vocabulary entries are :", vocab[:6])

Credit: TensorFlow Tutorials

ผลลัพธ์

Build a vocabulary using the tokens
Sort the vocabulary
The vocabulary size is : 10000
First six vocabulary entries are : [b',', b'the', b'and', b'"', b'of', b'.']

ต่อไปจะสร้าง Vocabulary โดยเรียง Token ตามความถี่และเก็บ Top VOCAB_SIZE ตัว

คำอธิบาย

  • สร้าง Vocabulary หลังเรียง Token ตามความถี่

  • แสดงรายการ Vocabulary บางส่วนบน Console