Keras带Tensorflow中间层的分批萃取 [英] Keras w/ Tensorflow intermediate layer extraction in batches

查看:276
本文介绍了Keras带Tensorflow中间层的分批萃取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试利用已经受过训练的DL模型的中间层作为对给定输入的嵌入.下面的代码已经可以用来获取我想要的图层,但是对于大量输入而言,迭代地执行此操作非常慢.

I am currently trying to leverage an intermediate layer from my already trained DL model as an embedding to a given input. The code below already works at getting the layer I want, however it is extremely slow to do this iteratively for a large number of inputs.

model = load_model('model.h5')
inp = model.input
outputs = [layer.output for layer in model.layers]
functors = [K.function([inp]+ [K.learning_phase()], [out]) for out in outputs]

def text2tensor(text):
    """Convert string to tensor"""
    tensor = tokenizer.texts_to_sequences([text])
    tensor = pad_sequences(tensor, maxlen=10, padding='pre')
    return tensor

def get_embedding(tensor, at_layer):
    """Get output at particular layer in network """
    functors = [K.function([inp]+ [K.learning_phase()], [out]) for out in outputs][at_layer-1]
    layer_outs = [func([tensor, 1.]) for func in [functors]]
    return layer_outs[0][0]


texts = ['this is my first text',
         'this is my second text',
         'this is my third text',
         .....nth text]

embeddings = np.empty((0,256))
for t in texts:
    tensor = text2tensor(t)
    embedding = get_embedding(tensor,at_layer=4)
    embeddings = np.append(embeddings,[embedding[0]],axis=0)

我如何利用批处理,而不必一个接一个地执行此操作?使用上述实现速度非常慢,但是可以正常工作.

How do I make use of batch processing so that I don't have to do this one by one? It is extremely slow with the above implementation, but it works.

推荐答案

除了我在评论中提到的要点外,我建议您创建一个模型而不是后端函数:

In addition to the point I mentioned in my comment, I suggest you to create a model instead of a backend function:

input_tensor = Input(shape=(10,))   # assuming maxlen=10
new_model = Model(input_tensor, my_desired_layer.output)

然后,首先对文本数据进行预处理以形成输入数组(即下面的my_data),然后使用

Then, first pre-process your text data to form an input array (i.e. my_data below) and afterwards use predict method and pass a batch_size argument to it to exploit batch processing:

out = new_model.predict(my_data)   # the default batch size is 32

这篇关于Keras带Tensorflow中间层的分批萃取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆