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

การสร้างโมเดล Sequential บน Auto MPG ด้วย TensorFlow (เวอร์ชัน Linear)


บทนำ

TensorFlow เฟรมเวิร์ก ML จาก Google โค้ดเปิด ใช้กับ Python รองรับ Deep Learning

คุณสมบัติ Tensor

  • Rank - มิติของเทนเซอร์
  • Type - ชนิดข้อมูลองค์ประกอบ
  • Shape - ขนาดแถวและคอลัมน์

การติดตั้ง

pip install tensorflow

โมเดล Sequential

โมเดลที่สร้างจากการวางเลเยอร์ซ้อนกัน (stack of layers)

ตัวอย่าง: โมเดล Linear Regression แบบ Sequential

print("กำลังสร้างโมเดล Sequential 1 เลเยอร์ Dense")
linear_model = tf.keras.Sequential([
    normalizer,
    layers.Dense(units=1)
])

print("กำลังทำนาย")
linear_model.predict(train_features[:10])
linear_model.layers[1].kernel

print("กำลังคอมไพล์โมเดล")
linear_model.compile(
    optimizer=tf.optimizers.Adam(learning_rate=0.1),
    loss='mean_absolute_error'
)

print("กำลังฝึกโมเดลกับข้อมูล")
history = linear_model.fit(
    train_features, train_labels,
    epochs=150,
    verbose=0,
    validation_split=0.25)

print("กำลังพล็อตค่าทำนาย")
plot_loss(history)

print("กำลังประเมินผลลัพธ์")
test_results['linear_model'] = linear_model.evaluate(
    test_features, test_labels, verbose=0)

ที่มา: TensorFlow Regression Tutorial

ผลลัพธ์

การสร้างโมเดล Sequential บน Auto MPG ด้วย TensorFlow (เวอร์ชัน Linear)

คำอธิบาย

  • สร้างโมเดล Sequential ด้วย Keras API
  • ทำนายค่า MPG
  • รูปแบบ Linear Regression ทั่วไป: y = mx + b
  • คอมไพล์โมเดลหลังทำนายตัวอย่าง
  • ฝึกโมเดล กำหนดจำนวน epochs
  • พล็อตค่าที่ทำนายได้ก่อนหน้า