[인공지능] 케라스 기초 - 모델 가중치 확인

참조


모델 가중치 확인

  • Keras에서 모델의 가중치 확인하는 방법은 다음과 같습니다.
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Flatten, Dense

inputs = Input(shape=(28,28,1))

x = Flatten(input_shape=(28,28,1))(inputs)
x = Dense(300, activation='relu')(x)
x = Dense(100, activation='relu')(x)
x = Dense(10, activation='softmax')(x)

model = Model(inputs=inputs, outputs = x)

model.summary()

# 모델의 레이어들이 리스트로 표현됨
print(model.layers)

hidden_2 = model.layers[2]
print(hidden_2.name)

# hidden_2 의 이름과 model의 'dense' 라는 이름이 같은지 확인
print(model.get_layer('dense') is hidden_2)

weights, biases = hidden_2.get_weights()
print(weights)
print(biases)

print(weights.shape)
print(biases.shape)
Model: "model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #
=================================================================
 input_1 (InputLayer)        [(None, 28, 28, 1)]       0

 flatten (Flatten)           (None, 784)               0

 dense (Dense)               (None, 300)               235500

 dense_1 (Dense)             (None, 100)               30100

 dense_2 (Dense)             (None, 10)                1010

=================================================================
Total params: 266,610
Trainable params: 266,610
Non-trainable params: 0
_________________________________________________________________
[<keras.engine.input_layer.InputLayer object at 0x000001A5419EC430>, <keras.layers.core.flatten.Flatten object at 0x000001A541A2AF20>, <keras.layers.core.dense.Dense object at 0x000001A541A2B2B0>, <keras.layers.core.dense.Dense object at 0x000001A541A2BCA0>, <keras.layers.core.dense.Dense object at 0x000001A541B34310>]
dense
True
[[ 0.00056587 -0.04243091  0.00236596 ...  0.06860478 -0.00170662
   0.0024661 ]
 [-0.02283993 -0.05340238  0.07114606 ...  0.02561086  0.0419556
   0.04638153]
 [ 0.02495409 -0.06335379  0.0248038  ...  0.01537879  0.05100092
   0.07438587]
 ...
 [-0.06526858 -0.00506426 -0.0511626  ... -0.03112128  0.06302378
   0.01169765]
 [-0.00833626  0.06536442 -0.06423136 ...  0.03417568  0.06499887
  -0.01175037]
 [-0.0508355  -0.06425534  0.04946434 ...  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