Keras【命名规范】及【函数式API】

命名规范

  • t_ for Tensor
  • l_ for Layer
  • m_ for Model
from keras.models import Model
from keras.layers import Input, Dense
from keras.utils import plot_model

# Layers
l_medium = Dense(100, activation='relu')
l_output1 = Dense(1, activation='sigmoid')
l_output2 = Dense(9, activation='softmax')

# Tensors
t_input = Input(shape=(5,))
t_medium = l_medium(t_input)
t_output1 = l_output1(t_medium)  # 输出1
t_output2 = l_output2(t_medium)  # 输出2

model = Model(t_input, [t_output1, t_output2])
plot_model(model, show_shapes=True, show_layer_names=False)

Keras【命名规范】及【函数式API】

单输入多输出(LSTM)

from keras.models import Model
from keras.layers import Input, LSTM
from keras.utils import plot_model
from numpy import reshape

t_input = Input(shape=(2, 1))
l_lstm = LSTM(units=4, return_sequences=True, return_state=True)
t_output, t_h, t_c = l_lstm(t_input)

model = Model(t_input, [t_output, t_h, t_c])
plot_model(model, show_shapes=True, show_layer_names=False)

x = reshape([1, 1], (1, 2, 1))
output, h, c = model.predict(x)
print(output.shape, h.shape, c.shape)
print(output[0][-1] == h[0])

Keras【命名规范】及【函数式API】

多输入单输出

from keras.models import Model
from keras.layers import Input, Dense, concatenate
from keras.utils import plot_model

t_input1 = Input(shape=(2, 1))
t_input2 = Input(shape=(3, 1))

l_medium1 = Dense(100, activation='relu')
l_medium2 = Dense(100, activation='relu')
l_output = Dense(1)

t_medium1 = l_medium1(t_input1)
t_medium2 = l_medium2(t_input2)
t_concat = concatenate([t_medium1, t_medium2], axis=1)  # 拼接
t_output = l_output(t_concat)

model = Model([t_input1, t_input2], t_output)
plot_model(model, show_shapes=True, show_layer_names=False)

Keras【命名规范】及【函数式API】

多输入多输出

from keras.models import Model
from keras.layers import Input, Dense, add
from keras.utils import plot_model

t_input1 = Input(shape=(1,))
t_input2 = Input(shape=(9,))

l_medium1 = Dense(100, activation='relu')
l_medium2 = Dense(100, activation='relu')
l_output1 = Dense(2)
l_output2 = Dense(8)

t_medium1 = l_medium1(t_input1)
t_medium2 = l_medium2(t_input2)
t_add = add([t_medium1, t_medium2])  # 相加
t_output1 = l_output1(t_add)
t_output2 = l_output2(t_add)

model = Model([t_input1, t_input2], [t_output1, t_output2])
plot_model(model, show_shapes=True, show_layer_names=False)

Keras【命名规范】及【函数式API】

动态输入维度

from keras.models import Model
from keras.layers import Input, Dense
from keras.utils import plot_model
from numpy import reshape

t_input = Input(shape=(None, 2))
l_medium = Dense(50, activation='relu')
l_output = Dense(8)

t_medium = l_medium(t_input)
t_output = l_output(t_medium)

model = Model(t_input, t_output)
plot_model(model, show_shapes=True, show_layer_names=False)

x = reshape([1] * 2, (1, 1, 2))
print(model.predict(x).shape)  # (1, 1, 8)
x = reshape([1] * 6, (1, 3, 2))
print(model.predict(x).shape)  # (1, 3, 8)

Keras【命名规范】及【函数式API】

共享层

from keras.models import Model
from keras.layers import Input, Dense, LSTM
from keras.utils import plot_model

t_input1 = Input(shape=(None, 5))
t_input2 = Input(shape=(None, 5))

l_medium = LSTM(50, activation='relu')  # 共享层
l_output1 = Dense(1)
l_output2 = Dense(9)

t_medium1 = l_medium(t_input1)
t_medium2 = l_medium(t_input2)
t_output1 = l_output1(t_medium1)
t_output2 = l_output2(t_medium2)

model = Model([t_input1, t_input2], [t_output1, t_output2])
plot_model(model, show_shapes=True, show_layer_names=False)

Keras【命名规范】及【函数式API】