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

วิธีสร้าง Sequential Model แบบเพิ่มเติม (Incremental) ด้วย Tensorflow ใน Python


Sequential Model เกี่ยวข้องเมื่อมี Stack ของ Layers แบบเรียบง่าย ใน Stack นี้ ทุก Layer มี Input Tensor เพียงหนึ่งและ Output Tensor เพียงหนึ่ง มันไม่เหมาะสมเมื่อ Model มี Multiple Inputs หรือ Multiple Outputs ไม่เหมาะสมเมื่อ Layers ต้องการ Share ไม่เหมาะสมเมื่อ Layer มี Multiple Inputs หรือ Multiple Outputs ไม่เหมาะสมเมื่อต้องการ Non-linear Architecture

Tensorflow เป็นเฟรมเวิร์ก Machine Learning ที่ Google มอบให้ เป็นเฟรมเวิร์ก Open Source ที่ใช้ร่วมกับ Python เพื่อนำอัลกอริทึม Deep Learning Applications และอื่นๆ ไปใช้งาน ใช้ทั้งในการวิจัยและการผลิต มีเทคนิค Optimization ที่ช่วยทำการดำเนินการทางคณิตศาสตร์ที่ซับซ้อนได้อย่างรวดเร็ว

เนื่องจากมันใช้ NumPy และ Multi-dimensional Arrays Arrays หลายมิตินี้ cònเรียกว่า 'Tensors' Framework รองรับการทำงานกับ Deep Neural Network มีความสามารถในการ Scale สูง และมาพร้อมกับ Popular Datasets หลายชุด ใช้ GPU Computation และจัดการทรัพยากรอัตโนมัติ มาพร้อมกับ Machine Learning Libraries จำนวนมาก และมีเอกสารและการสนับสนุนที่ดี Framework มีความสามารถในการรัน Deep Neural Network Models, Train พวกมัน, และสร้าง Applications ที่ทำนายลักษณะเฉพาะของ Datasets นั้นๆ ได้

แพ็กเกจ 'tensorflow' สามารถติดตั้งบน Windows ได้ด้วยโค้ดด้านล่าง −

pip install tensorflow

Tensor เป็นโครงสร้างข้อมูลที่ใช้ใน TensorFlow มันช่วยเชื่อม Edge ใน Flow Diagram Flow Diagram นี้เรียกว่า 'Data Flow Graph' Tensors ไม่ใช่别的东西 就是 Multidimensional Array หรือ List พวกมันสามารถระบุได้ด้วย 3 แอตทริบิวต์หลัก

  • Rank − บอกเกี่ยวกับ Dimensionality ของ Tensor สามารถเข้าใจได้ว่าเป็น Order ของ Tensor หรือจำนวนมิติใน Tensor ที่ถูกกำหนด

  • Type − บอกเกี่ยวกับ Data Type ที่เกี่ยวข้องกับ Elements ของ Tensor อาจเป็น 1D, 2D หรือ N-Dimensional Tensor

  • Shape − คือจำนวน Rows และ Columns รวมกัน

Keras หมายถึง 'Horn' ในภาษากรีก Keras ถูกพัฒนาขึ้นเป็นส่วนหนึ่งของการวิจัยโครงการ ONEIROS (Open ended Neuro-Electronic Intelligent Robot Operating System) Keras เป็น Deep Learning API ที่เขียนด้วย Python เป็น High-level API ที่มี Interface 产品化 ช่วยแก้ปัญหา Machine Learning ได้อย่างมีประสิทธิภาพ มันรันบน Tensorflow Framework ถูกสร้างขึ้นเพื่อช่วยให้ทดลองได้อย่างรวดเร็ว มันให้ Abstractions และ Building Blocks ที่จำเป็นในการพัฒนาและ Encapsulate Machine Learning Solutions

Keras อยู่ภายในแพ็กเกจ Tensorflow แล้ว สามารถเข้าถึงได้ด้วยโค้ดด้านล่าง

import tensorflow
from tensorflow import keras

เราใช้ Google Colaboratory รันโค้ดด้านล่าง Google Colab ช่วยรัน Python Code บนเบราว์เซอร์ ต้องการการตั้งค่าศูนย์ และเข้าถึง GPU ฟรี Colaboratory สร้างบน Jupyter Notebook

มาดูตัวอย่างการสร้าง Sequential Model ด้วย Tensorflow รวมถึง Keras

ตัวอย่าง

print("A sequential model is being created")
model = keras.Sequential()
model.add(layers.Dense(2, activation="relu"))
model.add(layers.Dense(3, activation="relu"))
model.add(layers.Dense(4))
print("Dense layers have been added to the model")

Code credit - https://www.tensorflow.org/guide/keras/sequential_model

ผลลัพธ์

A sequenital model is being created
Dense layers have been added to the model

คำอธิบาย

  • นี่คือวิธีสร้าง Sequential Model ใน Keras ด้วย Python และเพิ่ม Layer เข้าไป

  • ตัวแปรหนึ่งถูกกำหนดให้เป็นการเรียกเมธอด 'sequential'

  • ร่วมกับตัวแปรนี้ เมธอด 'add' ถูกใช้เพื่อสร้าง Layers ให้ Model

  • เมื่อเพิ่ม Layers แล้ว ข้อมูลจะแสดงบน Console