Keras - 後端配置



本章詳細解釋了 Keras 後端實現 TensorFlow 和 Theano。讓我們逐一瞭解每個實現。

TensorFlow

TensorFlow 是一個開源機器學習庫,用於 Google 開發的數值計算任務。Keras 是一個構建在 TensorFlow 或 Theano 之上的高階 API。我們已經知道如何使用 pip 安裝 TensorFlow。

如果尚未安裝,可以使用以下命令安裝:

pip install TensorFlow

一旦我們執行 keras,我們就可以看到配置檔案位於您的主目錄內,並轉到 .keras/keras.json。

keras.json

{ 
   "image_data_format": "channels_last", 
   "epsilon": 1e-07, "floatx": "float32", "backend": "tensorflow" 
}

這裡,

  • image_data_format 表示資料格式。

  • epsilon 表示數值常數。它用於避免除以零錯誤。

  • floatx 表示預設資料型別float32。您還可以使用set_floatx()方法將其更改為float16float64

  • image_data_format 表示資料格式。

假設,如果檔案未建立,則移動到該位置並按照以下步驟建立:

> cd home 
> mkdir .keras 
> vi keras.json

請記住,您應該將 .keras 指定為其資料夾名稱,並將上述配置新增到 keras.json 檔案中。我們可以執行一些預定義的操作來了解後端函式。

Theano

Theano 是一個開源深度學習庫,允許您有效地評估多維陣列。我們可以使用以下命令輕鬆安裝:

pip install theano

預設情況下,keras 使用 TensorFlow 後端。如果要將後端配置從 TensorFlow 更改為 Theano,只需在 keras.json 檔案中將 backend = theano 更改即可。如下所述:

keras.json

{ 
   "image_data_format": "channels_last", 
   "epsilon": 1e-07, 
   "floatx": "float32", 
   "backend": "theano" 
}

現在儲存您的檔案,重新啟動終端並啟動 keras,您的後端將更改。

>>> import keras as k 
using theano backend.
廣告

© . All rights reserved.