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

วิธีฝังข้อความ (Text Embedding) เป็นเวกเตอร์มิติด้วย Python และ Keras

TensorFlow: ML Framework Google Open-source Research/Production ใช้ NumPy และ Multi-dimensional arrays (Tensors) รองรับ Deep Neural Networks

Keras: Deep Learning API Python จากโครงการ ONEIROS รันบน TensorFlow Scalable, Cross-platform เข้าถึงด้วย from tensorflow import keras

Functional API

ยืดหยุ่นกว่า Sequential API รองรับ Non-linear topology, Shared layers, Multiple I/O สร้าง DAG ของเลเยอร์

ตัวอย่าง: Embed Text เป็น Vector แล้วประมวลผลด้วย LSTM

print("Number of unique issue tags")
num_tags = 12
print("Size of vocabulary while preprocessing text data")
num_words = 10000
print("Number of classes for predictions")
num_classes = 4

title_input = keras.Input(shape=(None,), name="title")
print("Variable length int sequence")
body_input = keras.Input(shape=(None,), name="body")
tags_input = keras.Input(shape=(num_tags,), name="tags")

print("Embed every word in the title to a 64-dimensional vector")
title_features = layers.Embedding(num_words, 64)(title_input)

print("Embed every word into a 64-dimensional vector")
body_features = layers.Embedding(num_words, 64)(body_input)

print("Reduce sequence of embedded words into single 128-dimensional vector")
title_features = layers.LSTM(128)(title_features)

print("Reduce sequence of embedded words into single 132-dimensional vector")
body_features = layers.LSTM(32)(body_features)

print("Merge available features into a single vector by concatenating it")
x = layers.concatenate([title_features, body_features, tags_input])

print("Use logistic regression to predict the features")
priority_pred = layers.Dense(1, name="priority")(x)
department_pred = layers.Dense(num_classes, name="class")(x)

print("Instantiate a model that predicts priority and class")
model = keras.Model(
    inputs=[title_input, body_input, tags_input],
    outputs=[priority_pred, department_pred],
)

Code credit: TensorFlow Keras Functional Guide

ผลลัพธ์

Number of unique issue tags
Size of vocabulary while preprocessing text data
Number of classes for predictions
Variable length int sequence
Embed every word in the title to a 64-dimensional vector
Embed every word into a 64-dimensional vector
Reduce sequence of embedded words into single 128-dimensional vector
Reduce sequence of embedded words into single 132-dimensional vector
Merge available features into a single vector by concatenating it
Use logistic regression to predict the features
Instantiate a model that predicts priority and class

คำอธิบาย

  • Functional API รองรับ Multiple Inputs/Outputs (Sequential API ทำไม่ได้)
  • Embedding Layer แปลงคำเป็น Dense Vector
  • LSTM ลด Sequence ของ Vector เป็น Single Vector
  • Concatenate รวม Features จากหลายแหล่ง
  • Dense Layer สำหรับ Prediction