### 2021.12.17
tf.keras.layers.Layer는 Neural Network의 모든 레이어 객체가 상속하는 클래스입니다.
tf.keras.layers.Layer의 다양한 속성 (Attribute)을 이용해서 각 레이어에 대한 정보를 확인할 수 있습니다.
뉴런층의 이름(name)과 자료형 (dtype)
import tensorflow as tf
tf.random.set_seed(0)
# 1. 뉴런층 만들기
input_layer = tf.keras.layers.InputLayer(input_shape=(3,))
hidden_layer = tf.keras.layers.Dense(units=4, activation='relu')
output_layer = tf.keras.layers.Dense(units=2, activation='softmax')
# 2. 모델 구성하기
model = tf.keras.Sequential([
input_layer,
hidden_layer,
output_layer
])
# 3. 모델 컴파일하기
model.compile(loss='mse', optimizer='Adam')
# 4. 뉴런층의 이름과 자료형
print(input_layer.name, input_layer.dtype)
print(hidden_layer.name, hidden_layer.dtype)
print(output_layer.name, output_layer.dtype)
#Out[]
input_1 float32
dense float32
dense_1 float32
name은 뉴런층의 이름입니다.
dtype은 뉴런층의 연산과 웨이트 값에 사용되는 자료형입니다.
아래와 같은 방법으로도 뉴런층의 속성을 확인할 수 있습니다.
print(model.layers[0].name)
print(model.layers[1].name)
print(model.layers[2].name)
#Out[]
dense
dense_1
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/var/folders/xx/_99p48vj7pg7bs_kpl7838_00000gn/T/ipykernel_92980/147579868.py in <module>
1 print(model.layers[0].name)
2 print(model.layers[1].name)
----> 3 print(model.layers[2].name)
IndexError: list index out of range
model.layers는 구성한 Neural Network 모델의 (입력층을 제외한) 뉴런층 레이어 객체를 리스트의 형태로 반환합니다.
model.layers[0]은 모델의 첫번째 뉴런층, 즉 은닉층 (hidden layer)입니다.
model.layers[1]은 모델의 두번째 뉴런층, 즉 출력층 (output_layer)입니다.
모델이 (입력층을 제외한) 두 개의 뉴런층을 포함하기 때문에
model.layers[2].name을 출력하면 에러를 발생합니다.
뉴런층의 입력(input)과 출력(output)
print(input_layer.input)
print(input_layer.output)
print()
print(hidden_layer.input)
print(hidden_layer.output)
print()
print(hidden_layer.input.shape)
print(hidden_layer.output.shape)
print()
print(output_layer.input)
print(output_layer.output)
#Out[]
Tensor("input_1:0", shape=(None, 3), dtype=float32)
Tensor("input_1:0", shape=(None, 3), dtype=float32)
Tensor("input_1:0", shape=(None, 3), dtype=float32)
Tensor("dense/Relu:0", shape=(None, 4), dtype=float32)
(None, 3)
(None, 4)
Tensor("dense/Relu:0", shape=(None, 4), dtype=float32)
Tensor("dense_1/Softmax:0", shape=(None, 2), dtype=float32)
input은 뉴런층의 입력 텐서 (input tensor)입니다.
output은 뉴런층의 출력 텐서 (output tensor)입니다.
은닉층 (hidden_layer)의 입력과 출력의 형태 (shape)를 출력해보면
입력 텐서는 길이 3의 형태, 출력 텐서는 길이 4의 형태를 가짐을 알 수 있습니다.
예를 들어, (None, 3)은 길이 3의 벡터의 시퀀스형태가 될 수 있음을 의미합니다.
뉴런층의 활성화함수 (activation)
#In[]
print(hidden_layer.activation)
print(hidden_layer.activation.__name__)
print(output_layer.activation)
print(output_layer.activation.__name__)
#Out[]
<function relu at 0x7fceb66d0430>
relu
<function softmax at 0x7fceb66bdd30>
softmax
activation은 뉴런 노드의 활성화함수 (Activation function)를 나타냅니다.
"__Name"__"*을 사용해서 활성화함수의 이름을 출력했습니다.
뉴런층의 가중치 (weights)
#In[]
print(hidden_layer.weights)
print()
print(output_layer.weights)
#Out[]
[<tf.Variable 'dense/kernel:0' shape=(3, 4) dtype=float32, numpy=
array([[-0.3851872 , -0.54333335, 0.0655309 , 0.1134268 ],
[-0.15428883, 0.5699866 , -0.01254469, 0.9223561 ],
[ 0.36428273, -0.6936733 , 0.38850498, 0.30073535]],
dtype=float32)>, <tf.Variable 'dense/bias:0' shape=(4,) dtype=float32, numpy=array([0., 0., 0., 0.], dtype=float32)>]
[<tf.Variable 'dense_1/kernel:0' shape=(4, 2) dtype=float32, numpy=
array([[ 0.11082816, -0.55741405],
[ 0.7298498 , 0.5545671 ],
[ 0.29023337, 0.0607245 ],
[-0.971118 , 0.74701834]], dtype=float32)>, <tf.Variable 'dense_1/bias:0' shape=(2,) dtype=float32, numpy=array([0., 0.], dtype=float32)>]
weights를 사용해서 각 뉴런층의 시냅스 가중치에 대한 정보를 얻을 수 있습니다.
get_weights() 메세드
#In[]
print(hidden_layer.get_weights())
print()
print(output_layer.get_weights())
#Out[]
[array([[-0.3851872 , -0.54333335, 0.0655309 , 0.1134268 ],
[-0.15428883, 0.5699866 , -0.01254469, 0.9223561 ],
[ 0.36428273, -0.6936733 , 0.38850498, 0.30073535]],
dtype=float32), array([0., 0., 0., 0.], dtype=float32)]
[array([[ 0.11082816, -0.55741405],
[ 0.7298498 , 0.5545671 ],
[ 0.29023337, 0.0607245 ],
[-0.971118 , 0.74701834]], dtype=float32), array([0., 0.], dtype=float32)]
get_weights() 메서드를 사용하면 시냅스 가중치를 NumPy 어레이 형태로 얻을 수 있습니다.
'K-디지털트레이닝 > 인공지능' 카테고리의 다른 글
[인공지능] #8 MNIST 손글씨 이미지 분류하기 (0) | 2021.12.17 |
---|---|
[인공지능] #7 뉴런층의 출력 확인하기 (0) | 2021.12.17 |
[인공지능] #5 AND 로직 연산 학습하기 (0) | 2021.12.16 |
[인공지능] #4 Optimizer 사용하기 (0) | 2021.12.16 |
[인공지능] #3 손실 함수 살펴보기 (0) | 2021.12.16 |