如何获得卷积神经网络倒数第二层的值?

2024-04-26 12:24:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试执行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))

Tags: fromtestimportaddsizemodeltrainembedding
1条回答
网友
1楼 · 发布于 2024-04-26 12:24:05

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

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

I want to see the how the weights are being optimized at each epoch.

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

^{pr2}$

I need the values of penultimate layer.

要获得最后一层的计算值,请使用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)

相关问题 更多 >