다운로드
작성자: admin 작성일시: 2018-04-22 15:05:43 조회수: 4126 다운로드: 214
카테고리: 머신 러닝 태그목록:

Keras Model 클래스

  • Sequential 클래스외에 더 유연한 Model 클래스 제공
  • Keras의 Model 클래스 객체와 레이어(Tensor) 객체는 callable 객체.
  • 다른 레이어(Tensor) 객체를 입력으로 호출하면 그 레이어를 입력으로 가지는 복합 레이어 객체가 된다.
  • Model 클래스 객체는 Input 레이어와 그 Input 레이어에 연결된 다른 레이어를 출력으로 주어 생성.
  • Model 클래스 객체도 다른 레이어(텐서)를 입력으로 호출하면 그 레이어를 입력으로 가지는 복합 레이어가 된다.
In [1]:
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)  # warning 출력 방지

from keras.layers import Input, Dense
from keras.models import Model, Sequential

# sample weights
np.random.seed(0)
w = 0.5 * np.random.normal(size=(4, 4))
b = np.zeros((4))

model1 = Sequential(name="model1")
model1.add(Dense(4, activation='sigmoid', 
                 input_shape=(4,), weights=(w, b), name="dense1"))
type(model1)

model1.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense1 (Dense)               (None, 4)                 20        
=================================================================
Total params: 20
Trainable params: 20
Non-trainable params: 0
_________________________________________________________________
Using TensorFlow backend.
In [2]:
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot

SVG(model_to_dot(model1, show_shapes=True).create(prog='dot', format='svg'))
Out:
G 139842865152240 dense1: Dense input: output: (None, 4) (None, 4) 139842558833160 139842558833160 139842558833160->139842865152240
In [3]:
input_layer2 = Input(shape=(4,), name="input2")
hidden_layer2 = Dense(4, activation='sigmoid', 
                      weights=(w, b), name="dense2")(input_layer2)
model2 = Model(input_layer2, hidden_layer2, name="model2")
type(model2)
Out:
keras.engine.training.Model
In [4]:
model2.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input2 (InputLayer)          (None, 4)                 0         
_________________________________________________________________
dense2 (Dense)               (None, 4)                 20        
=================================================================
Total params: 20
Trainable params: 20
Non-trainable params: 0
_________________________________________________________________
In [5]:
SVG(model_to_dot(model2, show_shapes=True).create(prog='dot', format='svg'))
Out:
G 139842531292832 input2: InputLayer input: output: (None, 4) (None, 4) 139842531293168 dense2: Dense input: output: (None, 4) (None, 4) 139842531292832->139842531293168
In [6]:
model3 = Model(input_layer2, model1(input_layer2), name="model3")
model3.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input2 (InputLayer)          (None, 4)                 0         
_________________________________________________________________
model1 (Sequential)          (None, 4)                 20        
=================================================================
Total params: 20
Trainable params: 20
Non-trainable params: 0
_________________________________________________________________
In [7]:
SVG(model_to_dot(model3, show_shapes=True).create(prog='dot', format='svg'))
Out:
G 139842531292832 input2: InputLayer input: output: (None, 4) (None, 4) 139842865152128 model1: Sequential input: output: (None, 4) (None, 4) 139842531292832->139842865152128
In [8]:
input_layer4 = Input(shape=(4,), name="input4")
model4 = Model(input_layer4, model2(input_layer4), name="model4")
model4.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input4 (InputLayer)          (None, 4)                 0         
_________________________________________________________________
model2 (Model)               (None, 4)                 20        
=================================================================
Total params: 20
Trainable params: 20
Non-trainable params: 0
_________________________________________________________________
In [9]:
SVG(model_to_dot(model4, show_shapes=True).create(prog='dot', format='svg'))
Out:
G 139842531291208 input4: InputLayer input: output: (None, 4) (None, 4) 139842531293896 model2: Model input: output: (None, 4) (None, 4) 139842531291208->139842531293896
In [10]:
data = np.ones((1, 4))

y = 1 / (1 + np.exp(-(w.T).dot(data.T)))
y
Out:
array([[0.89517384],
       [0.49439434],
       [0.7787448 ],
       [0.87421386]])
In [11]:
model1.predict(data)
Out:
array([[0.89517385, 0.49439433, 0.7787448 , 0.8742138 ]], dtype=float32)
In [12]:
model2.predict(data)
Out:
array([[0.89517385, 0.49439433, 0.7787448 , 0.8742138 ]], dtype=float32)
In [13]:
model3.predict(data)
Out:
array([[0.89517385, 0.49439433, 0.7787448 , 0.8742138 ]], dtype=float32)
In [14]:
model4.predict(data)
Out:
array([[0.89517385, 0.49439433, 0.7787448 , 0.8742138 ]], dtype=float32)

Serial Model

  • 모델을 직렬로 연결하려면 모델을 레이어로 변환하기 위해 추가적인 Input 레이어가 필요
In [15]:
input_5 = Input(shape=(4,), name="input5")
model5 = Model(input_5, model2(model1(input_5)), name="model5")

model5.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input5 (InputLayer)          (None, 4)                 0         
_________________________________________________________________
model1 (Sequential)          (None, 4)                 20        
_________________________________________________________________
model2 (Model)               (None, 4)                 20        
=================================================================
Total params: 40
Trainable params: 40
Non-trainable params: 0
_________________________________________________________________
In [16]:
SVG(model_to_dot(model5, show_shapes=True).create(prog='dot', format='svg'))
Out:
G 139842530223496 input5: InputLayer input: output: (None, 4) (None, 4) 139842865152128 model1: Sequential input: output: (None, 4) (None, 4) 139842530223496->139842865152128 139842531293896 model2: Model input: output: (None, 4) (None, 4) 139842865152128->139842531293896
In [17]:
model5.predict(data)
Out:
array([[0.82399994, 0.53757536, 0.71565944, 0.8425977 ]], dtype=float32)
In [18]:
y2 = 1 / (1 + np.exp(-(w.T).dot(y)))
y2.T
Out:
array([[0.82399994, 0.5375754 , 0.71565945, 0.84259771]])

질문/덧글

아직 질문이나 덧글이 없습니다. 첫번째 글을 남겨주세요!