为什么CNN中CNN的不同中间层输出?

时间:2017-10-23 09:12:07

标签: machine-learning deep-learning keras

我正在使用此代码执行一些实验,我想主要在CNN的完全连接层(或最后一层)之前使用层的中间层表示。

from __future__ import print_function

from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.layers import Embedding
from keras.layers import Conv1D, GlobalMaxPooling1D
from keras.datasets import imdb

# set parameters:
max_features = 5000
maxlen = 400
batch_size = 100
embedding_dims = 50
filters = 250
kernel_size = 3
hidden_dims = 250
epochs = 100

print('Loading data...')
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
print(len(x_train), 'train sequences')
print(len(x_test), 'test sequences')

print('Pad sequences (samples x time)')
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)

print('Build model...')
model = Sequential()

# we start off with an efficient embedding layer which maps
# our vocab indices into embedding_dims dimensions
model.add(Embedding(max_features,
                    embedding_dims,
                    input_length=maxlen))
model.add(Dropout(0.2))

# we add a Convolution1D, which will learn filters
# word group filters of size filter_length:
model.add(Conv1D(filters,
                 kernel_size,
                 padding='valid',
                 activation='relu',
                 strides=1))
# we use max pooling:
model.add(GlobalMaxPooling1D())

# We add a vanilla hidden layer:
model.add(Dense(hidden_dims))
model.add(Dropout(0.2))
model.add(Activation('relu'))#<======== I need output after this.

# We project onto a single unit output layer, and squash it with a sigmoid:
model.add(Dense(1))
model.add(Activation('sigmoid'))   

model.compile(loss='binary_crossentropy',
              optimizer='adam', metrics=['accuracy'])

为了得到倒数第二层的中间层表示,我使用了以下代码。

CODE1

get_layer_output = K.function([model.layers[0].input, K.learning_phase()],
                                  [model.layers[6].output])

# output in test mode = 0
layer_output_test = get_layer_output([x_test, 0])[0]

# output in train mode = 1
layer_output_train = get_layer_output([x_train, 1])[0]

print(layer_output_train)
print(layer_output_train.shape)

CODE2

def get_activations(model, layer, X_batch):
    get_activations = K.function([model.layers[0].input, K.learning_phase()], [model.layers[layer].output,])
    activations = get_activations([X_batch,1])
    return activations

import numpy as np
X_train=np.array(get_activations(model=model,layer=6, X_batch=x_train)[0], dtype=np.float32)
print(X_train)
print(X_train.shape)

哪一个是正确的,因为我正在获取/打印上述两个代码的不同输出?我想使用上面的正确输出乘以权重并通过自定义优化器进行优化。

2 个答案:

答案 0 :(得分:0)

如果您将 setkey(selecteddata, id) setkey(dta, id) selecteddata[dta] # do the merging 传递给1,您每次都会得到不同的结果。但是这两个代码都给出了相同的结果。

答案 1 :(得分:0)

使用更高级别的方法,您可以这样做:

from keras.models import Model

newModel = Model(model.inputs,model.layers[6].output)

使用newModel执行任何操作。您可以训练它(并影响原始模型),并使用它来预测值。