如何使用TensorFlow構建自定義層的物件?
TensorFlow可以透過先建立所需的層,然後在`tf.zeros`方法上使用該層來構建自定義層的物件。
閱讀更多: 什麼是TensorFlow以及Keras如何與TensorFlow一起建立神經網路?
包含至少一層卷積層的神經網路被稱為卷積神經網路。我們可以使用卷積神經網路來構建學習模型。
影像分類遷移學習背後的直覺是,如果一個模型在一個大型且通用的資料集上進行訓練,那麼這個模型可以有效地作為一個通用的視覺世界模型。它已經學習了特徵圖,這意味著使用者不必從頭開始在一個大型資料集上訓練一個大型模型。
TensorFlow Hub是一個包含預訓練TensorFlow模型的儲存庫。TensorFlow可用於微調學習模型。我們將瞭解如何使用TensorFlow Hub中的模型與`tf.keras`,使用TensorFlow Hub中的影像分類模型。完成後,可以執行遷移學習以微調用於自定義影像類別的模型。這是透過使用預訓練的分類器模型來獲取影像並預測它是做什麼的。這可以在無需任何訓練的情況下完成。
我們使用Google Colaboratory執行以下程式碼。Google Colab或Colaboratory幫助在瀏覽器上執行Python程式碼,無需任何配置即可免費訪問GPU(圖形處理單元)。Colaboratory構建在Jupyter Notebook之上。
示例
import tensorflow as tf
print(tf.test.is_gpu_available())
layer = tf.keras.layers.Dense(100)
print("A dense layer is created")
layer = tf.keras.layers.Dense(10, input_shape=(None, 5))
print("To use the layer, it is called")
layer(tf.zeros([10, 5]))程式碼來源 −https://www.tensorflow.org/tutorials/customization/custom_layers
輸出
WARNING:tensorflow:From <ipython-input-72-7364732a3855>:2: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
WARNING:tensorflow:From <ipython-input-72-7364732a3855>:2: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
False
A dense layer is created
To use the layer, it is called
<tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]], dtype=float32)>解釋
`tf.keras.layers`包包含作為物件的層。要構建一個層,可以構建一個物件。
許多層將輸出維度或通道數作為第一個引數。
`tf.keras`可以用作構建神經網路的高階API。
大多數TensorFlow API都可以與渴望執行一起使用。
不需要輸入維度的數量,但可以在第一次使用該層時推斷出來。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP