
Keras 模組
正如我們前面學到的,Keras 模組包含預定義的類、函式和變數,這些對於深度學習演算法非常有用。本章讓我們學習 Keras 提供的模組。
可用模組
讓我們首先看看 Keras 中可用的模組列表。
初始化器 - 提供初始化器函式列表。我們可以在 Keras 的層章節中詳細學習它,在機器學習的模型建立階段。
正則化器 - 提供正則化器函式列表。我們可以在Keras 層章節中詳細學習它。
約束 - 提供約束函式列表。我們可以在Keras 層章節中詳細學習它。
啟用函式 - 提供啟用函式列表。我們可以在Keras 層章節中詳細學習它。
損失函式 - 提供損失函式列表。我們可以在模型訓練章節中詳細學習它。
度量 - 提供度量函式列表。我們可以在模型訓練章節中詳細學習它。
最佳化器 - 提供最佳化器函式列表。我們可以在模型訓練章節中詳細學習它。
回撥函式 - 提供回撥函式列表。我們可以在訓練過程中使用它來列印中間資料,並根據某些條件停止訓練本身(EarlyStopping 方法)。
文字處理 - 提供將文字轉換為適合機器學習的 NumPy 陣列的函式。我們可以在機器學習的資料準備階段使用它。
影像處理 - 提供將影像轉換為適合機器學習的 NumPy 陣列的函式。我們可以在機器學習的資料準備階段使用它。
序列處理 - 提供根據給定輸入資料生成基於時間的函式。我們可以在機器學習的資料準備階段使用它。
後端 - 提供後端庫(如TensorFlow 和 Theano)的函式。
實用工具 - 提供許多在深度學習中非常有用的實用工具函式。
本章讓我們看看後端模組和實用工具模組。
後端模組
後端模組用於 Keras 後端操作。預設情況下,Keras 執行在 TensorFlow 後端之上。如果需要,您可以切換到其他後端,如 Theano 或 CNTK。預設後端配置定義在根目錄下的 .keras/keras.json 檔案中。
可以使用以下程式碼匯入 Keras 後端模組
>>> from keras import backend as k
如果我們使用預設後端TensorFlow,則以下函式將返回如下所示的基於TensorFlow 的資訊:
>>> k.backend() 'tensorflow' >>> k.epsilon() 1e-07 >>> k.image_data_format() 'channels_last' >>> k.floatx() 'float32'
讓我們簡要了解一些用於資料分析的重要後端函式:
get_uid()
它是預設圖的識別符號。定義如下:
>>> k.get_uid(prefix='') 1 >>> k.get_uid(prefix='') 2
reset_uids
它用於重置 uid 值。
>>> k.reset_uids()
現在,再次執行get_uid()。這將被重置並再次更改為 1。
>>> k.get_uid(prefix='') 1
placeholder
它用於例項化一個佔位符張量。一個簡單的用於儲存 3 維形狀的佔位符如下所示:
>>> data = k.placeholder(shape = (1,3,3)) >>> data <tf.Tensor 'Placeholder_9:0' shape = (1, 3, 3) dtype = float32> If you use int_shape(), it will show the shape. >>> k.int_shape(data) (1, 3, 3)
dot
它用於將兩個張量相乘。假設 a 和 b 是兩個張量,c 將是 ab 相乘的結果。假設 a 的形狀是 (4,2),b 的形狀是 (2,3)。定義如下:
>>> a = k.placeholder(shape = (4,2)) >>> b = k.placeholder(shape = (2,3)) >>> c = k.dot(a,b) >>> c <tf.Tensor 'MatMul_3:0' shape = (4, 3) dtype = float32> >>>
ones
它用於初始化所有值為1。
>>> res = k.ones(shape = (2,2)) #print the value >>> k.eval(res) array([[1., 1.], [1., 1.]], dtype = float32)
batch_dot
它用於對批次資料執行乘積。輸入維度必須為 2 或更高。如下所示:
>>> a_batch = k.ones(shape = (2,3)) >>> b_batch = k.ones(shape = (3,2)) >>> c_batch = k.batch_dot(a_batch,b_batch) >>> c_batch <tf.Tensor 'ExpandDims:0' shape = (2, 1) dtype = float32>
variable
它用於初始化一個變數。讓我們在這個變數中執行簡單的轉置操作。
>>> data = k.variable([[10,20,30,40],[50,60,70,80]]) #variable initialized here >>> result = k.transpose(data) >>> print(result) Tensor("transpose_6:0", shape = (4, 2), dtype = float32) >>> print(k.eval(result)) [[10. 50.] [20. 60.] [30. 70.] [40. 80.]]
如果要從 numpy 訪問:
>>> data = np.array([[10,20,30,40],[50,60,70,80]]) >>> print(np.transpose(data)) [[10 50] [20 60] [30 70] [40 80]] >>> res = k.variable(value = data) >>> print(res) <tf.Variable 'Variable_7:0' shape = (2, 4) dtype = float32_ref>
is_sparse(tensor)
它用於檢查張量是否是稀疏的。
>>> a = k.placeholder((2, 2), sparse=True) >>> print(a) SparseTensor(indices = Tensor("Placeholder_8:0", shape = (?, 2), dtype = int64), values = Tensor("Placeholder_7:0", shape = (?,), dtype = float32), dense_shape = Tensor("Const:0", shape = (2,), dtype = int64)) >>> print(k.is_sparse(a)) True
to_dense()
它用於將稀疏轉換為稠密。
>>> b = k.to_dense(a) >>> print(b) Tensor("SparseToDense:0", shape = (2, 2), dtype = float32) >>> print(k.is_sparse(b)) False
random_uniform_variable
它使用均勻分佈的概念進行初始化。
k.random_uniform_variable(shape, mean, scale)
這裡:
shape - 以元組的形式表示行和列。
mean - 均勻分佈的均值。
scale - 均勻分佈的標準差。
讓我們來看一下下面的示例用法:
>>> a = k.random_uniform_variable(shape = (2, 3), low=0, high = 1) >>> b = k. random_uniform_variable(shape = (3,2), low = 0, high = 1) >>> c = k.dot(a, b) >>> k.int_shape(c) (2, 2)
utils 模組
utils 提供了深度學習中非常有用的實用工具函式。utils 模組提供的一些方法如下:
HDF5Matrix
它用於以 HDF5 格式表示輸入資料。
from keras.utils import HDF5Matrix data = HDF5Matrix('data.hdf5', 'data')
to_categorical
它用於將類向量轉換為二進位制類矩陣。
>>> from keras.utils import to_categorical >>> labels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> to_categorical(labels) array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]], dtype = float32) >>> from keras.utils import normalize >>> normalize([1, 2, 3, 4, 5]) array([[0.13483997, 0.26967994, 0.40451992, 0.53935989, 0.67419986]])
print_summary
它用於列印模型的摘要。
from keras.utils import print_summary print_summary(model)
plot_model
它用於建立模型在點格式下的表示並將其儲存到檔案中。
from keras.utils import plot_model plot_model(model,to_file = 'image.png')
此plot_model 將生成一個影像來了解模型的效能。