gpt4 book ai didi

python - TensorFlow - nn.max_pooling 极大地增加了内存使用量

转载 作者:行者123 更新时间:2023-11-30 09:54:02 24 4
gpt4 key购买 nike

我尝试在 TensorFlow 中创建一个简单的卷积神经网络。当我运行下面的代码时,一切看起来都很好。我在 Spyder IDE 中运行它并监控内存使用情况 - 它在我的笔记本电脑上增长到 64-65%,并且不再继续增长。

batch_size = 16
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.
#Now instead of using strides = 2 for convolutions we will use maxpooling with
#same convolution sizes
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))

num_steps = 1001

with tf.Session(graph=graph) as session:
tf.initialize_all_variables().run()
print('Initialized')
for step in range(num_steps):
offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
batch_data = train_dataset[offset:(offset + batch_size), :, :, :]
batch_labels = train_labels[offset:(offset + batch_size), :]
feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}
_, l, predictions = session.run(
[optimizer, loss, train_prediction], feed_dict=feed_dict)
if (step % 50 == 0):
print('Minibatch loss at step %d: %f' % (step, l))
print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels))
print('Validation accuracy: %.1f%%' % accuracy(
valid_prediction.eval(), valid_labels))
print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels))

好吧,之后我尝试引入内核为2的maxpooling,并使用maxpooling而不是卷积层来减少数据的大小。如下所示:

conv = tf.nn.conv2d(data, layer1_weights, [1, 1, 1, 1], padding='SAME')
maxpool = tf.nn.max_pool(conv, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')
hidden = tf.nn.relu(maxpool + layer1_biases)
conv = tf.nn.conv2d(hidden, layer2_weights, [1, 2, 2, 1], padding='SAME')

其他一切保持不变。但是当我这样做时(注意,我只引入了一个 maxpooling 层),内存使用量增长到 100%,并且我的 iPython 内核就死掉了。对于这种奇怪的行为有什么想法,为什么内存使用量变得这么大?难道我做错了什么?关于如何减少内存使用有什么建议吗?

最佳答案

假设您在单 channel 6x6 输入上使用一个 3x3 滤波器。

当您进行步幅为 2 的步幅卷积时,您会产生 3x3 的结果。

So effectively you use input 36 units + filter 9 units + output 9 units of memory.

现在,当您尝试在无跨步卷积后应用最大池时,您的卷积层会产生 6x6 输出,您可以在其上应用 maxpool 以获得 3x3 输出。请注意,在应用最大池之前,我们在内存中有一个大小为 6x6 的中间结果。

So over here use input 36 units + filter 9 units + intermediate result 36 units + output 9 units of memory

这可以解释内存的使用情况。这并不是什么奇怪的行为。至于为什么它会完全耗尽您的资源取决于您的图像大小、批量大小和您使用的过滤器数量。

关于python - TensorFlow - nn.max_pooling 极大地增加了内存使用量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38384531/

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