keras中Convolution1D的使用(CNN情感分析yoom例子四) && Keras 1D,2D,3D卷积

这篇文章主要说明两个东西,一个是Convolution1D的介绍,另一个是model.summary()的使用。

首先我先说下model.summary(),此方法可以打印出模型的信息,读者可以查看每层输出内容。

接下来就说下Convolution1D的使用了,Convolution1D一维卷积,主要用于过滤一维输入的相邻元素,官方文档是这样的

1
keras.layers.convolutional.Convolution1D(nb_filter, filter_length, init = 'glorot_uniform' , activation = None , weights = None , border_mode = 'valid' , subsample_length = 1 , W_regularizer = None , b_regularizer = None , activity_regularizer = None , W_constraint = None , b_constraint = None , bias = True , input_dim = None , input_length = None )

然后官方给出的事例是这样的

复制代码
# apply a convolution 1d of length 3 to a sequence with 10 timesteps,
# with 64 output filters
model = Sequential()
model.add(Convolution1D(64, 3, border_mode='same', input_shape=(10, 32)))
# now model.output_shape == (None, 10, 64)

# add a new conv1d on top
model.add(Convolution1D(32, 3, border_mode='same'))
# now model.output_shape == (None, 10, 32)
复制代码

然后用print(model.summary())输出是这样的:

 

   下面我就围绕着上面代码简单介绍下:当把该层作为首层时,需要说明 input_shape

input_shape=(10, 32)简而言之就是10个32维的向量了,nb_filter : 卷积核的数量,也是输出的维度。filter_length : 每个过滤器的长度。
首先我们先看第一个卷积层,输出shape很容易理解,因为有64个卷积核,所以输出也就是64,接下来我们看下参数:其实可以这么理解,我们把例子中(10,32)的信号进行1D卷积相当于对其进行卷积核为(filter_length, 32)的2D卷积

为什么是10X64??

原10X32  加pad变为12X32,经过一个3X32的卷积核,变成10个数字组成的特征图。


FROM:http://blog.csdn.net/lyb3b3b/article/details/72904428


kears 1D,2D,3D都是卷积操作。他们处理的都是input tensor前几个维度的信息。

其中1D主要用于NLP中的N_gram,2D和3D卷积用于图像。其中2D卷积处理的是一张图像,3D卷积处理的就是多张图像。3D卷积考虑时间维度的信息。

官方给出的1D卷积的例子,处理输入第一个维度的信息。

# apply a convolution 1d of length 3 to a sequence with 10 timesteps,
# with 64 output filters
model = Sequential()
model.add(Convolution1D(64, 3, border_mode='same', input_shape=(10, 32)))
# now model.output_shape == (None, 10, 64)

# add a new conv1d on top
model.add(Convolution1D(32, 3, border_mode='same'))
# now model.output_shape == (None, 10, 32)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Keras2D卷积就是熟悉的图像卷积操作

kears3D卷积请参考传送门


FROM:  http://blog.csdn.net/zeroqiaoba/article/details/72864071

猜你喜欢

转载自blog.csdn.net/han____shuai/article/details/78494667