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

อธิบายวิธีสร้าง Sequential Model (Dense Layer) ใน Tensorflow ด้วย Python


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

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

pip install tensorflow

Layers API เป็นส่วนหนึ่งของ Keras API 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 มันมีความสามารถในการ Scale สูง และมีความสามารถ Cross-platform หมายความว่า Keras รันบน TPU หรือ Cluster ของ GPUs ได้ โมเดล Keras ยังสามารถ Export ไปรันบน Web Browser หรือ Mobile Phone ได้อีกด้วย

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

import tensorflow
from tensorflow import keras

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

โค้ดต่อไปนี้สำหรับสร้าง Dense Layers

ตัวอย่าง

print("Three dense layers are being created")
layer1 = layers.Dense(2, activation="relu", name="layer_1")
layer2 = layers.Dense(3, activation="relu", name="layer_2")
layer3 = layers.Dense(4, name="layer_3")
print("The model is being called on test data")
x = tf.ones((4, 4))
y = layer3(layer2(layer1(x)))

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

ผลลัพธ์

Three dense layers are being created
The model is being called on test data
The layers are [, , ]

คำอธิบาย

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

  • ทุก Layer ถูกสร้างอย่างชัดเจนโดยการเรียกเมธอด 'layers.Dense'

  • Sequential Model ถูกสร้างโดยการส่งลิสต์ของ Layers ให้ Constructor นี้

  • แอตทริบิวต์ 'layers' สามารถใช้เพื่อทราบรายละเอียดเพิ่มเติมเกี่ยวกับ Layers ใน Model

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