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

อภิพาย: Keras Functional API สร้างเลเยอร์อย่างไรด้วย Python

TensorFlow: ML Framework Google Open-source Optimization คณิตศาสตร์ซับซ้อน ติดตั้ง pip install tensorflow

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

Functional API

ยืดหยุ่นกว่า Sequential API รองรับ Non-linear topology, Shared layers, Multiple I/O สร้าง DAG ของเลเยอร์

ตัวอย่าง: สร้างเลเยอร์และโมเดลด้วย Functional API

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

inputs = keras.Input(shape=(784,))
print("Demonstration")
img_inputs = keras.Input(shape=(32, 32, 3))

print("Dimensions of input")
print(inputs.shape)
print("The type of input")
print(inputs.dtype)

print("Layers in the model")
dense = layers.Dense(64, activation="relu")
x = dense(inputs)
x = layers.Dense(64, activation="relu")(x)
outputs = layers.Dense(10)(x)

print("Model is being built")
model = keras.Model(inputs=inputs, outputs=outputs, name="mnist_model")
print("More information about the model")
model.summary()

Code credit: TensorFlow Keras Functional Guide

ผลลัพธ์

อภิพาย: Keras Functional API สร้างเลเยอร์อย่างไรด้วย Python

คำอธิบาย

  • สร้าง Input Node กำหนด Shape เป็น 784-D Vector
  • Input Object คืนค่าข้อมูล Shape และ Dtype
  • สร้างเลเยอร์ Dense เชื่อมต่อกัน (Functional Style)
  • สร้าง Model โดยระบุ Inputs และ Outputs