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

วิธีสร้างโมเดล Keras ที่ระบุ Input Shape ล่วงหน้าด้วย Python

TensorFlow: ML Framework Google Open-source Research/Production

Keras: Deep Learning API Python จาก ONEIROS ระดับสูง รันบน TensorFlow Scalable, Cross-platform (TPU, GPU, Web, Mobile) เข้าถึงด้วย from tensorflow import keras

ตัวอย่าง: เลเยอร์สร้าง Weights เมื่อรู้ Input Shape

print("Three dense layers are being created")
layer = layers.Dense(3)

print("The weights associated with the layers are")
print(layer.weights)  # []

print("The created layers is called on test data")
x = tf.ones((2, 3))
y = layer(x)

print("Now, the weights are : ")
print(layer.weights)

Code credit: TensorFlow Keras Sequential Model Guide

ผลลัพธ์

Three dense layers are being created
The weights associated with the layers are []
The created layers is called on test data
Now, the weights are :
[,
 ]

คำอธิบาย

  • ทุกเลเยอร์ใน Keras ต้องรู้ Input Shape เพื่อสร้าง Weights ที่เหมาะสม
  • ตอนสร้างเลเยอร์ ยังไม่มี Weights (Empty List)
  • Weights ถูกสร้างเมื่อเรียกเลเยอร์ครั้งแรกกับ Input จริง (Lazy Initialization)
  • เพราะ Weights ขึ้นอยู่กับ Input Shape
  • เลเยอร์ถูกสร้างแบบ Sequential เรียกกับ Test Data → แสดง Weights ใหม่