TensorFlow เฟรมเวิร์ก ML จาก Google Open Source ใช้กับ Python อัลกอริทึม Deep Learning งานวิจัยและ Production
ติดตั้ง tensorflow บน Windows:
pip install tensorflow
Tensor โครงสร้างข้อมูลใน TensorFlow (Data Flow Graph) เป็นอาร์เรย์หลายมิติ/ลิสต์
ใช้ชุดข้อมูล Iliad (ข้อความแปล 3 ชิ้น) โมเดลระบุผู้แปลจากบรรทัดเดี่ยว ข้อมูล Preprocessed แล้ว ใช้ Google Colab รันโค้ด (Python บนเบราว์เซอร์ ฟรี GPU บน Jupyter Notebook)
ตัวอย่างโค้ด
print("Prepare the dataset for training")
tokenizer = tf_text.UnicodeScriptTokenizer()
print("Defining a function named 'tokenize' to tokenize the text data")
def tokenize(text, unused_label):
lower_case = tf_text.case_fold_utf8(text)
return tokenizer.tokenize(lower_case)
tokenized_ds = all_labeled_data.map(tokenize)
print("Iterate over the dataset and print a few samples")
for text_batch in tokenized_ds.take(6):
print("Tokens: ", text_batch.numpy())
Credit: TensorFlow Tutorials
ผลลัพธ์
Prepare the dataset for training Defining a function named 'tokenize' to tokenize the text data WARNING:tensorflow:From /usr/local/lib/python3.6/distpackages/tensorflow/python/util/dispatch.py:201: batch_gather (from tensorflow.python.ops.array_ops) is deprecated and will be removed after 2017-10-25. Instructions for updating: `tf.batch_gather` is deprecated, please use `tf.gather` with `batch_dims=-1` instead. Iterate over the dataset and print a few samples Tokens: [b'but' b'i' b'have' b'now' b'both' b'tasted' b'food' b',' b'and' b'given'] Tokens: [b'all' b'these' b'shall' b'now' b'be' b'thine' b':' b'but' b'if' b'the' b'gods'] Tokens: [b'their' b'spiry' b'summits' b'waved' b'.' b'there' b',' b'unperceived'] Tokens: [b'"' b'i' b'pray' b'you' b',' b'would' b'you' b'show' b'your' b'love' b',' b'dear' b'friends' b','] Tokens: [b'entering' b'beneath' b'the' b'clavicle' b'the' b'point'] Tokens: [b'but' b'grief' b',' b'his' b'father' b'lost' b',' b'awaits' b'him' b'now' b',']
คำอธิบาย
กำหนดฟังก์ชัน
tokenizeแยกประโยคเป็นคำโดยกำจัด Spaceเรียกฟังก์ชันนี้กับ Dataset ทั้งชุด
แสดงตัวอย่าง Dataset หลัง Tokenize บน Console