如何在卷积神经网络(CNN)中获得倒数第二层的值?

时间:2017-10-05 05:34:23

标签: python machine-learning deep-learning

我正在尝试为分类任务实施CNN。我想看看在每个时代如何优化权重。为此,我需要倒数第二层的值。此外,我将自己硬编码最后一层和反向传播。请推荐API也会有所帮助。

修改:我添加了keras示例中的代码。期待编辑它。 This链接提供了一些提示。我已经提到了我需要输出的图层。

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'))

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



model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          validation_data=(x_test, y_test))

1 个答案:

答案 0 :(得分:0)

您可以像这样获取模型的各个图层:

num_layer = 7 # Dense(1) layer
layer = model.layers[num_layer]
  

我想看看在每个时代如何优化权重。

要获得图层的权重,请使用layer.get_weights(),如下所示:

w, b = layer.get_weights() # weights and bias of Dense(1)
  

我需要倒数第二层的值。

要获得最后一层的评估值,请使用model.predict()

prediction = model.predict(x_test)

要获得任何其他图层的评估,请使用tensorflow进行评估:

input = tf.placeholder(tf.float32) # Create input placeholder
layer_output = layer(input) # create layer output operation

init_op = tf.global_variables_initializer() # initialize variables

with tf.Session() as sess:
    sess.run(init_op)

    # evaluate layer output
    output = sess.run(layer_output, feed_dict = {input: x_test})
    print(output)