我尝试在TensorFlow中创建一个简单的卷积神经网络。当我运行下面的代码时,一切看起来都很正常。我在Spyder IDE中运行它并监控内存使用情况 – 它在我的笔记本电脑上增长到64-65%,然后不再继续增长。
batch_size = 16patch_size = 5depth = 16num_hidden = 64graph = tf.Graph()with graph.as_default(): # 输入数据。 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) # 变量。 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])) # 模型。 # 现在我们不使用卷积的步长为2,而是使用最大池化与相同的卷积尺寸 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 # 训练计算。 logits = model(tf_train_dataset) loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels)) # 优化器。 optimizer = tf.train.GradientDescentOptimizer(0.05).minimize(loss) # 训练、验证和测试数据的预测。 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 = 1001with 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×2的最大池化核,并使用最大池化来减少数据大小,而不是使用卷积层。看起来像下面这样:
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')
其他部分保持不变。但是当我这样做时(注意,我只引入了一个最大池化层),内存使用量增长到100%,我的iPython内核就崩溃了。有什么关于这种奇怪行为的想法吗?为什么内存使用量会变得这么大?我做错了什么吗?有什么建议可以减少内存使用量吗?
回答:
假设你在一个单通道6×6的输入上使用一个3×3的滤波器。
当你进行步长为2的卷积时,你会产生一个3×3的结果。
所以实际上你使用了输入36个单位 + 滤波器9个单位 + 输出9个单位的内存。
现在当你在未步长的卷积后尝试应用最大池化时,你的卷积层会产生6×6的输出,然后你在这个输出上应用最大池化得到3×3的输出。请注意,在应用最大池化之前,我们有一个大小为6×6的中间结果在内存中。
所以在这里使用了输入36个单位 + 滤波器9个单位 + 中间结果36个单位 + 输出9个单位的内存
这可以解释内存使用情况。这不是奇怪的行为。至于为什么它完全耗尽了你的资源,这取决于你的图像大小、批次大小和你使用的滤波器数量。