Keras 模型可以像一個層一樣被呼叫嗎?如果是,請演示。
Tensorflow 是 Google 提供的一個機器學習框架。它是一個開源框架,與 Python 結合使用,可以實現演算法、深度學習應用程式等等。它用於研究和生產目的。
Keras 是作為 ONEIROS 專案(開放式神經電子智慧機器人作業系統)研究的一部分而開發的。Keras 是一個用 Python 編寫的深度學習 API。它是一個高階 API,具有高效的介面,有助於解決機器學習問題。它執行在 Tensorflow 框架之上。它旨在幫助快速進行實驗。它提供了開發和封裝機器學習解決方案所需的必要抽象和構建塊。
它具有高度的可擴充套件性,並具有跨平臺功能。這意味著 Keras 可以執行在 TPU 或 GPU 叢集上。Keras 模型也可以匯出以在 Web 瀏覽器或行動電話上執行。
Keras 已經存在於 Tensorflow 包中。可以使用以下程式碼行訪問它。
import tensorflow from tensorflow import keras
是的,Keras 模型可以像一個層一樣被呼叫。Keras 函式式 API 有助於建立比使用順序 API 建立的模型更靈活的模型。函式式 API 可以處理具有非線性拓撲的模型,可以共享層,並可以處理多個輸入和輸出。深度學習模型通常是一個包含多個層的定向無環圖 (DAG)。函式式 API 有助於構建層圖。
我們使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助在瀏覽器上執行 Python 程式碼,無需任何配置,並可免費訪問 GPU(圖形處理單元)。Colaboratory 建立在 Jupyter Notebook 之上。以下是將 Keras 模型視為一層並使用 Python 呼叫的程式碼片段:
示例
Encoder_input = keras.Input(shape=(28, 28, 1), name=”original_img”)
print("Adding layers to the model")
x = layers.Conv2D(16, 3, activation="relu")(encoder_input)
x = layers.Conv2D(32, 3, activation="relu")(x)
x = layers.MaxPooling2D(3)(x)
x = layers.Conv2D(32, 3, activation="relu")(x)
x = layers.Conv2D(16, 3, activation="relu")(x)
print("Performing golbal max pooling")
encoder_output = layers.GlobalMaxPooling2D()(x)
print("Creating a model using the layers")
encoder = keras.Model(encoder_input, encoder_output, name="encoder")
print("More information about the model")
encoder.summary()
decoder_input = keras.Input(shape=(16,), name="encoded_img")
print("Reshaping the layers in the model")
x = layers.Reshape((4, 4, 1))(decoder_input)
x = layers.Conv2DTranspose(16, 3, activation="relu")(x)
x = layers.Conv2DTranspose(32, 3, activation="relu")(x)
x = layers.UpSampling2D(3)(x)
x = layers.Conv2DTranspose(16, 3, activation="relu")(x)
decoder_output = layers.Conv2DTranspose(1, 3, activation="relu")(x)
print("Creating a model using the layers")
decoder = keras.Model(decoder_input, decoder_output, name="decoder")
print("More information about the model")
decoder.summary()
autoencoder_input = keras.Input(shape=(28, 28, 1), name="img")
encoded_img = encoder(autoencoder_input)
decoded_img = decoder(encoded_img)
autoencoder = keras.Model(autoencoder_input, decoded_img, name="autoencoder")
print("More information about the model")
autoencoder.summary()程式碼來源:https://www.tensorflow.org/guide/keras/functional
輸出
original_img (InputLayer) [(None, 28, 28, 1)] 0 _________________________________________________________________ conv2d_28 (Conv2D) (None, 26, 26, 16) 160 _________________________________________________________________ conv2d_29 (Conv2D) (None, 24, 24, 32) 4640 _________________________________________________________________ max_pooling2d_7 (MaxPooling2 (None, 8, 8, 32) 0 _________________________________________________________________ conv2d_30 (Conv2D) (None, 6, 6, 32) 9248 _________________________________________________________________ conv2d_31 (Conv2D) (None, 4, 4, 16) 4624 _________________________________________________________________ global_max_pooling2d_3 (Glob (None, 16) 0 ================================================================= Total params: 18,672 Trainable params: 18,672 Non-trainable params: 0 _________________________________________________________________ Reshaping the layers in the model Creating a model using the layers More information about the model Model: "decoder" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= encoded_img (InputLayer) [(None, 16)] 0 _________________________________________________________________ reshape_1 (Reshape) (None, 4, 4, 1) 0 _________________________________________________________________ conv2d_transpose_4 (Conv2DTr (None, 6, 6, 16) 160 _________________________________________________________________ conv2d_transpose_5 (Conv2DTr (None, 8, 8, 32) 4640 _________________________________________________________________ up_sampling2d_1 (UpSampling2 (None, 24, 24, 32) 0 _________________________________________________________________ conv2d_transpose_6 (Conv2DTr (None, 26, 26, 16) 4624 _________________________________________________________________ conv2d_transpose_7 (Conv2DTr (None, 28, 28, 1) 145 ================================================================= Total params: 9,569 Trainable params: 9,569 Non-trainable params: 0 _________________________________________________________________ More information about the model Model: "autoencoder" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= img (InputLayer) [(None, 28, 28, 1)] 0 _________________________________________________________________ encoder (Functional) (None, 16) 18672 _________________________________________________________________ decoder (Functional) (None, 28, 28, 1) 9569 ================================================================= Total params: 28,241 Trainable params: 28,241 Non-trainable params: 0 _________________________________________________________________
解釋
任何模型都可以透過在另一個層的“輸入”或輸出上呼叫它來作為一層。
呼叫模型時,架構將被重用。
此外,權重也將被重用。
可以使用編碼器模型和解碼器模型建立自動編碼器模型。
這兩個模型透過兩次呼叫連結在一起以獲得自動編碼器模型。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP