Deep Learning 5 - Enhance performance with batch processing
We’ve learned a multiple neural network, but there is one problem - processing time. You can use batch processing to reduce it.
Batch processing is to execute a program by a set of data (batch). Most of libraries are highly optimized for array calculation and it is suitable for deep learning.
Let’s change the logic of accuracy calculation from 04_recognition.py in Deep Learning 4.
04_recognition.py
x, t = get_data()
network = init_network()
accuracy_cnt = 0
for i in range(len(x)):
y = predict(network, x[i])
p = np.argmax(y)
if p == t[i]:
accuracy_cnt += 1
print("Accuracy:" + str(float(accuracy_cnt) / len(x)))
05_batch.py
x, t = get_data()
network = init_network()
batch_size = 100
accuracy_cnt = 0
for i in range(0, len(x), batch_size):
x_batch = x[i:i+batch_size]
y_batch = predict(network, x_batch)
p = np.argmax(y_batch, axis=1)
accuracy_cnt += np.sum(p == t[i:i+batch_size])
print("Accuracy:" + str(float(accuracy_cnt) / len(x)))
The step is to divide the data in the batch size, predict them with each array, extract the maximum value, and calculate accuracy. It is repeated in each batch. As a result, you can get accuracy faster! This dataset is not so large, but batch processing exerts an effect in time when you manipulate big data.
The sample code is here.