我一直在研究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克隆。
非常感谢:)
答案 0 :(得分:0)
回答[image_size // 4 * image_size // 4 *深度]部分:
该代码使用SAME填充两次应用卷积 -
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