Tensorflow 组合多张流模型

Tensorflow 组合多张流模型,tensorflow,keras,tf.keras,Tensorflow,Keras,Tf.keras,我不熟悉深度学习和tensorflow。似乎所有在线示例都是非常简单的序列模型。但我有一个稍微有点复杂的模型,我正试图用tensorflow 2.1实现它。简言之,我有两个模型A,B,我正试图将其输出组合起来,并用作模型C的输入。请参考我所附的图表,以便更清楚地理解我试图实现的模型体系结构 这里是尚未完成的代码,到目前为止,我仍然得到很多错误。关于如何在网络下实施的任何建议。提前谢谢 def model_a(): model_small = Sequential() model_

我不熟悉深度学习和tensorflow。似乎所有在线示例都是非常简单的序列模型。但我有一个稍微有点复杂的模型,我正试图用tensorflow 2.1实现它。简言之,我有两个模型A,B,我正试图将其输出组合起来,并用作模型C的输入。请参考我所附的图表,以便更清楚地理解我试图实现的模型体系结构

这里是尚未完成的代码,到目前为止,我仍然得到很多错误。关于如何在网络下实施的任何建议。提前谢谢

def model_a():
    model_small = Sequential()
    model_small.add(Conv1D(filters=64, kernel_size=50, activation=None, input_shape=(3000, 1)))
    model_small.add(MaxPool1D(pool_size=8, strides=8))
    model_small.add(Dropout(0.5))
    model_small.add(Conv1D(filters=128, kernel_size=8, strides=1, activation=None))
    model_small.add(Conv1D(filters=128, kernel_size=8, strides=1, activation=None))
    model_small.add(Conv1D(filters=128, kernel_size=8, strides=1, activation=None))
    model_small.add(MaxPool1D(pool_size=4, strides=4))
    model_small.add(Flatten())
    return model_small
    #return model_small.add(Flatten())

def model_c():
    model = Sequential()
    model.add(Bidirectional(LSTM(512)))
    model.add(Dropout(0.5))
    model.add(Dense(4, activation='sigmoid'))

def model_b():
    model_large = Sequential()
    # the number of strides in this layer is too large at 50?
    model_large.add(Conv1D(filters=64, kernel_size=400, activation=None, input_shape=(3000, 1)))
    model_large.add(MaxPool1D(pool_size=4))
    model_large.add(Dropout(0.5))
    model_large.add(Conv1D(filters=128, kernel_size=6, activation=None))
    model_large.add(Conv1D(filters=128, kernel_size=6, activation=None))
    model_large.add(Conv1D(filters=128, kernel_size=6, strides=1, activation=None))
    model_large.add(MaxPool1D(pool_size=2))
    model_large.add(Flatten())
    return model_large
    #return model_large.add(Flatten())

def final_model():
    input1 = model_a()
    input2 = model_b()
    model_concat = concatenate([input1.output, input2.output], axis=1)
    model_concat = Dropout(0.5)(model_concat)
    # try to fix model_c here but i don't how 
    model= Model(inputs=[input1.input, input2.input], outputs=model_concat)

    return model

model_2 = final_model()
model_2.compile(
    loss=tf.keras.losses.sparse_categorical_crossentropy,
    optimizer=tf.keras.optimizers.SGD(learning_rate=0.01),
    metrics=['accuracy']  # can add more metrics
)


model_2.fit(x=INPUT, epochs=10)

你在正确的轨道上。您可以通过两种方式完成:构建一个模型,将其拆分为功能部分,或者构建多个模型并将它们链接在一起。从功能上来说,它们是一样的,因为层是一个模型;模型可以是图层

我将使用一些简单的
密集的
层通过
模型c
来表示
模型a
。作为一个警告,如果您使用顺序api,您可能会遇到一个错误,以我的方式执行此操作,因此我将使用函数api进行演示,但我向您保证它同样易于使用(模型只是在层之后定义的,而不是之前定义的)

作为一个模型拆分为多个功能:

import tensorflow as tf

def model_a(x):
  return tf.keras.layers.Dense(56,name='model_a_whatever')(x) # returning output of layer

def model_b(x):
  x = tf.keras.layers.Dense(56,name='model_b_whatever')(x)
  return tf.keras.layers.Dense(56,name='model_b_more_whatever')(x)

def model_c(x):
  x = tf.keras.layers.Dense(56,name='model_c_whatever')(x)
  x = tf.keras.layers.Dense(56,name='model_c_more_whatever')(x)
  return tf.keras.layers.Dense(56,name='model_c_even_more_whatever')(x)

# in through the input layer
main_input = tf.keras.layers.Input(shape=(12,34,56),name='INPUT')

# now through functions containing different models' layers
left = model_a(main_input)
right = model_b(main_input)

# concatenate their outputs
concatenated = tf.keras.layers.Concatenate(axis=-1)([left,right])

# now through function containing layers of model c
left = model_c(concatenated)

# and the juke right to a fully connected layer
right = tf.keras.layers.Dense(56,name='FC')(concatenated)

# then add the outputs and apply softmax activation
added = tf.keras.layers.Add(name='add')([left,right])
outputs = tf.keras.layers.Activation('softmax',name='Softmax')(added)

# now define the model
model = tf.keras.models.Model(main_input,outputs) # Model(input layer, final output))

print(model.summary())
tf.keras.utils.plot_model(model, to_file='just_a_model.png')
# as separate models linked together

def build_model_a():
    a = tf.keras.layers.Input(shape=(12,34,56),name='model_a_input')
    b = tf.keras.layers.Dense(56,name='model_a_whatever')(a) # whatever layers
    return tf.keras.models.Model(a,b,name='MODEL_A') # returning model, not just layer output

def build_model_b():
  a = tf.keras.layers.Input(shape=(12,34,56),name='model_b_input')
  b = tf.keras.layers.Dense(56,name='model_b_whatever')(a)
  b = tf.keras.layers.Dense(56,name='model_b_more_whatever')(b)
  return tf.keras.models.Model(a,b,name='MODEL_B')

def build_model_c():
  a = tf.keras.layers.Input(shape=(12,34,112),name='model_c_input') # axis 2 is doubled because concatenation.
  b = tf.keras.layers.Dense(56,name='model_c_whatever')(a)
  b = tf.keras.layers.Dense(56,name='model_c_more_whatever')(b)
  b = tf.keras.layers.Dense(56,name='model_c_even_more_whatever')(b)
  return tf.keras.models.Model(a,b,name='MODEL_C')

# define the main input
main_input = tf.keras.layers.Input(shape=(12,34,56),name='INPUT')

# build the models
model_a = build_model_a()
model_b = build_model_b()
model_c = build_model_c()

# pass input through models a and b
a = model_a(main_input)
b = model_b(main_input)

# concatenate their outputs
ab = tf.keras.layers.Concatenate(axis=-1,name='Concatenate')([a,b])

# pass through model c and fully-connected layer
c = model_c(ab)
d = tf.keras.layers.Dense(56,name='FC')(ab)

# add their outputs and apply softmax activation
add = tf.keras.layers.Add(name="add")([c,d])
outputs = tf.keras.layers.Activation('softmax',name='Softmax')(add)

model = tf.keras.models.Model(main_input,outputs)

print(model.summary())
tf.keras.utils.plot_model(model, to_file='multi_model.png')
因为所有的层都是可见的,所以图表看起来会比你的更混乱:

连接在一起的型号越多:

import tensorflow as tf

def model_a(x):
  return tf.keras.layers.Dense(56,name='model_a_whatever')(x) # returning output of layer

def model_b(x):
  x = tf.keras.layers.Dense(56,name='model_b_whatever')(x)
  return tf.keras.layers.Dense(56,name='model_b_more_whatever')(x)

def model_c(x):
  x = tf.keras.layers.Dense(56,name='model_c_whatever')(x)
  x = tf.keras.layers.Dense(56,name='model_c_more_whatever')(x)
  return tf.keras.layers.Dense(56,name='model_c_even_more_whatever')(x)

# in through the input layer
main_input = tf.keras.layers.Input(shape=(12,34,56),name='INPUT')

# now through functions containing different models' layers
left = model_a(main_input)
right = model_b(main_input)

# concatenate their outputs
concatenated = tf.keras.layers.Concatenate(axis=-1)([left,right])

# now through function containing layers of model c
left = model_c(concatenated)

# and the juke right to a fully connected layer
right = tf.keras.layers.Dense(56,name='FC')(concatenated)

# then add the outputs and apply softmax activation
added = tf.keras.layers.Add(name='add')([left,right])
outputs = tf.keras.layers.Activation('softmax',name='Softmax')(added)

# now define the model
model = tf.keras.models.Model(main_input,outputs) # Model(input layer, final output))

print(model.summary())
tf.keras.utils.plot_model(model, to_file='just_a_model.png')
# as separate models linked together

def build_model_a():
    a = tf.keras.layers.Input(shape=(12,34,56),name='model_a_input')
    b = tf.keras.layers.Dense(56,name='model_a_whatever')(a) # whatever layers
    return tf.keras.models.Model(a,b,name='MODEL_A') # returning model, not just layer output

def build_model_b():
  a = tf.keras.layers.Input(shape=(12,34,56),name='model_b_input')
  b = tf.keras.layers.Dense(56,name='model_b_whatever')(a)
  b = tf.keras.layers.Dense(56,name='model_b_more_whatever')(b)
  return tf.keras.models.Model(a,b,name='MODEL_B')

def build_model_c():
  a = tf.keras.layers.Input(shape=(12,34,112),name='model_c_input') # axis 2 is doubled because concatenation.
  b = tf.keras.layers.Dense(56,name='model_c_whatever')(a)
  b = tf.keras.layers.Dense(56,name='model_c_more_whatever')(b)
  b = tf.keras.layers.Dense(56,name='model_c_even_more_whatever')(b)
  return tf.keras.models.Model(a,b,name='MODEL_C')

# define the main input
main_input = tf.keras.layers.Input(shape=(12,34,56),name='INPUT')

# build the models
model_a = build_model_a()
model_b = build_model_b()
model_c = build_model_c()

# pass input through models a and b
a = model_a(main_input)
b = model_b(main_input)

# concatenate their outputs
ab = tf.keras.layers.Concatenate(axis=-1,name='Concatenate')([a,b])

# pass through model c and fully-connected layer
c = model_c(ab)
d = tf.keras.layers.Dense(56,name='FC')(ab)

# add their outputs and apply softmax activation
add = tf.keras.layers.Add(name="add")([c,d])
outputs = tf.keras.layers.Activation('softmax',name='Softmax')(add)

model = tf.keras.models.Model(main_input,outputs)

print(model.summary())
tf.keras.utils.plot_model(model, to_file='multi_model.png')
虽然这在功能上与第一种情况下的网络相同,但该图现在与您的网络相匹配:

两种方法都可以。如您所见,第一种方法实际上只是一种代码清理;为了清晰起见,将单独的数据管道放入函数中。如果您想变得更复杂,比如子模型有不同的损失函数等等,那么第二种方法可能会简化过程。我提到的错误只有在使用顺序api和第二种方法时才会发生