Keras Functional API tutorial - pai-plznw4me/tensorflow_basic GitHub Wiki

keras κΈ°λ³Έ μ‚¬μš©λ²•

kerasλŠ” μ•„λž˜ 6κ°€μ§€ μˆœμ„œλŒ€λ‘œ 생성 ν•  수 μžˆλ‹€.

  1. input node μ„€μ •
  2. layer μ—°κ²° λ˜λŠ” output μ„€μ •
  3. Model 생성 및 μ‹€ν–‰
  4. Compile (for deep learning)
  5. Training
  6. Evaluation

referece by tensorflow officaial tutorial : Keras > Functional API

μœ„ 과정을 따라 Keras 을 μ‚¬μš©ν•˜λŠ”λ° μžˆμ–΄ 크게 4κ°€μ§€λ‘œ λΆ„λ₯˜ λœλ‹€.

  1. single input, single output
  2. single input, Multi output
  3. Multi input, single output
  4. Multi input, Multi output

ν•˜μ§€λ§Œ μ—¬κΈ°μ„œλŠ” λŒ€ν‘œμ μœΌλ‘œ 2κ°€μ§€ κ³Όμ •λ§Œ μ‚΄νŽ΄λ³Έλ‹€.

  1. single input, single output
  2. Multi input, Multi output

1. input node μ„€μ •(single input, multi input)

from tensorflow.keras.layers import Input

1.1 Single Input

x1 = Input(shape=[], name='x1')

1.2 Multi Input

x1 = Input(shape=[], name='x1')
x2 = Input(shape=[], name='x2')

1.3 Input Datatype

Input Datartype μœΌλ‘œλŠ” tuple, list κ°€ κ°€λŠ₯ν•˜λ‹€. ㅁ

x1 = Input(shape=(256,), name='x1')
x1 = Input(shape=[256], name='x1')

2 . μ—¬λŸ¬ layer μ—°κ²° λ˜λŠ” μ—°μ‚°

2.1 μ—°μ‚°

y = x1 + x2

2.2 μ—¬λŸ¬ Layer μ—°κ²°

input_ = Input(shape=[784])
layer1 = Desne(units=256, activation='relu')(input_)
layer2 = Desne(units=256, activation='relu')(layer1)
layer3 = Desne(units=256, activation='relu')(layer2)

3 . Output

3.1 single output

input_ = Input(shape=[784])
layer1 = Desne(units=256, activation='relu')
layer2 = Desne(units=256, activation='relu')(layer1)
layer3 = Desne(units=256, activation='relu')(layer2)

output = layer3

3.2 multi output

input_ = Input(shape=[784])
layer1 = Desne(units=256, activation='relu')
layer2 = Desne(units=256, activation='relu')(layer1)
layer3 = Desne(units=256, activation='relu')(layer2)

output1 = layer2
output2 = layer3

Model 생성

  1. single input, single output

    from tensorflow.keras.models import Model
    from tensorflow.keras.layers import Input, Dense
    import tensorflow as tf
    
    input_ = Input(shape=[784], name='input_')
    layer1 = Dense(units=256, activation='relu')(input_)
    layer2 = Dense(units=256, activation='relu')(layer1)
    layer3 = Dense(units=10, activation='relu')(layer2)
    output = layer3
    
    # model 생성 
    model = Model(input_, output)
    
    # model μ‹€ν–‰
    x = tf.zeros(shape=[1, 784])
    print(model(x))
    print(model.predict(x))
    
    
  2. multi input, multi output

    from tensorflow.keras.models import Model
    from tensorflow.keras.layers import Input, Dense
    import tensorflow as tf
    
    input_1 = Input(shape=[784], name='input_1')
    layer1 = Dense(units=10, activation='relu')(input_1)
    
    input_2 = Input(shape=[784], name='input_2')
    layer2 = Dense(units=10, activation='relu')(input_2)
    
    
    output = layer1 + layer2
    inputs = {"input_1": input_1, "input_2": input_2}
    model = Model(inputs, output)
    
    # model μ‹€ν–‰
    x = tf.zeros(shape=[1, 784])
    print(model(x))
    print(model.predict(x))
    
    

Model μ‹€ν–‰

  • λͺ¨λΈ μ‹€ν–‰ 방법

    1. __call__()
    2. Model.predict()
  • multi input , single output

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense
import tensorflow as tf

# multi input 
input_1 = Input(shape=[784])
layer1 = Dense(units=10, activation='relu')(input_1)

input_2 = Input(shape=[784])
layer2 = Dense(units=10, activation='relu')(input_2)

# single output
output = layer1 + layer2
inputs = {"input_1": input_1, "input_2": input_2}
model = Model(inputs, output)

x = tf.zeros(shape=[1, 784])
input_values = {"input_1": x, "input_2": x}

# model μ‹€ν–‰ : __call__() => numpy 
print(model.predict(input_values))

# model μ‹€ν–‰ : Model.predict() => tensor 
print(model(input_values))

  • Multi input, multi output

    from tensorflow.keras.models import Model
    from tensorflow.keras.layers import Input, Dense
    import tensorflow as tf
    
    # multi input 
    input_1 = Input(shape=[784])
    output_1 = Dense(units=10, activation='relu', name='output_1')(input_1)
    
    input_2 = Input(shape=[784])
    output_2 = Dense(units=10, activation='relu', name='output_2')(input_2)
    
    # single output
    inputs = {"input_1": input_1, "input_2": input_2}
    outputs = {"output_1": output_1, "output_2": output_2}
    model = Model(inputs, output)
    
    x = tf.zeros(shape=[1, 784])
    input_values = {"input_1": x, "input_2": x}
    
    # model μ‹€ν–‰ : __call__() => numpy 
    print(model.predict(input_values))
    
    # model μ‹€ν–‰ : Model.predict() => tensor 
    print(model(input_values))
    
    

Optional

model 에 input μž…λ ₯μ‹œ dictionary 둜 μž…λ ₯ κ°€λŠ₯

output = layer1 + layer2
inputs = {"input_1": input_1, "input_2": input_2}
model = Model(inputs, output)

model 에 input μž…λ ₯μ‹œ list 둜 μž…λ ₯ κ°€λŠ₯

output = layer1 + layer2
inputs = [input_1, input_2]
model = Model(inputs, output)

Reference

offical tensorflow guide