python - Udacity 深度学习卷积神经网络 - TensorFlow

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

我一直在研究 Udacity 的深度学习类(class) - 我必须补充一点,这非常棒!我对迄今为止的任务感到非常满意。但是有两行代码我不太明白。

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 * height,而且我特别不明白为什么我们使用4。

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

非常感谢:)

最佳答案

回答 [image_size//4 * image_size//4 * 深度] 部分:

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

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

关于python - Udacity 深度学习卷积神经网络 - TensorFlow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38211862/

相关文章:

python - 在 python 中使用 for 循环反转字符串 IndexError : string index out of range

python - opencv cv2.imshow自动向小图像添加填充

r - 如何找到支持向量机中的重要因素

neural-network - 在 PyTorch 中提取图像的编码表示?

python - 凯拉斯错误 "You must feed a value for placeholder tensor"

python - 如何在 python flask 中设置全局变量?

python - PyQt 按钮选择

neural-network - 在神经网络中进行自适应学习率时使用哪个乘法和加法因子?

python - sklearn - 模型保持过度拟合

machine-learning - 机器学习中的特征可以是矩阵吗?