获取在softmax层之前的CNN的最后一层中获得的向量

时间:2018-11-13 12:22:02

标签: python tensorflow keras conv-neural-network natural-language-processing

我正在尝试通过使用CNN编码输入来实现系统。在CNN之后,我需要获取一个向量并将其用于另一种深度学习方法。

  def get_input_representation(self):
    # get word vectors from embedding
    inputs = tf.nn.embedding_lookup(self.embeddings, self.input_placeholder)


    sequence_length = inputs.shape[1] # 56
    vocabulary_size = 160 # 18765
    embedding_dim = 256
    filter_sizes = [3,4,5]
    num_filters = 3
    drop = 0.5

    epochs = 10
    batch_size = 30

    # this returns a tensor
    print("Creating Model...")
    inputs = Input(shape=(sequence_length,), dtype='int32')
    embedding = Embedding(input_dim=vocabulary_size, output_dim=embedding_dim, input_length=sequence_length)(inputs)
    reshape = Reshape((sequence_length,embedding_dim,1))(embedding)

    conv_0 = Conv2D(num_filters, kernel_size=(filter_sizes[0], embedding_dim), padding='valid', kernel_initializer='normal', activation='relu')(reshape)
    conv_1 = Conv2D(num_filters, kernel_size=(filter_sizes[1], embedding_dim), padding='valid', kernel_initializer='normal', activation='relu')(reshape)
    conv_2 = Conv2D(num_filters, kernel_size=(filter_sizes[2], embedding_dim), padding='valid', kernel_initializer='normal', activation='relu')(reshape)

    maxpool_0 = MaxPool2D(pool_size=(sequence_length - filter_sizes[0] + 1, 1), strides=(1,1), padding='valid')(conv_0)
    maxpool_1 = MaxPool2D(pool_size=(sequence_length - filter_sizes[1] + 1, 1), strides=(1,1), padding='valid')(conv_1)
    maxpool_2 = MaxPool2D(pool_size=(sequence_length - filter_sizes[2] + 1, 1), strides=(1,1), padding='valid')(conv_2)

    concatenated_tensor = Concatenate(axis=1)([maxpool_0, maxpool_1, maxpool_2])
    flatten = Flatten()(concatenated_tensor)
    dropout = Dropout(drop)(flatten)
    output = Dense(units=2, activation='softmax')(dropout)
    model = Model(inputs=inputs, outputs=output)
    adam = Adam(lr=1e-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)

    model.compile(optimizer=adam, loss='binary_crossentropy', metrics=['accuracy'])
    adam = Adam(lr=1e-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)

    model.compile(optimizer=adam, loss='binary_crossentropy', metrics=['accuracy'])
    print("Traning Model...")
    model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, callbacks=[checkpoint], validation_data=(X_test, y_test))  # starts training


    return ??

上面的代码使用X_trainY_train训练模型,然后对其进行测试。但是,在我的系统中,我没有Y_trainY_test,我只需要softmax层之前的最后一个隐藏层中的向量。我如何获得它?

1 个答案:

答案 0 :(得分:2)

为此,您可以定义一个后端函数来获取任意层的输出:

from keras import backend as K

func = K.function([model.input], [model.layers[index_of_layer].output])

您可以使用model.summary()查找所需图层的索引,其中列出的图层从索引零开始。如果您需要在最后一层之前的一层,则可以使用-2作为索引(即.layers属性实际上是一个列表,因此您可以像在python中的列表一样对其进行索引)。然后,您可以通过传递输入数组的列表来使用已定义的功能:

outputs = func(inputs)

或者,您也可以为此目的定义模型。 Keras documentation中对此进行了更全面的介绍,因此我建议您阅读。