Keras API


Document Link

Keras 주요 포인트


  1. Layers API (Base Layer Class, Core Layer Class)
  2. Model API (Sequential Class)

Base Layer Class


1
2
3
tf.keras.layers.Layer(
trainable=True, name=None, dtype=None, dynamic=False, **kwargs
)

가중치(Weight)와 바이어스(Bias)를 가지고 있는 신경망 계층 클래스이다.

Keras의 다양한 신경망 계층들은 모두 이 Layer 클래스를 상속한다.

가중치와 바이어스는 입력 값이 들어왔을 때 제약사항에 따라 초기화 된다. (kernel_intializer, bias_initializer, … 등등)

keras layer

Core Layers


Input Object

입력 객체(Input Object)는 텐서 타입을 갖는 추상 객체이다.

실제 입력 데이터는 아니며, 입력 데이터가 형태가 이렇다라는 것을 의미한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from tensorflow.keras.layers import Dense, Input
import numpy as np

weights = [
np.array([[-0.43890882],[ 0.23219848]]), # Weight
np.array([1.]), # Bias
]

dense = Dense(units=1, input_shape=(1, 2), weights=weights)

print('before passing x: ', dense.get_weights()) # 입력 값이 넘어오기 전까지 가중치가 초기화되지 않는다.

x = Input(shape=(1, 2))
y = dense(x)

W, b = dense.get_weights()

print('y = ', y)
print('after passing x: ', W.shape, b.shape)
1
2
3
before passing x:  []
y = KerasTensor(type_spec=TensorSpec(shape=(None, 1, 1), dtype=tf.float32, name=None), name='dense_158/BiasAdd:0', description="created by layer 'dense_158'")
after passing x: (2, 1) (1,)

Input Object는 추상 데이터이기 때문에 y값이 구체화 되지 않는다.

가중치와 바이어스가 입력 값이 들어왔을 때 초기화되는데, 실제로 Input Object를 넣으면 초기화되는 것을 볼 수 있다.

1
2
3
4
5
6
7
8
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras import Model

x = Input(shape=(32,))
y = Dense(16, activation='softmax')(x)
model = Model(x, y)

model.summary()
1
2
3
4
5
6
7
8
9
10
11
12
13
Model: "model_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_15 (InputLayer) [(None, 32)] 0

dense_152 (Dense) (None, 16) 528

=================================================================
Total params: 528
Trainable params: 528
Non-trainable params: 0
_________________________________________________________________

Input Object를 이용하면 구체적인 데이터 없어도 신경망을 설계할 수 있다.

Dense Layer

완전 연결 계층(Fully Connected Layer)

Dense Layer

Activation Layer

활성화 계층(Activation Layer)

  • Activation Class를 이용한 방법
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import tensorflow as tf
from tensorflow.keras.layers import Activation

x = tf.random.normal(shape=(1, 5))

sigmoid = Activation('sigmoid')
tanh = Activation('tanh')
relu = Activation('relu')
softmax = Activation('softmax')

y_sigmoid_tf = sigmoid(x)
y_tanh_tf = tanh(x)
y_relu_tf = relu(x)
y_softmax_tf = softmax(x)

print(f'x = {x}')
print('simoid(x)= ', y_sigmoid_tf)
print('tanh(x)= ', y_tanh_tf)
print('relu(x)= ', y_relu_tf)
print('softmax(x)= ', y_softmax_tf)
1
2
3
4
5
x = [[-0.51983726  0.292437    0.27465725  0.9531079   1.0535461 ]]
simoid(x)= tf.Tensor([[0.3728903 0.5725926 0.5682359 0.72173977 0.74145526]], shape=(1, 5), dtype=float32)
tanh(x)= tf.Tensor([[-0.47757438 0.28437626 0.26795304 0.74118686 0.7831812 ]], shape=(1, 5), dtype=float32)
relu(x)= tf.Tensor([[0. 0.292437 0.27465725 0.9531079 1.0535461 ]], shape=(1, 5), dtype=float32)
softmax(x)= tf.Tensor([[0.06825311 0.15377602 0.1510661 0.29772434 0.3291805 ]], shape=(1, 5), dtype=float32)
  • ReLU, Softmax 등 구체화된 클래스를 이용하는 방법
1
2
3
4
5
6
7
8
9
10
11
12
13
import tensorflow as tf
from tensorflow.keras.layers import ReLU, Softmax

x = tf.random.normal(shape=(1, 5))

relu = ReLU()
softmax = Softmax()

y_relu_tf = softmax(x)
y_softmax_tf = relu(x)

print('relu(x)= ', y_relu_tf)
print('softmax(x)= ', y_softmax_tf)
1
2
relu(x)=  tf.Tensor([[0.08697527 0.3341398  0.5049807  0.01408062 0.05982368]], shape=(1, 5), dtype=float32)
softmax(x)= tf.Tensor([[0. 1.1802413 1.5932021 0. 0. ]], shape=(1, 5), dtype=float32)
  • Dense Layer와 함께 사용
1
2
3
4
5
6
7
from tensorflow.keras.layers import Dense

x = tf.random.normal(shape=(1, 5))
dense = Dense(units=3, activation='softmax')
y = dense(x)

print(y)
1
tf.Tensor([[0.17379178 0.4849285  0.34127975]], shape=(1, 3), dtype=float32)
Dense With Activation

Embedding Layer

양의 정수(인덱스)가 주어졌을 때 고정된 크기의 Dense 벡터로 전환하는 계층

e.g. [[4], [20]] -> [[0.25, 0.1], [0.6, -0.2]]

단어 사전이라고도 불리며 텍스트 분류에서 많이 사용된다.

embeddings

Masking Layer

특정 단계(timestamp)의 텐서 값을 모두 mask_value로 설정한 뒤, Masking Layer를 통과하면 그 단계는 건너뛴다.

Lambda Layer

Lambda 식을 이용해 수학적 함수를 직접 지정할 수 있는 계층

1
2
3
square = keras.layers.Lambda(lambda x: x ** 2)
y = square(tf.constant(4))
print(y)
1
tf.Tensor(16, shape=(), dtype=int32)

Model Class


모델은 학습을 위한 레이어들을 그룹화한 클래스이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense

x = Input(shape=(3,))
a = Dense(4, activation=tf.nn.relu)(x)
y = Dense(5, activation=tf.nn.softmax)(a)
model = tf.keras.Model(inputs=x, outputs=y)

model.summary()

predicted = model.predict(np.array([[1, 2, 3]]))

print(predicted)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Model: "model_7"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_8 (InputLayer) [(None, 3)] 0

dense_24 (Dense) (None, 4) 16

dense_25 (Dense) (None, 5) 25

=================================================================
Total params: 41
Trainable params: 41
Non-trainable params: 0
_________________________________________________________________
[[0.20138083 0.05447086 0.62297124 0.04173229 0.07944483]]

흔히 사용하는 모델 클래스 중에는 Sequential 모델이 있다.

텐서플로우 문서에 따르면 다음과 같은 경우엔 Sequential이 부적합하다.

Sequential 모델은 다음의 경우에 적합하지 않습니다.

모델에 다중 입력 또는 다중 출력이 있습니다

레이어에 다중 입력 또는 다중 출력이 있습니다

레이어 공유를 해야 합니다

비선형 토폴로지를 원합니다(예: 잔류 연결, 다중 분기 모델)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Input

model = Sequential(
[
Input(shape=(2, 2)),
Dense(units=2, activation="relu"),
Dense(units=2, activation="softmax"),
]
)
x = tf.random.normal((5, 2, 2))
predicited = model.predict(x)
print(predicited)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[[[0.68282527 0.31717473]
[0.53100306 0.46899694]]

[[0.86712956 0.13287047]
[0.72597367 0.27402633]]

[[0.55213726 0.44786274]
[0.9673605 0.0326395 ]]

[[0.5482915 0.45170853]
[0.9302453 0.06975465]]

[[0.6118701 0.38812986]
[0.9730487 0.02695133]]]