命名规范
-
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
l_medium = Dense(100, activation='relu')
l_output1 = Dense(1, activation='sigmoid')
l_output2 = Dense(9, activation='softmax')
t_input = Input(shape=(5,))
t_medium = l_medium(t_input)
t_output1 = l_output1(t_medium)
t_output2 = l_output2(t_medium)
model = Model(t_input, [t_output1, t_output2])
plot_model(model, show_shapes=True, show_layer_names=False)

单输入多输出(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])

多输入单输出
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)

多输入多输出
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)

动态输入维度
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)
x = reshape([1] * 6, (1, 3, 2))
print(model.predict(x).shape)

共享层
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)
