Keras conv1d 层参数:过滤器和 kernel_size
Keras conv1d layer parameters: filters and kernel_size
我对keras的conv1d层中的这两个参数感到很困惑:
https://keras.io/layers/convolutional/#conv1d
文档说:
filters: Integer, the dimensionality of the output space (i.e. the number output of filters in the convolution).
kernel_size: An integer or tuple/list of a single integer, specifying the length of the 1D convolution window.
但这似乎与我在许多教程中看到的标准术语无关,例如 https://adeshpande3.github.io/adeshpande3.github.io/A-Beginner's-Guide-To-Understanding-Convolutional-Neural-Networks/ and https://machinelearningmastery.com/sequence-classification-lstm-recurrent-neural-networks-python-keras/
使用使用 Keras 的第二个教程 link,我想实际上 'kernel_size' 与定义滑动 window 的常规 'filter' 概念相关在输入特征 space 上。但是conv1d中的'filter'参数呢?它有什么作用?
例如,在下面的代码片段中:
model.add(embedding_layer)
model.add(Dropout(0.2))
model.add(Conv1D(filters=100, kernel_size=4, padding='same', activation='relu'))
假设embedding层输出一个维度为50(行,每行是句子中的一个词)x 300(列,词向量维度)的矩阵,conv1d层如何变换该矩阵?
非常感谢
你说得对kernel_size
定义了滑动的大小window。
filters
参数就是您将拥有的不同 windows 数量。 (它们都具有相同的长度,即 kernel_size
)。您想要产生多少不同的结果或渠道。
当您使用 filters=100
和 kernel_size=4
时,您正在创建 100 个不同的过滤器,每个过滤器的长度为 4。结果将带来 100 个不同的卷积。
此外,每个过滤器都有足够的参数来考虑所有输入通道。
Conv1D 层需要这些维度:
(batchSize, length, channels)
我想最好的使用方法是在长度维度上有单词的数量(好像单词按顺序组成一个句子),通道是嵌入的输出维度(定义的数字一个词)。
所以:
batchSize = number of sentences
length = number of words in each sentence
channels = dimension of the embedding's output.
卷积层将通过 100 个不同的过滤器,每个过滤器将沿着 length
维度滑动(逐个单词,以 4 个为一组),考虑定义单词的所有通道。
输出的形状为:
(number of sentences, 50 words, 100 output dimension or filters)
过滤器的形状为:
(4 = length, 300 = word vector dimension, 100 output dimension of the convolution)
下面的解释代码可以帮助做到这一点。我去了类似的问题并自己回答了。
from tensorflow.keras.layers import MaxPool1D
import tensorflow.keras.backend as K
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Conv1D
tf.set_random_seed(1)
batch,rows,cols = 3,8,3
input_shape = (batch,rows,cols)
np.set_random_seed = 132
data = np.random.randint(low=1,high=6,size=input_shape,dtype='int32')
data = np.float32(data)
data = tf.constant(data)
print("Data:")
print(K.eval(data))
print()
print(f'm,n,k:{input_shape}')
from tensorflow.keras.layers import Conv1D
#############################
# Understandin filters and kernel_size
##############################
num_filters=5
kernel_size= 3
'''
Few Notes about Kernel_size:
1. max_kernel_size == max_rows
2. since Conv1D, we are creating 1D Matrix of 1's with kernel_size
if kernel_size = 1, [[1,1,1..]]
if kernel_size = 2, [[1,1,1..][1,1,1,..]]
if kernel_size = 3, [[1,1,1..][1,1,1,..]]
I have chosen tf.keras.initializers.constant(1) to create a matrix of Ones.
Size of matrix is Kernel_Size
'''
y= Conv1D(filters=num_filters,kernel_size=kernel_size,
kernel_initializer=tf.keras.initializers.constant(1),
#glorot_uniform(seed=12)
input_shape=(k,n)
)(data)
#########################
# Checking the out outcome
#########################
print(K.eval(y))
print(f' Resulting output_shape == (batch_size, num_rows-kernel_size+1,num_filters): {y.shape}')
# # Verification
K.eval(tf.math.reduce_sum(data,axis=(2,1), # Sum along axis=2, and then along
axis=1,keep_dims=True)
###########################################
# Understanding MaxPool and Strides in
##########################################
pool = MaxPool1D(pool_size=3,strides=3)(y)
print(K.eval(pool))
print(f'Shape of Pool: {pool.shape}')
我对keras的conv1d层中的这两个参数感到很困惑: https://keras.io/layers/convolutional/#conv1d
文档说:
filters: Integer, the dimensionality of the output space (i.e. the number output of filters in the convolution).
kernel_size: An integer or tuple/list of a single integer, specifying the length of the 1D convolution window.
但这似乎与我在许多教程中看到的标准术语无关,例如 https://adeshpande3.github.io/adeshpande3.github.io/A-Beginner's-Guide-To-Understanding-Convolutional-Neural-Networks/ and https://machinelearningmastery.com/sequence-classification-lstm-recurrent-neural-networks-python-keras/
使用使用 Keras 的第二个教程 link,我想实际上 'kernel_size' 与定义滑动 window 的常规 'filter' 概念相关在输入特征 space 上。但是conv1d中的'filter'参数呢?它有什么作用?
例如,在下面的代码片段中:
model.add(embedding_layer)
model.add(Dropout(0.2))
model.add(Conv1D(filters=100, kernel_size=4, padding='same', activation='relu'))
假设embedding层输出一个维度为50(行,每行是句子中的一个词)x 300(列,词向量维度)的矩阵,conv1d层如何变换该矩阵?
非常感谢
你说得对kernel_size
定义了滑动的大小window。
filters
参数就是您将拥有的不同 windows 数量。 (它们都具有相同的长度,即 kernel_size
)。您想要产生多少不同的结果或渠道。
当您使用 filters=100
和 kernel_size=4
时,您正在创建 100 个不同的过滤器,每个过滤器的长度为 4。结果将带来 100 个不同的卷积。
此外,每个过滤器都有足够的参数来考虑所有输入通道。
Conv1D 层需要这些维度:
(batchSize, length, channels)
我想最好的使用方法是在长度维度上有单词的数量(好像单词按顺序组成一个句子),通道是嵌入的输出维度(定义的数字一个词)。
所以:
batchSize = number of sentences
length = number of words in each sentence
channels = dimension of the embedding's output.
卷积层将通过 100 个不同的过滤器,每个过滤器将沿着 length
维度滑动(逐个单词,以 4 个为一组),考虑定义单词的所有通道。
输出的形状为:
(number of sentences, 50 words, 100 output dimension or filters)
过滤器的形状为:
(4 = length, 300 = word vector dimension, 100 output dimension of the convolution)
下面的解释代码可以帮助做到这一点。我去了类似的问题并自己回答了。
from tensorflow.keras.layers import MaxPool1D
import tensorflow.keras.backend as K
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Conv1D
tf.set_random_seed(1)
batch,rows,cols = 3,8,3
input_shape = (batch,rows,cols)
np.set_random_seed = 132
data = np.random.randint(low=1,high=6,size=input_shape,dtype='int32')
data = np.float32(data)
data = tf.constant(data)
print("Data:")
print(K.eval(data))
print()
print(f'm,n,k:{input_shape}')
from tensorflow.keras.layers import Conv1D
#############################
# Understandin filters and kernel_size
##############################
num_filters=5
kernel_size= 3
'''
Few Notes about Kernel_size:
1. max_kernel_size == max_rows
2. since Conv1D, we are creating 1D Matrix of 1's with kernel_size
if kernel_size = 1, [[1,1,1..]]
if kernel_size = 2, [[1,1,1..][1,1,1,..]]
if kernel_size = 3, [[1,1,1..][1,1,1,..]]
I have chosen tf.keras.initializers.constant(1) to create a matrix of Ones.
Size of matrix is Kernel_Size
'''
y= Conv1D(filters=num_filters,kernel_size=kernel_size,
kernel_initializer=tf.keras.initializers.constant(1),
#glorot_uniform(seed=12)
input_shape=(k,n)
)(data)
#########################
# Checking the out outcome
#########################
print(K.eval(y))
print(f' Resulting output_shape == (batch_size, num_rows-kernel_size+1,num_filters): {y.shape}')
# # Verification
K.eval(tf.math.reduce_sum(data,axis=(2,1), # Sum along axis=2, and then along
axis=1,keep_dims=True)
###########################################
# Understanding MaxPool and Strides in
##########################################
pool = MaxPool1D(pool_size=3,strides=3)(y)
print(K.eval(pool))
print(f'Shape of Pool: {pool.shape}')