第12章最適化アルゴリズム
12.5.2. Minibatches12.5.2 ミニバッチ
In the past we took it for granted that we would read minibatches of data rather than single observations to update parameters. We now give a brief justification for it. Processing single observations requires us to perform many single matrix-vector (or even vector-vector) multiplications, which is quite expensive and which incurs a significant overhead on behalf of the underlying deep learning framework. This applies both to evaluating a network when applied to data (often referred to as inference) and when computing gradients to update parameters. That is, this applies whenever we perform where
英語のヒント
これまでは、パラメータを更新するのに、単一の観測ではなくデータのミニバッチを読むことを当然と考えてきた。ここで、その理由を簡単に述べる。単一の観測を処理するには、単一の行列とベクトルの(あるいはベクトルとベクトルの)乗算をたくさん行う必要があり、それはかなり高くつき、土台にある深層学習のフレームワークの側で大きなオーバーヘッドを招く。これは、データにネットワークを当てはめて評価するとき(よく推論と呼ばれる)にも、パラメータを更新するために勾配を計算するときにも当てはまる。つまり、これは、を行うときにはいつでも当てはまる。ここで、勾配は次のとおりである。
(12.5.1)
We can increase the computational efficiency of this operation by applying it to a minibatch of observations at a time. That is, we replace the gradient over a single observation by one over a small batch
英語のヒント
この演算を、観測のミニバッチに一度に当てはめることで、計算の効率を上げられる。つまり、単一の観測についての勾配を、小さなバッチについての勾配に置き換える。
(12.5.2)
Let’s see what this does to the statistical properties of : since both and also all elements of the minibatch are drawn uniformly at random from the training set, the expectation of the gradient remains unchanged. The variance, on the other hand, is reduced significantly. Since the minibatch gradient is composed of independent gradients which are being averaged, its standard deviation is reduced by a factor of . This, by itself, is a good thing, since it means that the updates are more reliably aligned with the full gradient.
英語のヒント
これがの統計的な性質に何をもたらすかを見よう。もミニバッチのすべての要素も、訓練集合から一様にランダムに引かれるので、勾配の期待値は変わらない。一方、分散は大きく減る。ミニバッチの勾配は、平均される個の独立な勾配からなるので、その標準偏差は倍に減る。これはそれ自体よいことである。更新が完全な勾配ともっと確実にそろうことを意味するからである。
Naively this would indicate that choosing a large minibatch would be universally desirable. Alas, after some point, the additional reduction in standard deviation is minimal when compared to the linear increase in computational cost. In practice we pick a minibatch that is large enough to offer good computational efficiency while still fitting into the memory of a GPU. To illustrate the savings let’s have a look at some code. In it we perform the same matrix-matrix multiplication, but this time broken up into “minibatches” of 64 columns at a time.
英語のヒント
素朴に考えると、これは大きなミニバッチを選ぶことが常に望ましいことを示しているように見える。残念ながら、ある点を過ぎると、標準偏差がさらに減る分は、計算コストの線形な増加と比べてわずかになる。実際には、計算の効率がよく、しかもGPUのメモリに収まるくらいの大きさのミニバッチを選ぶ。節約を示すため、コードを見てみよう。そこでは同じ行列と行列の積を行うが、今回は一度に64列ずつの「ミニバッチ」に分ける。
timer.start()
for j in range(0, 256, 64):
A[:, j:j+64] = torch.mm(B, C[:, j:j+64])
timer.stop()
print(f'performance in Gigaflops: block {0.03 / timer.times[3]:.3f}')performance in Gigaflops: block 37.640As we can see, the computation on the minibatch is essentially as efficient as on the full matrix. A word of caution is in order. In Section 8.5 we used a type of regularization that was heavily dependent on the amount of variance in a minibatch. As we increase the latter, the variance decreases and with it the benefit of the noise-injection due to batch normalization. See e.g., Ioffe (2017) for details on how to rescale and compute the appropriate terms.