如何使用Tensorflow和預訓練模型來編譯模型?


Tensorflow 和預訓練模型可以使用‘compile’方法來編譯模型。在編譯引數中,損失被指定為‘BinaryCrossentropy’。

閱讀更多: 什麼是TensorFlow以及Keras如何與TensorFlow一起建立神經網路?

包含至少一層卷積層的神經網路被稱為卷積神經網路。我們可以使用卷積神經網路來構建學習模型。

我們將瞭解如何藉助來自預訓練網路的遷移學習來對貓和狗的影像進行分類。影像分類中遷移學習背後的直覺是,如果一個模型在大型通用資料集上進行訓練,則此模型可以有效地用作視覺世界的通用模型。它將學習特徵圖,這意味著使用者不必從頭開始在大型資料集上訓練大型模型。

閱讀更多: 如何預訓練自定義模型?

我們正在使用Google Colaboratory來執行以下程式碼。Google Colab或Colaboratory幫助在瀏覽器上執行Python程式碼,並且無需任何配置即可免費訪問GPU(圖形處理單元)。Colaboratory構建在Jupyter Notebook之上。

示例

print("The model is being compiled")
model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
   optimizer = tf.keras.optimizers.RMSprop(lr=base_learning_rate/10),
   metrics=['accuracy'])
print("The architecture of the model")
model.summary()

程式碼來源 −https://www.tensorflow.org/tutorials/images/transfer_learning

輸出

The model is being compiled
The architecture of the model
Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape           Param #
=================================================================
input_3 (InputLayer)     [(None, 160, 160, 3)]       0
_________________________________________________________________
sequential_1 (Sequential)  (None, 160, 160, 3)       0
_________________________________________________________________
tf.math.truediv (TFOpLambda) (None, 160, 160, 3)     0
_________________________________________________________________
tf.math.subtract (TFOpLambda (None, 160, 160, 3)     0
_________________________________________________________________
mobilenetv2_1.00_160 (Functi (None, 5, 5, 1280)   2257984
_________________________________________________________________
global_average_pooling2d_2  ( (None, 1280)           0
_________________________________________________________________
dropout (Dropout)     (None, 1280)                   0
_________________________________________________________________
dense_2 (Dense)            (None, 1)               1281
=================================================================
Total params: 2,259,265
Trainable params: 1,862,721
Non-trainable params: 396,544

解釋

  • 模型被解凍,然後編譯。
  • 這是使用‘compile’方法完成的。

更新於: 2021年2月25日

164 次檢視

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.