简体   繁体   English

Keras 中间层输出

[英]Keras intermediate layers output

I'm trying to get the intermediate layers output when using functional API of Keras.我试图在使用 Keras 的功能 API 时获得中间层输出。 I'm able to get the output when using the standard Sequential API, but not with the functional API.使用标准 Sequential API 时我能够获得输出,但不能使用功能 API。

I'm working on this working toy example:我正在研究这个工作玩具示例:

from keras.models import Sequential
from keras.layers import Input, Dense,TimeDistributed
from keras.models import Model
from keras.layers import Dense, LSTM, Bidirectional,Masking

inputs = [[[0,0,0],[0,0,0],[0,0,0],[0,0,0]],[[1,2,3],[4,5,6],[7,8,9],[10,11,12]],[[10,20,30],[40,50,60],[70,80,90],[100,110,120]]]

model = Sequential()
model.add(Masking(mask_value=0., input_shape = (4,3)))
model.add(Bidirectional(LSTM(3,return_sequences = True),merge_mode='concat'))
model.add(TimeDistributed(Dense(3,activation = 'softmax')))


print "First layer:"
intermediate_layer_model = Model(input=model.input,output=model.layers[0].output)
print intermediate_layer_model.predict(inputs)
print ""
print "Second layer:"
intermediate_layer_model = Model(input=model.input,output=model.layers[1].output)
print intermediate_layer_model.predict(inputs)
print ""
print "Third layer:"
intermediate_layer_model = Model(input=model.input,output=model.layers[2].output)
print intermediate_layer_model.predict(inputs)

But if I use the functional API, it doesn't work.但是如果我使用函数式 API,它就不起作用。 The outputs are not correct.输出不正确。 For example it is outputting the initial input in the second layer:例如它在第二层输出初始输入:

inputs_ = Input(shape=(4,3))
x = Masking(mask_value=0., input_shape = (4,3))(inputs_)
x = Bidirectional(LSTM(3,return_sequences = True),merge_mode='concat')(x)
predictions = TimeDistributed(Dense(3,activation = 'softmax'))(x)
model2 = Model(input=inputs_, output=predictions)

print "First layer:"
intermediate_layer_model = Model(input=model2.input,output=model2.layers[0].output)
print intermediate_layer_model.predict(inputs)
print ""
print "Second layer:"
intermediate_layer_model = Model(input=model2.input,output=model2.layers[1].output)
print intermediate_layer_model.predict(inputs)
print ""
print "Third layer:"
intermediate_layer_model = Model(input=model2.input,output=model2.layers[2].output)
print intermediate_layer_model.predict(inputs)

ANSWER: Apparently when using the functional API layer 0 is the input itself.答案:显然,当使用功能性 API 层时,0 层是输入本身。 And so everything is shifted one position forward.所以一切都向前移动了一个位置。

The issue arise from the fact, as the OP suggested, that the layer with index 0 (ie model.layers[0] ) corresponds to the input layer: "when using the functional API layer 0 is the input itself. And so everything is shifted one position forward."问题源于这样一个事实,正如 OP 所建议的那样,索引为 0 的层(即model.layers[0] )对应于输入层:“当使用功能性 API 层 0 是输入本身。所以一切都是向前移动了一个位置。”

Note: this answer is posted as community wiki as suggested in accepted answer of "Question with no answers, but issue solved in the comments (or extended in chat)" .注意:此答案作为社区维基发布,如已接受“问题没有答案,但问题已在评论中解决(或在聊天中扩展)”中的建议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM