在另一个模型中将模型用作层,第一个模型不训练

时间:2020-03-12 23:19:16

标签: python tensorflow keras transformer

我构建了一个Keras模型,该模型使用另一个模型作为图层,但是问题是另一个模型中的权重没有得到训练。我该如何解决?

有关更多详细信息,我使用一个转换器单独编码句子,然后将句子集与另一个转换器组合。

这是伪代码:

Class:
 def build_context_encoder(self):
     a = Input(sentences shape)
     #function stuff
     b = #transformer structure
     context_encoder = Model(inputs=[a], outputs=b)
     return context encoder

  def build_model(self):
    list_of _contexts = Input(list of contexts shape)
    context_embs = Lambda(lambda x: K.map_fn(fn=self.context_encoder, elems=x, dtype=tf.float32))(list_of_contexts)
    c = #rest of the model (context_embs)
    model = Model(inputs=[list_of _contexts], outputs=c)
    model.compile(loss='mean_squared_error', optimizer='adam', metrics=[])

    return model

  def __init__():
    self.context_encoder = self.build_context_encoder()

    self.model = self.build_model()

为什么我称其为fit时context_encoder中的权重不更新?是由于map_fn,还是因为我正在调用模型?我该如何解决?

0 个答案:

没有答案