简体   繁体   English

Tensorflow:在另一个 model 中使用 model 作为层

[英]Tensorflow: Use model inside another model as layer

I want to use a classification model inside another model as layer, since I thought that keras models can be used as layers also.我想在另一个 model 中使用分类 model 作为层,因为我认为 keras 模型也可以用作层。 This is the code of the first model:这是第一个model的代码:

cencoder_inputs = keras.layers.Input(shape=[pad_len], dtype=np.int32)
ccondi_input = keras.layers.Input(shape=[1], dtype=np.int32)
ccondi_layer = tf.keras.layers.concatenate([cencoder_inputs, ccondi_input], axis=1)
cembeddings = keras.layers.Embedding(vocab_size, 4)
cencoder_embeddings = cembeddings(ccondi_layer)


clstm = keras.layers.LSTM(128)(cencoder_embeddings)
cout_layer = keras.layers.Dense(16, activation="softmax")(clstm)

classification_model = keras.Model(inputs=[cencoder_inputs, ccondi_input], outputs=[cout_layer])
classification_model.compile(optimizer="Nadam", loss="sparse_categorical_crossentropy", metrics=["accuracy"], experimental_run_tf_function=False)

I train this model, save and reload it as class_model and set trainable=False This is the code of my model, which should use the model above as layer:我训练这个 model,保存并重新加载它为class_model并设置 trainable trainable=False这是我的 model 的代码,它应该使用上面的 model 作为层:

encoder_inputs = keras.layers.Input(shape=[pad_len], dtype=np.int32)
decoder_inputs = keras.layers.Input(shape=[pad_len], dtype=np.int32)
condi_input = keras.layers.Input(shape=[1], dtype=np.int32)

class_layer = class_model((encoder_inputs, condi_input))
#Thats how I use the class model. Compilation goes fine so far
class_pred_layer = keras.layers.Lambda(lambda x: tf.reshape(tf.cast(tf.keras.backend.argmax(x, axis=1), dtype=tf.int32),shape=(tf.shape(encoder_inputs)[0],1)))(class_layer)
# Lambda and reshape layer, so I get 1 prediction per batch as integer
condi_layer = tf.keras.layers.concatenate([encoder_inputs, condi_input, class_pred_layer], axis=1)

embeddings = keras.layers.Embedding(vocab_size, 2)
encoder_embeddings = embeddings(condi_layer)
decoder_embeddings = embeddings(decoder_inputs)

encoder_1 = keras.layers.LSTM(64, return_sequences=True, return_state=True)
encoder_lstm_bidirectional_1 = keras.layers.Bidirectional(encoder_1)

encoder_output, state_h1, state_c1, state_h2, state_c2 = encoder_lstm_bidirectional_1(encoder_embeddings)
encoder_state = [Concatenate()([state_h1, state_h2]), Concatenate()([state_c1, state_c2])]

decoder_lstm = keras.layers.LSTM(64*2, return_sequences=True, return_state=True, name="decoder_lstm")
print(encoder_output.shape)
decoder_outputs,decoder_fwd_state, decoder_back_state = decoder_lstm(decoder_embeddings,initial_state=encoder_state) 
print(decoder_outputs.shape)
attn_layer = AttentionLayer(name="attention_layer")
attn_out, attn_states = attn_layer([encoder_output, decoder_outputs])

decoder_concat_input = Concatenate(axis=-1, name="decoder_concat_layer")([decoder_outputs, attn_out])

decoder_dense_out = keras.layers.TimeDistributed(keras.layers.Dense(vocab_size, activation="softmax"))
decoder_outputs = decoder_dense_out(decoder_concat_input)

model = keras.Model(inputs=[encoder_inputs, decoder_inputs, condi_input], outputs=[decoder_outputs])

When I execute model.fit(), I receive the following error:当我执行 model.fit() 时,我收到以下错误:

Inputs to eager execution function cannot be Keras symbolic tensors, but found [<tf.Tensor 'input_21:0' shape=(None, 35) dtype=int32>]

I thought trained models could be used easily as layers, what am I doing wrong?我认为训练有素的模型可以很容易地用作层,我做错了什么? I also already looked into this Post but it didnt help me either.我也已经查看了这篇文章,但它也没有帮助我。 Thanks for your help!谢谢你的帮助!

Ok, i will do 2 things: (1) I will give you an example that works where i had to do call a model inside an other model, and (2) try to give you a hint on what could be your problem here ( i cant really undertand the code but i had in the past the same error )好的,我会做两件事:(1)我会给你一个例子,我必须在另一个 model 中调用 model,以及(2)试着给你一个提示,说明你的问题可能在这里(我不能真正理解代码,但我过去有同样的错误)

1. This is an example of a model that use an other model as an hidden layer: 1. 这是使用另一个 model 作为隐藏层的 model 示例:

def model_test(input_shape, sub_model):
  inputs = Input(input_shape)
  eblock_1_1 = dense_convolve(inputs, n_filters=growth_rate)
  eblock_1_2 = dense_convolve(eblock_1_1, n_filters=growth_rate);
  dblock_1_1 = dense_convolve(eblock_1_2, n_filters=growth_rate);
  dblock_1_2 = dense_convolve(dblock_1_1, n_filters=growth_rate);
  final_convolution = Conv3D(2, (1, 1, 1), padding='same', activation='relu')(dblock_1_2)
  intermedio = sub_model(final_convolution)
  layer = LeakyReLU(alpha=0.3)(intermedio)
  model = Model(inputs=inputs, outputs=layer)
    
  return model

I call it like this:我这样称呼它:

with strategy.scope():
  sub_model = tf.keras.models.load_model('link_to_the_model')
  sub_model.trainable = False
  model = model_test(INPUT_SIZE, sub_model)
  model.compile(optimizer=Adam(lr=0.1),
                loss=tf.keras.losses.MeanSquaredError(),
                metrics=None)

I just tested this on google colab with keras.我刚刚在 google colab 上用 keras 测试了这个。

  1. I had the same error some time ago when i tried to call a function with eager execution inside a model, the problem here is that the training is executed in graph mode ( you can find online some info about https://towardsdatascience.com/eager-execution-vs-graph-execution-which-is-better-38162ea4dbf6 ).前段时间我尝试在 model 中调用 function 并急切执行时遇到了同样的错误,这里的问题是训练是在图形模式下执行的(您可以在网上找到一些关于Z5E056C500A1C4B6A71105B 的信息) eager-execution-vs-graph-execution-which-is-better-38162ea4dbf6 )。

If the problem is the call of the model maybe try to do what i did, pass the model as a parameter and call it inside with a layer as argument and use it as a simple layer如果问题是 model 的调用可能会尝试做我所做的,将 model 作为参数传递,并在内部使用层作为参数调用它并将其用作简单层

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM