DataScience/Keras

Keras - 정리 3. (Sequential model )

KoTiv 2022. 1. 26. 11:53
반응형
SMALL

본게시물은 Keras 링크의 내용을 공부하며 한글로 번역 및 정리한 문서입니다.

Keras: the Python deep learning API

 

Keras: the Python deep learning API

State-of-the-art research. Keras is used by CERN, NASA, NIH, and many more scientific organizations around the world (and yes, Keras is used at the LHC). Keras has the low-level flexibility to implement arbitrary research ideas while offering optional high

keras.io


<1> Sequential model lib 호출 

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

- 이전 정리 2와 Sequential model의 가장 큰 차이점은 layer의 input / output tensor에서 큰차이점이 있다고 생각한다.

 

만약 모델을 사용함에있어 다중 입출력이 존재할경우 쓸수 없다 . 

 

<2> Sequential model 생성 

add() function을 이용하여 계층을 추가할수 있고 

pop() function을 이용하여 계층을 뺼수있음 .

 


<3> 특징 추출하기 

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

init_model = keras.Sequential(
    [
        keras.Input(shape=(250,250,3)),
        layers.Conv2D(32, 5, strides=2, activation="relu"),
        layers.Conv2D(32, 3, activation="relu"),
        layers.Conv2D(32, 3, activation="relu"),
    ]
)

feature_extractor = keras.Model(
    inputs = init_model.inputs,
    outputs = [layer.output for layer in init_model.layers],
)

x = tf.ones((1,250,250,3))
features = feature_extractor(x)

<4> 전이학습하기 

모델의 가장 하위 계층은 고정하지 않고 , 오직 상위 레이어만 학습하는것.

model = keras.Sequential([
    keras.Input(shape=(784)),
    layers.Dense(32, activation='relu'),
    layers.Dense(32, activation='relu'),
    layers.Dense(32, activation='relu'),
    layers.Dense(10),
])

# Presumably you would want to first load pre-trained weights.
model.load_weights(...)

# Freeze all layers except the last one.
for layer in model.layers[:-1]:
  layer.trainable = False

# Recompile and train (this will only update the weights of the last layer).
model.compile(...)
model.fit(...)

 

반응형
LIST