【发布时间】:2020-11-23 17:06:57
【问题描述】:
我正在使用卷积神经网络进行多类文本分类,我在手套嵌入权重上应用了以下代码,我得到了很好的结果,但我对 CONV2D 形状有疑问: 为什么在 CONV2D 1 中我们得到 conv_1 (None, 407, 1, 64) 和分别在 conv_2 :None, 406, 1, 64) 和 conv_2 :405 中?
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_18 (InputLayer) (None, 409) 0
__________________________________________________________________________________________________
embedding_17 (Embedding) (None, 409, 100) 1766600 input_18[0][0]
__________________________________________________________________________________________________
reshape_10 (Reshape) (None, 409, 100, 1) 0 embedding_17[0][0]
__________________________________________________________________________________________________
conv_1 (Conv2D) (None, 407, 1, 64) 19264 reshape_10[0][0]
__________________________________________________________________________________________________
conv_2 (Conv2D) (None, 406, 1, 64) 25664 reshape_10[0][0]
__________________________________________________________________________________________________
conv_3 (Conv2D) (None, 405, 1, 64) 32064 reshape_10[0][0]
__________________________________________________________________________________________________
max_pooling2d_16 (MaxPooling2D) (None, 1, 1, 64) 0 conv_1[0][0]
__________________________________________________________________________________________________
max_pooling2d_17 (MaxPooling2D) (None, 1, 1, 64) 0 conv_2[0][0]
__________________________________________________________________________________________________
max_pooling2d_18 (MaxPooling2D) (None, 1, 1, 64) 0 conv_3[0][0]
__________________________________________________________________________________________________
concatenate_6 (Concatenate) (None, 3, 1, 64) 0 max_pooling2d_16[0][0]
max_pooling2d_17[0][0]
max_pooling2d_18[0][0]
__________________________________________________________________________________________________
flatten_5 (Flatten) (None, 192) 0 concatenate_6[0][0]
__________________________________________________________________________________________________
dropout_11 (Dropout) (None, 192) 0 flatten_5[0][0]
__________________________________________________________________________________________________
dense_11 (Dense) (None, 3) 579 dropout_11[0][0]
==================================================================================================
Total params: 1,844,171
Trainable params: 1,844,171
Non-trainable params: 0
__________________________________________________________________________________________________
None
from keras.layers import Dense, Input, GlobalMaxPooling1D
from keras.layers import Conv1D, MaxPooling1D, Embedding
from keras.models import Model
from keras.layers import Input, Dense, Embedding, Conv2D, MaxPooling2D, Dropout,concatenate
from keras.layers.core import Reshape, Flatten
from keras.callbacks import EarlyStopping
from keras.optimizers import Adam
from keras.models import Model
from keras import regularizers
sequence_length = 409
filter_sizes = [3,4,5]
num_filters = 64
drop = 0.5
EMBEDDING_DIM=100
embedding_layer = Embedding(len(tokenizer.word_index) + 1,
EMBEDDING_DIM,
weights=[embedding_matrix],
trainable=True)
inputs = Input(shape=(sequence_length,))
embedding = embedding_layer(inputs)
reshape = Reshape((sequence_length,EMBEDDING_DIM,1))(embedding)
conv_0 = Conv2D(num_filters, (filter_sizes[0], EMBEDDING_DIM),activation='relu',kernel_regularizer=regularizers.l2(0.01),name='conv_1')(reshape)
conv_1 = Conv2D(num_filters, (filter_sizes[1], EMBEDDING_DIM),activation='relu',kernel_regularizer=regularizers.l2(0.01),name='conv_2')(reshape)
conv_2 = Conv2D(num_filters, (filter_sizes[2], EMBEDDING_DIM),activation='relu',kernel_regularizer=regularizers.l2(0.01),name='conv_3')(reshape)
maxpool_0 = MaxPooling2D((sequence_length - filter_sizes[0] + 1, 1), strides=(1,1))(conv_0)
maxpool_1 = MaxPooling2D((sequence_length - filter_sizes[1] + 1, 1), strides=(1,1))(conv_1)
maxpool_2 = MaxPooling2D((sequence_length - filter_sizes[2] + 1, 1), strides=(1,1))(conv_2)
merged_tensor = concatenate([maxpool_0, maxpool_1, maxpool_2], axis=1)
flatten = Flatten()(merged_tensor)
reshape = Reshape((3*num_filters,))(flatten)
dropout = Dropout(drop)(flatten)
output = Dense(units=3, activation='softmax',kernel_regularizer=regularizers.l2(0.01))(dropout)
# this creates a model that includes
model = Model(inputs, output)
adam = Adam(lr=1e-3)
model.compile(loss='categorical_crossentropy',
optimizer=adam,
metrics=['acc'])
print(model.summary())
【问题讨论】:
标签: python keras deep-learning conv-neural-network