如何使用 Python 和 Keras 利用預訓練模型?
Tensorflow 是 Google 提供的一個機器學習框架。它是一個開源框架,與 Python 結合使用以實現演算法、深度學習應用程式等等。它用於研究和生產目的。
Keras 在希臘語中意為“角”。Keras 是作為 ONEIROS 專案(開放式神經電子智慧機器人作業系統)研究的一部分開發的。Keras 是一個深度學習 API,用 Python 編寫。它是一個高階 API,具有高效的介面,有助於解決機器學習問題。
它執行在 Tensorflow 框架之上。它旨在幫助快速進行實驗。它提供了開發和封裝機器學習解決方案所需的必要抽象和構建塊。
它具有高度可擴充套件性,並具有跨平臺功能。這意味著 Keras 可以執行在 TPU 或 GPU 叢集上。Keras 模型也可以匯出到 Web 瀏覽器或手機上執行。
Keras 已經存在於 Tensorflow 包中。可以使用以下程式碼行訪問它。
import tensorflow from tensorflow import keras
我們正在使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助在瀏覽器上執行 Python 程式碼,無需任何配置,並可以免費訪問 GPU(圖形處理單元)。Colaboratory 建立在 Jupyter Notebook 之上。以下是程式碼片段:
示例
print("A convolutional model with pre-trained weights is loaded")
base_model = keras.applications.Xception(
weights='imagenet',
include_top=False,
pooling='avg')
print("This model is freezed")
base_model.trainable = False
print("A sequential model is used to add a trainable classifier on top of the base")
model = keras.Sequential([
base_model,
layers.Dense(1000),
])
print("Compile the model")
print("Fit the model to the test data")
model.compile(...)
model.fit(...)程式碼來源 - https://www.tensorflow.org/guide/keras/sequential_model
輸出
A convolutional model with pre-trained weights is loaded Downloading data from https://storage.googleapis.com/tensorflow/kerasapplications/xception/xception_weights_tf_dim_ordering_tf_kernels_notop.h583689472/83683744 [==============================] - 1s 0us/step This model is freezed A sequential model is used to add a trainable classifier on top of the base Compile the model Fit the model to the test data
解釋
可以使用順序模型堆疊,並藉助預訓練模型來初始化分類層。
構建此模型後,對其進行編譯。
編譯完成後,可以將此模型擬合到訓練資料。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP