tf.keras.layers.Dense

tf.keras.layers.Dense는 일반적인 완전 연결된 (densely-connected, fully-connected) 신경망 층입니다.



예제1

import tensorflow as tf

model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(4,)))
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Dense(32))

print(model.output_shape)
(None, 32)

tf.keras.Sequential()는 여러 층을 순서대로 쌓아서 신경망 모델을 구성합니다.

tf.keras.layers.Dense()는 완전 연결된 하나의 층입니다.

예제에서는 입력층과 두 개의 Dense 층을 이용해서 하나의 신경망 모델을 구성했습니다.

첫번째 인자는 출력의 개수입니다.

activation은 활성화 함수 (Activation function)를 지정합니다.



예제2

import tensorflow as tf

model = tf.keras.Sequential()
model.add(tf.keras.Input(shape=(4,)))
model.add(tf.keras.layers.Dense(16, activation='relu', use_bias=True))
model.add(tf.keras.layers.Dense(64, activation='relu', use_bias=False))
model.add(tf.keras.layers.Dense(32))

weights = model.get_weights()

print(len(weights))

## Weight matrix
print(weights[0])
print(weights[0].shape)
print(weights[2])
print(weights[2].shape)
print(weights[3])
print(weights[3].shape)

## Bias
print(weights[1])
print(weights[1].shape)
print(weights[4])
print(weights[4].shape)
5
[[-0.15961117  0.08456475 -0.24968344 -0.36658332  0.07679915  0.17068964
 0.1778081  -0.38852942 -0.06455371  0.4411075   0.4293936   0.20278746
 0.22275543 -0.52968496  0.22820109  0.13503438]
[ 0.23478085 -0.40661967  0.14057547  0.17817807  0.48860252 -0.3235253
-0.2718914  -0.19798109 -0.50538033 -0.08868048  0.23505479  0.10324138
 0.1214717  -0.48385563  0.34959757 -0.38813818]
[-0.04738975  0.06036866  0.49999487 -0.3364758  -0.3631742   0.41728276
 0.25621098 -0.25678754  0.4853561   0.11184692  0.5304036  -0.22724217
-0.19604304  0.40794802 -0.36713964  0.311534  ]
[-0.48184693  0.5102377  -0.54514503  0.4631971   0.2934478  -0.05013728
-0.11922002 -0.21711382 -0.24939421 -0.48483777  0.43167675 -0.48317057
-0.22137985 -0.24726653  0.29294533  0.3475535 ]]
(4, 16)
[[ 0.07478541  0.24733317 -0.13384454 ...  0.12875515 -0.22520004
-0.15150705]
[-0.06689265  0.12984452 -0.04549156 ...  0.0831902  -0.16397238
-0.12485544]
[ 0.05101803  0.11736333  0.0056456  ... -0.09104013  0.24490261
 0.08408114]
...
[-0.25388765 -0.09528434  0.06389534 ...  0.23301822  0.0425615
-0.02164999]
[ 0.03868753  0.19131729  0.20450869 ...  0.11165053 -0.19286358
 0.2471717 ]
[-0.03288291  0.20366484 -0.09108402 ...  0.25347614 -0.15563512
 0.07185888]]
(16, 64)
[[-0.08658165  0.22643822 -0.01747632 ...  0.12661862 -0.24255043
-0.22251076]
[ 0.20762944 -0.2188192  -0.09554124 ... -0.09945965 -0.036488
 0.15517801]
[ 0.01596808 -0.22038126  0.23374367 ... -0.19538617  0.15170097
 0.2142784 ]
...
[-0.15392667 -0.02247685  0.19617432 ... -0.1477536   0.06678343
 0.1253925 ]
[ 0.14867151  0.07034957 -0.04406935 ... -0.24543858  0.21045578
-0.09523022]
[ 0.11093956  0.07624835 -0.1829744  ... -0.1168623   0.22546047
-0.246795  ]]
(64, 32)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
(16,)
[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.]
(32,)

use_bias는 신경망에서 bias 파라미터를 사용할지 여부를 지정합니다.

get_weights() 메서드를 사용해서 모델의 weight를 얻어보면,

weights[0], weights[2], weights[3]은 각각 (4, 16), (16, 64), (64, 32) 형태의 weight matrix이고,

weights[1], weights[4]는 (16,), (32,) 형태의 bias 값임을 알 수 있습니다.



이전글/다음글