keras中间层输出

https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate

One simple way is to create a new Model that will output the layers that you are interested in:
```python
from keras.models import Model

model = ...  # create the original model

layer_name = 'my_layer'
intermediate_layer_model = Model(inputs=model.input,
                                 outputs=model.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(data)
```
Alternatively, you can build a Keras function that will return the output of a certain layer given a certain input, for example:
```python
from keras import backend as K

# with a Sequential model
get_3rd_layer_output = K.function([model.layers[0].input],
                                  [model.layers[3].output])
layer_output = get_3rd_layer_output([x])[0]

Similarly, you could build a Theano and TensorFlow function directly.
```

Note that if your model has a different behavior in training and testing phase (e.g. if it uses ```Dropout```, ```BatchNormalization```, etc.), you will need to pass the learning phase flag to your function:
```python
get_3rd_layer_output = K.function([model.layers[0].input, K.learning_phase()],
                                  [model.layers[3].output])

# output in test mode = 0
layer_output = get_3rd_layer_output([x, 0])[0]

# output in train mode = 1
layer_output = get_3rd_layer_output([x, 1])[0]
```

=====================
```X``````model.layer[0].input]```

====================

Example:

```python
import keras
import sys
import numpy as np
from keras.layers import Input, Embedding, LSTM, Dense
from keras.models import Model
from keras import losses
# Headline input: meant to receive sequences of 100 integers, between 1 and 10000.
# Note that we can name any layer by passing it a "name" argument.
main_input = Input(shape=(100,), dtype='int32', name='main_input')

# This embedding layer will encode the input sequence
# into a sequence of dense 512-dimensional vectors.
x = Embedding(output_dim=512, input_dim=10000, input_length=100)(main_input)

# A LSTM will transform the vector sequence into a single vector,
# containing information about the entire sequence
lstm_out = LSTM(32, return_sequences=True, name='LSTM')(x)

model = Model(inputs=main_input, outputs=lstm_out)

model.compile(loss=losses.mean_squared_error, optimizer='sgd')

#set input and labels
num = 50
headline_data = np.random.randint(1,10000, size=(num,100))
additional_data = np.random.randint(1,10000, size=(num,5))
labels = np.random.randint(1,10000,size=(num,32))

#get the output of one intermediate layer 
intermediate_layer_model = Model(inputs=model.input,
                                 outputs=model.get_layer("LSTM").output)
intermediate_output = intermediate_layer_model.predict(headline_data)
print(intermediate_output.shape)
sys.exit()
#train
model.fit([headline_data, additional_data], [labels, labels],
          epochs=7, batch_size=32)
```









































































































































评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值