【问题标题】:Udacity Deep Learning Convolutional Neural Networks- TensorFlowUdacity 深度学习卷积神经网络 - TensorFlow
【发布时间】:2016-07-05 20:08:04
【问题描述】:

我一直在学习 Udacity 的深度学习课程——我必须补充一下,这很棒!我对迄今为止的任务感到非常满意。但是有两行代码,我不是很理解。

batch_size = 20
patch_size = 5
depth = 16
num_hidden = 64

graph = tf.Graph()

with graph.as_default():

  # Input data.
  tf_train_dataset = tf.placeholder(
    tf.float32, shape=(batch_size, image_size, image_size, num_channels))
  tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
  tf_valid_dataset = tf.constant(valid_dataset)
  tf_test_dataset = tf.constant(test_dataset)

  # Variables.
  layer1_weights = tf.Variable(tf.truncated_normal(
      [patch_size, patch_size, num_channels, depth], stddev=0.1))
  layer1_biases = tf.Variable(tf.zeros([depth]))
  layer2_weights = tf.Variable(tf.truncated_normal(
      [patch_size, patch_size, depth, depth], stddev=0.1))
  ***********************************************************
  layer2_biases = tf.Variable(tf.constant(1.0, shape=[depth]))
  ***********************************************************
  layer3_weights = tf.Variable(tf.truncated_normal(
      [image_size // 4 * image_size // 4 * depth, num_hidden], stddev=0.1))
  ***********************************************************
  layer3_biases = tf.Variable(tf.constant(1.0, shape=[num_hidden]))
  layer4_weights = tf.Variable(tf.truncated_normal(
      [num_hidden, num_labels], stddev=0.1))
  layer4_biases = tf.Variable(tf.constant(1.0, shape=[num_labels]))

  # Model.
  def model(data):
    conv = tf.nn.conv2d(data, layer1_weights, [1, 2, 2, 1], padding='SAME')
    hidden = tf.nn.relu(conv + layer1_biases)
    conv = tf.nn.conv2d(hidden, layer2_weights, [1, 2, 2, 1], padding='SAME')
    hidden = tf.nn.relu(conv + layer2_biases)
    shape = hidden.get_shape().as_list()
    reshape = tf.reshape(hidden, [shape[0], shape[1] * shape[2] * shape[3]])
    hidden = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases)
    return tf.matmul(hidden, layer4_weights) + layer4_biases

  # Training computation.
  logits = model(tf_train_dataset)
  loss = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels))

  # Optimizer.
  optimizer = tf.train.GradientDescentOptimizer(0.05).minimize(loss)

  # Predictions for the training, validation, and test data.
  train_prediction = tf.nn.softmax(logits)
  valid_prediction = tf.nn.softmax(model(tf_valid_dataset))
  test_prediction = tf.nn.softmax(model(tf_test_dataset))

我在我不太理解的代码部分周围加上了星号。首先,我不太清楚为什么输入层和卷积层之间的第一组偏差为零,然后在第二层它们全为 1。

接下来,看不懂下面这行代码:

layer3_weights = tf.Variable(tf.truncated_normal(
  [image_size // 4 * image_size // 4 * depth, num_hidden], stddev=0.1))

具体来说,我不明白我们为什么使用image_size // 4 * image_size // 4 * depth,尤其不明白我们为什么使用4。

如果您需要更多信息,请告诉我。这取自 Udacity 的深度学习课程,其中笔记本可以从 GitHub 克隆。

非常感谢:)

【问题讨论】:

  • 他们只是手工计算,参数空间的大小是多少,这是卷积中使用的参数的结果。它正在减小图像的大小(当您使用一些填充等移动内核时),在这种情况下 - 它减小了四倍的大小。它本身并不是一个神奇的常数——只是之前卷积层中使用的参数的结果。他们只需要匹配,仅此而已。
  • 好的,谢谢@lejlot。你知道他们为什么对初始偏差使用不同的值吗?
  • 我很确定它也适用于其他偏差初始化。它看起来完全随意
  • 我还在学习中,但输入层可能没有偏差吗?如果这是浅层神经网络中的输入层,则不会使用偏差。
  • 不,初始化为零并不能消除偏差。它就在那里,只是以另一种方式初始化。再次 - 没关系,两者都可以工作

标签: python machine-learning neural-network tensorflow deep-learning


【解决方案1】:

回答 [image_size // 4 * image_size // 4 * depth] 部分:

代码使用相同的填充两次应用卷积 -

In each convolution the output image is half of input size (since stride = 2)
Therefore size of output after first 2 convolution layers is :
(image_size / 4) * (image_size / 4) * depth

现在下一层是全连接层,因此该层的输入应该是平面图像而不是 3D 图像,因此,第 3 层的权重是 od 维度:

((image_size / 4) * (image_size / 4) * depth), num_hidden

【讨论】: