gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:21:12 25 4
gpt4 key购买 nike

我一直在研究 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com