Keras-modules
Keras-モジュール
先に学習したように、Kerasモジュールには、事前定義されたクラス、関数、および変数が含まれており、ディープラーニングアルゴリズムに役立ちます。 この章でKerasが提供するモジュールを学びましょう。
利用可能なモジュール
まず、Kerasで利用可能なモジュールのリストを見てみましょう。
- 初期化子-初期化関数のリストを提供します。 詳細については、Kerasの_layerの章で学習できます。 機械学習のモデル作成フェーズ。
- Regularizers -レギュラライザー関数のリストを提供します。 詳しくは、_Keras Layers_の章をご覧ください。
- 制約-制約関数のリストを提供します。 詳しくは、_Keras Layers_の章をご覧ください。
- アクティベーション-アクティベーター機能のリストを提供します。 詳しくは、_Keras Layers_の章をご覧ください。
- 損失-損失関数のリストを提供します。 詳細については、_Model Training_の章で学習できます。
- Metrics -メトリック関数のリストを提供します。 詳細については、_Model Training_の章で学習できます。
- オプティマイザ-オプティマイザ機能のリストを提供します。 詳細については、_Model Training_の章で学習できます。
- Callback -コールバック関数のリストを提供します。 これをトレーニングプロセス中に使用して、中間データを出力したり、条件に基づいてトレーニング自体を停止したりできます( EarlyStopping メソッド)。
- テキスト処理-テキストを機械学習に適したNumPy配列に変換する関数を提供します。 機械学習のデータ準備フェーズで使用できます。
- 画像処理-画像を機械学習に適したNumPy配列に変換する関数を提供します。 機械学習のデータ準備フェーズで使用できます。
- シーケンス処理-指定された入力データから時間ベースのデータを生成する関数を提供します。 機械学習のデータ準備フェーズで使用できます。
- バックエンド-_TensorFlow_や_Theano_などのバックエンドライブラリの機能を提供します。
- ユーティリティ-ディープラーニングに役立つ多くのユーティリティ関数を提供します。
この章の backend モジュールと utils モデルを見てみましょう。
_backend_モジュール
*_backend_ module* は、kerasバックエンド操作に使用されます。 デフォルトでは、kerasはTensorFlowバックエンドの上で実行されます。 必要に応じて、TheanoやCNTKなどの他のバックエンドに切り替えることができます。 デフォルトのバックエンド構成は、ルートディレクトリ内の.keras/keras.jsonファイルの下に定義されています。
Keras _backend_モジュールは、以下のコードを使用してインポートできます
>>> 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
プレースホルダー
プレースホルダーテンソルをインスタンス化するために使用されます。 3D形状を保持する単純なプレースホルダーを以下に示します-
>>> 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
2つのテンソルを乗算するために使用されます。 aとbが2つのテンソルであり、cがabの乗算の結果であるとします。 形状が(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([[dtype = float32)
batch_dot
2つのデータの積をバッチで実行するために使用されます。 入力次元は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>
変数
変数を初期化するために使用されます。 この変数で単純な転置演算を実行してみましょう。
>>> data = k.variable([[variable initialized here
>>> result = k.transpose(data)
>>> print(result)
Tensor("transpose_6:0", shape = (4, 2), dtype = float32)
>>> print(k.eval(result))
[[If you want to access from numpy −
[source,prettyprint,notranslate]
>>>データ= np.array([[print(np.transpose(data))[[res = k.variable(value = data)>>> print(res)<tf.Variable 'Variable_7:0' shape = (2、4)dtype = float32_ref>
==== is_sparse(tensor)
It is used to check whether the tensor is sparse or not.
[source,prettyprint,notranslate]
>>> 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()
It is used to converts sparse into dense.
[source,prettyprint,notranslate]
>>> b = k.to_dense(a)>>> print(b)Tensor( "SparseToDense:0"、shape =(2、2)、dtype = float32)>>> print(k.is_sparse(b))誤り
==== random_uniform_variable
It is used to initialize using *_uniform distribution_* concept.
[source,result,notranslate]
k.random_uniform_variable(形状、平均、スケール)
Here,
* *shape* − denotes the rows and columns in the format of tuples.
* *mean* − mean of uniform distribution.
* *scale* − standard deviation of uniform distribution.
Let us have a look at the below example usage −
[source,result,notranslate]
>>> 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 module
*_utils_ *provides useful utilities function for deep learning. Some of the methods provided by the* _utils_* module is as follows −
==== HDF5Matrix
It is used to represent the input data in HDF5 format.
[source,result,notranslate]
keras.utilsからHDF5Matrixデータをインポートする= HDF5Matrix( 'data.hdf5'、 'data')
==== to_categorical
It is used to convert class vector into binary class matrix.
[source,result,notranslate]
>>> from keras.utils import to_categorical >>> labels = [0、1、2、3、4、5、6、7、8、9] >>> to_categorical(labels)array([[dtype = float32) >>> from keras.utils import normalize >>> normalize([1、2、3、4、5])array([[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 は、モデルのパフォーマンスを理解するための画像を生成します。