Python Keras w/Tensorflow中间层分批萃取
我目前正试图利用我已经训练过的DL模型中的中间层作为对给定输入的嵌入。下面的代码已经可以获得我想要的层,但是对于大量输入,迭代地执行此操作非常缓慢Python Keras w/Tensorflow中间层分批萃取,python,tensorflow,keras,deep-learning,batch-processing,Python,Tensorflow,Keras,Deep Learning,Batch Processing,我目前正试图利用我已经训练过的DL模型中的中间层作为对给定输入的嵌入。下面的代码已经可以获得我想要的层,但是对于大量输入,迭代地执行此操作非常缓慢 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 output
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)
我如何利用批处理,使我不必一个接一个地做这件事?上面的实现速度非常慢,但它可以工作。除了我在评论中提到的一点,我建议您创建一个模型,而不是后端函数:
input_tensor = Input(shape=(10,)) # assuming maxlen=10
new_model = Model(input_tensor, my_desired_layer.output)
然后,首先预处理文本数据以形成输入数组,即下面的my_数据,然后使用方法并将batch_size参数传递给它以利用批处理:
out = new_model.predict(my_data) # the default batch size is 32
如果您事先知道要获得其输出的层,那么为什么要获得所有层的输出?为什么要为每个输出张量创建一个函数?非常好!我会解决这个问题,我没想过。但不幸的是,这无助于我的批处理问题。谢谢,不过这很有帮助。