提取3个嵌入层的Keras连接层,但这是一个空列表

我正在构建具有多个输入(实际上是3个)的Keras分类模型,以预测单个输出。具体来说,我的3个输入是:

  1. 演员
  2. 图摘要
  3. 相关电影功能

输出:

  1. 流派标签

Python代码(创建多个输入keras)

instance = kwargs['instance']
if instance.id is not None:
    current_image = Image.objects.filter(id=instance.id)[0]
    print(current_image.image.path) ### WORKS FINE
    os.remove(current_image.image.path) ### WORKS FINE

模型的结构

提取3个嵌入层的Keras连接层,但这是一个空列表

我的问题:

在一些训练数据上成功拟合并训练了模型后,我想提取该模型的嵌入物以供以后使用。在使用多输入keras模型之前,我的主要方法是训练3种不同的keras模型并提取3个形状为100的不同嵌入层。现在,我有了多输入keras模型,我想提取级联的嵌入层,并带有输出形状(无,300)。

尽管如此,当我尝试使用此python命令时:

def kera_multy_classification_model():

    sentenceLength_actors = 15
    vocab_size_frequent_words_actors = 20001

    sentenceLength_plot = 23
    vocab_size_frequent_words_plot = 17501

    sentenceLength_features = 69
    vocab_size_frequent_words_features = 20001

    model = keras.Sequential(name='Multy-Input Keras Classification model')

    actors = keras.Input(shape=(sentenceLength_actors,),name='actors_input')
    plot = keras.Input(shape=(sentenceLength_plot,name='plot_input')
    features = keras.Input(shape=(sentenceLength_features,name='features_input')

    emb1 = layers.Embedding(input_dim = vocab_size_frequent_words_actors + 1,# based on keras documentation input_dim: int > 0. Size of the vocabulary,i.e. maximum integer index + 1.
                            output_dim = Keras_Configurations_model1.EMB_DIMENSIONS,# int >= 0. Dimension of the dense embedding
                            embeddings_initializer = 'uniform',# Initializer for the embeddings matrix.
                            mask_zero = False,input_length = sentenceLength_actors,name="actors_embedding_layer")(actors)
    encoded_layer1 = layers.LSTM(100)(emb1)

    emb2 = layers.Embedding(input_dim = vocab_size_frequent_words_plot + 1,output_dim = Keras_Configurations_model2.EMB_DIMENSIONS,embeddings_initializer = 'uniform',mask_zero = False,input_length = sentenceLength_plot,name="plot_embedding_layer")(plot)
    encoded_layer2 = layers.LSTM(100)(emb2)

    emb3 = layers.Embedding(input_dim = vocab_size_frequent_words_features + 1,output_dim = Keras_Configurations_model3.EMB_DIMENSIONS,input_length = sentenceLength_features,name="features_embedding_layer")(features)
    encoded_layer3 = layers.LSTM(100)(emb3)

    merged = layers.concatenate([encoded_layer1,encoded_layer2,encoded_layer3])

    layer_1 = layers.Dense(Keras_Configurations_model1.BATCH_SIZE,activation='relu')(merged)

    output_layer = layers.Dense(Keras_Configurations_model1.TARGET_LABELS,activation='softmax')(layer_1)

    model = keras.Model(inputs=[actors,plot,features],outputs=output_layer)

    print(model.output_shape)

    print(model.summary())

    model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['sparse_categorical_accuracy'])

embeddings = model_4.layers[9].get_weights()
print(embeddings)

我得到一个空列表(第一个代码示例)或一个 IndenError:列表索引超出范围(第二个代码示例)。

在此问题上,谢谢您的任何建议或帮助。请随时在评论中询问我可能错过的任何其他信息,以使此问题更完整。

注意:Python代码和模型的结构也已提供给此先前回答的question

linrunwang 回答:提取3个嵌入层的Keras连接层,但这是一个空列表

连接层没有任何权重(如您从模型摘要中看到的那样,它没有可训练的参数),因此get_weights()输出为空。串联是一项操作。
对于您的情况,可以在训练后获得各个嵌入层的权重。

model.layers[3].get_weights() # similarly for layer 4 and 5

或者,如果要将嵌入存储在(无,300)中,则可以使用numpy连接权重。

out_concat = np.concatenate([mdoel.layers[3].get_weights()[0],mdoel.layers[4].get_weights()[0],mdoel.layers[5].get_weights()[0]],axis=-1)

尽管可以获得连接层的输出张量:

out_tensor = model.layers[9].output
# <tf.Tensor 'concatenate_3_1/concat:0' shape=(?,300) dtype=float32>
本文链接:https://www.f2er.com/2844123.html

大家都在问