Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Thảo luận cách Keras Functional API tạo các lớp bằng Python

TensorFlow framework ML của Google. Keras API deep learning Python trên TensorFlow. Functional API tạo mô hình linh hoạt: topo không tuyến tính, chia sẻ lớp, đa đầu vào/đầu ra.

Sử dụng Google Colab. Code minh họa tạo Input, Dense layers, xây dựng mô hình:

Ví dụ

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

inputs = keras.Input(shape=(784,))
print("Minh họa")
img_inputs = keras.Input(shape=(32, 32, 3))
print("Kích thước đầu vào")
print(inputs.shape)
print("Kiểu dữ liệu đầu vào")
print(inputs.dtype)
print("Các lớp trong mô hình")
dense = layers.Dense(64, activation="relu")
x = dense(inputs)
x = layers.Dense(64, activation="relu")(x)
outputs = layers.Dense(10)(x)
print("Xây dựng mô hình")
model = keras.Model(inputs=inputs, outputs=outputs, name="mnist_model")
print("Thông tin mô hình")
model.summary()

Nguồn: TensorFlow Guide

Đầu ra

Thảo luận cách Keras Functional API tạo các lớp bằng Python

Giải thích

  • Tạo nút Input, thiết lập shape vector 784 chiều.
  • Input trả về thông tin shape và dtype của dữ liệu đầu vào.
  • Tạo mô hình bằng cách chỉ định inputs và outputs.