前几天我的程序的龟速运行让我自以为明白了机器学习的耗时、耗硬件,并且想要GPU……
但今天我稍微优化了一点性能:
原代码:
index = 0 def next_batch(n): global index if index+n>60000: index = 0 xs = np.float32(mnist.train_images()[index:index+n]).reshape((n,28**2))/255 ys = to_onehotv(mnist.train_labels()[index:index+n]) index += n #print(index) return xs,ys
优化后:
#将以下代码及部分其它代码单独放在mnistpack.py中 train_xs = np.float32(mnist.train_images()).reshape((60000,784))/255 mnist_train_labels = mnist.train_labels() index = 0 def next_batch(n): global index if index+n>60000: index = 0 xs = train_xs[index:index+n] ys = to_onehotv(mnist_train_labels[index:index+n]) index += n #print(index) return xs,ys
这样修改后运行速度快了大概百倍以上……以前看着它一个一个蹦,现在就是“刷”一下全出来了。看来之前的龟速不是因为训练耗时,而是每次都要在函数next_batch()
里完整载入一次mnist.train_images()
……
虽说过早的优化是万恶之源,但缺少必要的优化也很要命啊。