为什么在喀拉拉邦,CNN的中间层输出不同? [英] Why different intermediate layer ouput of CNN in keras?

查看:240
本文介绍了为什么在喀拉拉邦,CNN的中间层输出不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using this code to perform some experiment, I want to use intermediate layer representation of layer mainly before the fully connected layer(or last layer) of 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'])

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

To get the intermediate layer representation of penultimate layer I used following code.

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)

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

Which one is correct as I am getting/printing different output for above two codes? I want to use the above correct output to multiply by weights and optimise by custom optimiser.

推荐答案

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

Using a higher level approach, you can do this:

from keras.models import Model

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

使用newModel做任何您想做的事情.您可以训练它(并影响原始模型),并用它来预测值.

Do whatever you want with newModel. You can train it (and affect the original model), and use it to predict values.

这篇关于为什么在喀拉拉邦,CNN的中间层输出不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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