Keras

park paul·2021년 8월 21일
0

Dense

신경망 구조의 가장 기본적인 형태를 의미한다.
y = f(Wx+b)

    # 사용법
    dense = tf.keras.layers.Dense(...)

Drop out

Overfitting issue를 Regularization을 사용해서 해결하는데, 그중 가장 대표적인 것이 Drop out이다.

    tf.keras.layers.Dropout(...)
    

Conv1D

convolution 연산 중 하나, Conv1D, Conv2D, Conv3D로 나뉨.
|:--------|:--------------:|--------------:|
|Conv1D | 한 방향(가로) | 1-D array(vector) |
|Conv2D | 두 방향(가로, 세로) | 2-D array(matrix) |
|Conv3D | 세 방향(가로, 세로, 높이) | 3-D array(tensor)|

MaxPool1D

합성곡 신경망과 함께 쓰이는 기법 중 하나는 Pooling이다.
보통 feature map의 크기를 줄이거나 specific feature를 extract하기 위해 합성곱 이후 적용되는 기법이다.
다음과 같이 두 가지로 나뉘는데
- max-pooling
Extract max value in feature map
- average-pooling
Extract mean value of whole value in feature map

#1. 객체 생성 후 apply 함수를 이용해 입력 값 설정
max_pool = tf.kears.layers.MaxPool1D(...)
max_pool.apply(input)

#2. 객체 생성 시 입력 값 설정
max_pool = tf.keras.layers.MaxPool1D(...)(input)

Example to use(above 4things)

SIZE_INPUT = (1, 28, 28)

input = tf.placeholder(tf.float32, shape = SIZE_INPUT)
dropOut = tf.kears.layers.Dropout(rate = 0.2)(input)
conv1 = tf.keras.layers.Conv1D(
			filters = 10,
            kernel_size=3,
            padding='same'
            activation=tf.nn.relu)(dropOut)
 max_pool = tf.kears.layers.MaxPool1D(pool_size = 3, padding = 'same')(conv1)
 flatten = tf.keras.layers.Flatten()(max_pool)
 hidden = tf.keras.layers.Dense(units = 50, activation = tf.nn.relu)(flatten)
 output = tf.kears.layers.Dense(units = 10, activation = tf.nn.softmax)(hidden)

Estimator

4가지의 기능이 있다.
- Train
- Evaluate
- Predict
- Export

Estimator에는 선형 회귀, 선형 분류, 심층 신경망 분류기, 심층 신경망 회귀 모델 등 기본적인 모델이 이미 구현돼 있다.

Sequential model

케라스의 순차형 모델은 코드처럼 차례로 layer를 쌓으면 되는 간단한 구조이다.
1)모델 생성

from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Activation

model = Sequential()
model.add(Dense(units=64,  input_dim=784))
model.add(Activation('softmax')

#다른 방법
model = Sequential([
	Dense(64, input_shape=(784,), activation='softmax')])

2) Compilation
시퀀셜 모델을 학습하기 전에 컴파일(.compile)을 해줘야한다.

  • Optimizer: 'sgd', 'adam', rmsprop'
  • Loss Function: 손실함수를 설정해주는 부분('mse','categorical_crossentropy')
  • Metrics: 모델 성능 지표함수 model.compile(optimizer='adam',
    loss='mse', metrics=['accuracy'])
    혹은
    model.compile(optimizer= tf.keras.optimizers.SGD(lr=0.02, momentum=0,4, nesterov=True,
    loss='mse', metrics=['accuracy'])
    *이것이 더 정교한 함수 설정

3) Training
.fit()를 사용하여 훈련시킨다.

model.fit(X_train, y_train)
profile
Innovation is mine

0개의 댓글