Keras-dense-layer

提供:Dev Guides
移動先:案内検索

Keras-高密度レイヤー

  • 高密度レイヤー*は、通常の深く接続されたニューラルネットワークレイヤーです。 最も一般的で、頻繁に使用されるレイヤーです。 高密度レイヤーは、入力に対して以下の操作を実行し、出力を返します。
output = activation(dot(input, kernel) + bias)

どこで、

  • input は入力データを表します
  • *カーネル*は重量データを表します
  • dot は、すべての入力とそれに対応する重みの巨大なドット積を表します
  • bias は、モデルを最適化するために機械学習で使用されるバイアス値を表します
  • activation はアクティベーション機能を表します。

以下のサンプル入力と重みを考えて、結果を見つけてみましょう-

  • 2 x 2行列として入力 [[1、2]、[3、4]]
  • 2 x 2行列としてのカーネル [[0.5、0.75]、[0.25、0.5]]
  • 0 としてのバイアス値
  • linear としてのアクティブ化。 前に学習したように、線形アクティブ化は何もしません。
>>> import numpy as np

>>> input = [ [1, 2], [3, 4] ]
>>> kernel = [ [0.5, 0.75], [0.25, 0.5] ]
>>> result = np.dot(input, kernel)
>>> result array([[result *is the output and it will be passed into the next layer.

The output shape of the Dense layer will be affected by the number of neuron/units specified in the Dense layer. For example, if the input shape is* (8,) *and number of unit is 16, then the output shape is* (16,)*. All layer will have batch size as the first dimension and so, input shape will be represented by *(None, 8) *and the output shape as* (None, 16)*. Currently, batch size is None as it is not set. Batch size is usually set during training phase.

[source,prettyprint,notranslate]

>>> from keras.models import Sequential >>> from keras.layers import Activation、Dense

>>>モデル= Sequential()>>> layer_1 = Dense(16、input_shape =(8、))>>> model.add(layer_1)>>> layer_1.input_shape(None、8)>>> layer_1.output_shape (なし、16)>>>

where,

* *layer_1.input_shape* returns the input shape of the layer.
* *layer_1.output_shape *returns the output shape of the layer.

The argument supported by* Dense layer* is as follows −

* *_units_* represent the number of units and it affects the output layer.
* *_activation_* represents the activation function.
* *_use_bias_* represents whether the layer uses a bias vector.
* *_kernel_initializer_* represents the initializer to be used for kernel.
* *_bias_initializer_* represents the initializer to be used for the bias vector.
* *_kernel_regularizer_* represents the regularizer function to be applied to the kernel weights matrix.
* *_bias_regularizer_* represents the regularizer function to be applied to the bias vector.
* *_activity_regularizer_* represents the regularizer function tp be applied to the output of the layer.
* *_kernel_constraint_* represent constraint function to be applied to the kernel weights matrix.
* *_bias_constraint_ *represent constraint function to be applied to the bias vector.

As you have seen, there is no argument available to specify the* _input_shape_ *of the input data.* _input_shape_* is a special argument, which the layer will accept only if it is designed as first layer in the model.

Also, all Keras layer has few common methods and they are as follows −

=== get_weights

Fetch the full list of the weights used in the layer.

[source,prettyprint,notranslate]

>>> from keras.models import Sequential >>> from keras.layers import Activation、Dense >>> model = Sequential()>>> layer_1 = Dense(16、input_shape =(8、))>>> model.add (layer_1)>>> layer_1.get_weights()>>> [array([[dtype = float32)、array([0。、0.、0.、0.、0.、0.、0.、0。 、0.、0.、0.、0.、0.、0.、0.、0。]、dtype = float32)] >>>

* *_set_weights_* − Set the weights for the layer
* *_get_config_* − Get the complete configuration of the layer as an object which can be reloaded at any time.

[source,result,notranslate]

config = layer_1.get_config()

=== from_config

Load the layer from the configuration object of the layer.

[source,result,notranslate]

config = layer_1.get_config()reload_layer = Dense.from_config(config)

=== input_shape

Get the input shape, if only the layer has single node.

[source,prettyprint,notranslate]

>>> from keras.models import Sequential >>> from keras.layers import Activation、Dense >>> model = Sequential()>>> layer_1 = Dense(16、input_shape =(8、))>>> model.add (layer_1)>>> layer_1.get_weights()>>> layer_1.input_shape(なし、8)

=== input

Get the input data, if only the layer has single node.

[source,prettyprint,notranslate]

>>> from keras.models import Sequential >>> from keras.layers import Activation、Dense >>> model = Sequential()>>> layer_1 = Dense(16、input_shape =(8、))>>> model.add (layer_1)>>> layer_1.get_weights()>>> layer_1.input <tf.Tensor 'dense_1_input:0' shape =(?、8)dtype = float32>

* *_get_input_at_* − Get the input data at the specified index, if the layer has multiple node
* *_get_input_shape_at_* − Get the input shape at the specified index, if the layer has multiple node
* *_output_shape_* − Get the output shape, if only the layer has single node.

[source,result,notranslate]

>>> from keras.models import Sequential >>> from keras.layers import Activation、Dense >>> model = Sequential()>>> layer_1 = Dense(16、input_shape =(8、))>>> model.add (layer_1)>>> layer_1.get_weights()>>> layer_1.output_shape(なし、16)

=== output

Get the output data, if only the layer has single node.

[source,result,notranslate]

>>> from keras.models import Sequential >>> from keras.layers import Activation、Dense >>> model = Sequential()>>> layer_1 = Dense(16、input_shape =(8、))>>> model.add (layer_1)>>> layer_1.get_weights()>>> layer_1.output <tf.Tensor 'dense_1/BiasAdd:0' shape =(?、16)dtype = float32>

* *_get_output_at_* − Get the output data at the specified index, if the layer has multiple node
* *_get_output_shape_ at_* − Get the output shape at the specified index, if the layer has multiple node