【发布时间】:2020-06-26 16:15:15
【问题描述】:
我刚刚完成了 ANN 课程并开始学习 CNN。我对 CNN 中的 padding 和 stride 操作有基本的了解。
但是很难将输入图像与第一个 conv 层中的神经元映射,但我有基本的 了解输入特征如何映射到 ANN 中的第一个隐藏层。
理解输入图像与第一卷积层神经元之间映射的最佳方式是什么?
如何澄清我对以下代码示例的疑问?代码取自 Coursera 的 DL 课程。
def initialize_parameters():
"""
Initializes weight parameters to build a neural network with tensorflow. The shapes are:
W1 : [4, 4, 3, 8]
W2 : [2, 2, 8, 16]
Returns:
parameters -- a dictionary of tensors containing W1, W2
"""
tf.set_random_seed(1) # so that your "random" numbers match ours
### START CODE HERE ### (approx. 2 lines of code)
W1 = tf.get_variable("W1",[4,4,3,8],initializer = tf.contrib.layers.xavier_initializer(seed = 0))
W2 = tf.get_variable("W2",[2,2,8,16],initializer = tf.contrib.layers.xavier_initializer(seed = 0))
### END CODE HERE ###
parameters = {"W1": W1,
"W2": W2}
return parameters
def forward_propagation(X, parameters):
"""
Implements the forward propagation for the model:
CONV2D -> RELU -> MAXPOOL -> CONV2D -> RELU -> MAXPOOL -> FLATTEN -> FULLYCONNECTED
Arguments:
X -- input dataset placeholder, of shape (input size, number of examples)
parameters -- python dictionary containing your parameters "W1", "W2"
the shapes are given in initialize_parameters
Returns:
Z3 -- the output of the last LINEAR unit
"""
# Retrieve the parameters from the dictionary "parameters"
W1 = parameters['W1']
W2 = parameters['W2']
### START CODE HERE ###
# CONV2D: stride of 1, padding 'SAME'
Z1 = tf.nn.conv2d(X,W1, strides = [1,1,1,1], padding = 'SAME')
# RELU
A1 = tf.nn.relu(Z1)
# MAXPOOL: window 8x8, sride 8, padding 'SAME'
P1 = tf.nn.max_pool(A1, ksize = [1,8,8,1], strides = [1,8,8,1], padding = 'SAME')
# CONV2D: filters W2, stride 1, padding 'SAME'
Z2 = tf.nn.conv2d(P1,W2, strides = [1,1,1,1], padding = 'SAME')
# RELU
A2 = tf.nn.relu(Z2)
# MAXPOOL: window 4x4, stride 4, padding 'SAME'
P2 = tf.nn.max_pool(A2, ksize = [1,4,4,1], strides = [1,4,4,1], padding = 'SAME')
# FLATTEN
P2 = tf.contrib.layers.flatten(P2)
# FULLY-CONNECTED without non-linear activation function (not not call softmax).
# 6 neurons in output layer. Hint: one of the arguments should be "activation_fn=None"
Z3 = tf.contrib.layers.fully_connected(P2, 6,activation_fn=None)
### END CODE HERE ###
return Z3
with tf.Session() as sess:
np.random.seed(1)
X, Y = create_placeholders(64, 64, 3, 6)
parameters = initialize_parameters()
Z3 = forward_propagation(X, parameters)
init = tf.global_variables_initializer()
sess.run(init)
a = sess.run(Z3, {X: np.random.randn(1,64,64,3), Y: np.random.randn(1,6)})
print("Z3 = " + str(a))
这个大小为64*64*3的输入图像是如何被8个每个大小为4*4*3的filter处理的?
stride = 1,padding = 相同,batch_size = 1。
到目前为止,我所了解的是,第一个 conv 层中的每个神经元将有 8 个过滤器,每个过滤器的大小为 4*4*3。第一个卷积层中的每个神经元将获取与滤波器大小(这里为 4*4*3)相同的输入图像的一部分,并应用卷积操作并产生 8 个 64*64 的特征映射。
如果我的理解是正确的,那么:
1> 为什么我们需要跨步操作,因为每个神经元进行的内核大小和部分输入图像是相同的,如果我们应用 stride = 1(或 2),那么输入图像部分的边界是交叉的,这是我们不知道的需要对吗?
2> 我们如何知道输入图像的哪一部分(与内核大小相同)映射到第一个 conv 层中的哪个神经元?
如果没有,那么:
3> 输入图像如何在第一个卷积层的神经元上传递,Is 是完整的输入图像如何传递给每个神经元(就像在全连接 ANN 中,所有输入特征都映射到第一个隐藏层中的每个神经元) ?
或输入图像的一部分?我们如何知道输入图像的哪一部分映射到第一个卷积层中的哪个神经元?
4> 上面示例中指定的内核数(W1= [4, 4, 3, 8])是每个神经元还是第一个卷积层中的内核总数?
5> 我们如何知道上述示例在第一个卷积层中使用的神经元。
6> 神经元个数和kernel first conv layer的个数有关系吗?
【问题讨论】:
标签: tensorflow machine-learning artificial-intelligence conv-neural-network