简体   繁体   English

批大小如何影响神经网络中的时间执行?

[英]How does batch size impact time execution in neural networks?

I am using CNN (convolution neural network) model to train cifar10. 我正在使用CNN(卷积神经网络)模型来训练cifar10。 I tried to change the batch size at each execution to see its impact on time 我尝试在每次执行时更改批处理大小,以查看其对时间的影响

My conclusion was : the bigger the batch size the more time the model took to be executed. 我的结论是:批量越大,执行模型所需的时间越多。

Does this seem logical because at the end of every batch we apply back propagation algorithm, meaning with larger batch size we apply less gradient descent so logically we should have less execution time. 这看起来是否合乎逻辑,因为在每批处理结束时,我们都应用反向传播算法,这意味着在批处理较大的情况下,我们将应用较少的梯度下降,因此从逻辑上讲,我们应该有更少的执行时间。

I found the opposite. 我发现相反。 What do you think guys ! 你们觉得怎么样 ! Thanks 谢谢

Here is my session code : 这是我的会话代码:

       with tf.Session() as sess:
         sess.run(init)
         summary_writer =tf.summary.FileWriter(logs_path,graph=tf.get_default_graph())
        start_time = time.time()
        for i in range(iteration_number):
          j = (i - epoch) * batch_size % number_of_examples
          k= (i - epoch + 1) * batch_size % number_of_examples
          if (k < j): # THE END OF DATA SET ------------------------
            k = number_of_examples
            batch_x = train_images[j:number_of_examples, :]
            batch_y = train_labels[j:number_of_examples, :]
            print("Iter " + str(i) + ", epoch Loss= " + \
                    "{:.6f}".format(loss) + ", Training Accuracy= " + \
                    "{:.5f}".format(acc))

            data = numpy.concatenate((train_images, train_labels), axis=1)
            numpy.random.shuffle(data)
            train_images = data[:, :3072]
            train_labels = data[:, 3072:3082]
            epoch = i + 1

          else:
            batch_x = train_images[j:k, :]
            batch_y = train_labels[j:k, :]
            loss, acc, summary = sess.run([cost, accuracy, merged_summary_op], feed_dict={x: batch_x,
                                                        y: batch_y,
                                                        keep_prob: 0.3})
            summary_writer.add_summary(summary)

            sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,
                                        keep_prob: dropout})

The batch size basically indicates how often you want to adjust the weights for your neural network. 批次大小基本上表示您要多长时间调整一次神经网络的权重。 A batch size of 1 would mean you give your NN 1 input and output pair, propagate the network with the inputs, calculate the error and adjust the weights. 批次大小为1意味着您将给NN 1输入和输出对,使用输入传播网络,计算误差并调整权重。 If you have a batch size the same as your datasets size, the NN will propagate all the input output pairs and add up the error and adjust the weights in the end. 如果批次大小与数据集大小相同,则NN将传播所有输入输出对,并加总误差并最终调整权重。 using a batch size that big usually gives you less accurate results but they are better suiting the average, you could say the outputs get kind of blurred out to avoid extremely big errors for some data and extremely small error for other data. 使用大的批次大小通常会给您带来不太准确的结果,但它们更适合平均值,您可以说输出会变得模糊不清,以避免某些数据出现极大的误差,而对其他数据产生非常小的误差。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM