Difference between revisions of "Keras: Difference Between a Batch and an Epoch"

From OnnoWiki
Jump to navigation Jump to search
Line 55: Line 55:
 
==Apakah Batch?==
 
==Apakah Batch?==
  
Ukuran batch adalah hyperparameteryang menentukan jumlah sampel untuk dikerjakan sebelum memperbarui parameter model internal.
+
Ukuran batch adalah hyperparameter yang menentukan jumlah sampel untuk dikerjakan sebelum memperbarui parameter model internal.
  
 
Bayangkan batch sebagai iterasi for-loop pada satu atau lebih sampel dan membuat prediksi. Pada akhir batch, prediksi dibandingkan dengan variabel output yang diharapkan dan error dihitung. Dari error ini, algoritma pembaruan digunakan untuk meningkatkan model, mis. bergerak ke bawah sepanjang gradien error.
 
Bayangkan batch sebagai iterasi for-loop pada satu atau lebih sampel dan membuat prediksi. Pada akhir batch, prediksi dibandingkan dengan variabel output yang diharapkan dan error dihitung. Dari error ini, algoritma pembaruan digunakan untuk meningkatkan model, mis. bergerak ke bawah sepanjang gradien error.
Line 73: Line 73:
 
Ini bisa dan memang sering terjadi ketika training sebuah model. Ini berarti bahwa batch akhir memiliki sampel lebih sedikit daripada batch lainnya.
 
Ini bisa dan memang sering terjadi ketika training sebuah model. Ini berarti bahwa batch akhir memiliki sampel lebih sedikit daripada batch lainnya.
  
Alternately, you can remove some samples from the dataset or change the batch size such that the number of samples in the dataset does divide evenly by the batch size.
+
Sebagai alternatif, kita dapat menghapus beberapa sampel dari dataset atau mengubah ukuran batch sehingga jumlah sampel dalam dataset terbagi rata dengan ukuran batch.
  
For more on the differences between these variations of gradient descent, see the post:
+
Untuk lebih lanjut tentang perbedaan antara variasi gradient descent ini, lihat tulisan:
  
    A Gentle Introduction to Mini-Batch Gradient Descent and How to Configure Batch Size
+
* [[Keras: Introduction to Mini-Batch Gradient Descent]]
  
For more on the effect of batch size on the learning process, see the post:
+
Untuk lebih lanjut tentang pengaruh ukuran batch pada proses learning, lihat tulisan:
  
 
     How to Control the Speed and Stability of Training Neural Networks Batch Size
 
     How to Control the Speed and Stability of Training Neural Networks Batch Size
  
A batch involves an update to the model using samples; next, let’s look at an epoch.
+
Batch melibatkan pembaruan ke model menggunakan sampel; selanjutnya, mari kita lihat epoch.
  
 
==What Is an Epoch?==
 
==What Is an Epoch?==

Revision as of 11:49, 3 September 2019

Sumber: https://machinelearningmastery.com/difference-between-a-batch-and-an-epoch/


Stochastic gradient descent adalah learning algorithm dengan beberapa hyperparameter.

Dua hyperparameter yang sering membingungkan para pemula adalah ukuran batch dan jumlah epoch. Keduanya adalah nilai integer dan tampaknya melakukan hal yang sama.

Dalam tulisan ini, anda akan menemukan perbedaan antara batch dan epoch dalam stochastic gradient descent.

Sesudah membaca tulisan ini, anda akan mengetahui:

  • Stochastic gradient descent adalah algoritma pembelajaran iteratif yang menggunakan dataset training untuk meng-update model.
  • Ukuran batch adalah hyperparameter dari gradient descent yang mengontrol jumlah sampel training untuk dikerjakan sebelum parameter internal model diperbarui.
  • Jumlah epoch adalah hyperparameter dari gradient descent yang mengontrol jumlah lintasan lengkap yang harus dilalui oleh dataset training.

Overview

Posting ini dibagi menjadi lima bagian; yaitu:

  • Stochastic Gradient Descent
  • Apakah Sample?
  • Apakah a Batch?
  • Apakah Epoch?
  • Apa beda Batch dan Epoch?

Stochastic Gradient Descent

Stochastic Gradient Descent, atau disingkat SGD, adalah algoritma pengoptimalan yang digunakan untuk melatih algoritma machine learning, terutama artificial neural network yang digunakan dalam deep learning.

Tugas algoritma adalah untuk menemukan satu set parameter model internal yang berkinerja baik terhadap beberapa ukuran kinerja seperti loss logaritmik atau mean squared error.

Optimasi adalah jenis proses pencarian dan kita dapat menganggap pencarian ini sebagai pembelajaran. Algoritma optimisasi disebut "gradient descent", di mana "gradient" mengacu pada perhitungan gradien kesalahan atau kemiringan kesalahan dan "descent" mengacu pada bergerak turun sepanjang kemiringan menuju beberapa tingkat kesalahan minimum.

Algoritma ini bersifat iteratif. Ini berarti bahwa proses pencarian terjadi pada beberapa langkah diskrit, setiap langkah akan berusaha untuk sedikit meningkatkan parameter model.

Setiap langkah melibatkan penggunaan model dengan set parameter internal saat itu untuk membuat prediksi pada beberapa sampel, membandingkan prediksi dengan hasil yang diharapkan, menghitung kesalahan, dan menggunakan kesalahan untuk memperbarui parameter model internal.

Prosedur pembaruan ini berbeda untuk algoritma yang berbeda, tetapi dalam kasus artificial neural network, backpropagation update algorithm digunakan.

Sebelum kita menyelami batch dan epoch, mari kita lihat apa yang dimaksud dengan sampel.

Untuk belajar lebih lanjut dapat membaca2:

Apakah Sample?

Sampel adalah satu baris data.

Ini berisi input yang dimasukkan ke dalam algoritma dan output yang digunakan untuk membandingkan dengan prediksi dan menghitung kesalahan.

Dataset training terdiri dari banyak baris data, mis. banyak sampel. Sampel juga dapat disebut instance, observasi, vektor input, atau vektor feature.

Apakah Batch?

Ukuran batch adalah hyperparameter yang menentukan jumlah sampel untuk dikerjakan sebelum memperbarui parameter model internal.

Bayangkan batch sebagai iterasi for-loop pada satu atau lebih sampel dan membuat prediksi. Pada akhir batch, prediksi dibandingkan dengan variabel output yang diharapkan dan error dihitung. Dari error ini, algoritma pembaruan digunakan untuk meningkatkan model, mis. bergerak ke bawah sepanjang gradien error.

Dataset training dapat dibagi menjadi satu atau beberapa batch.

Ketika semua sampel training digunakan untuk membuat satu batch, algoritma learning disebut batch gradient descent. Ketika batch adalah ukuran satu sampel, algoritma learning disebut stochastic gradient descent. Ketika ukuran batch lebih dari satu sampel dan kurang dari ukuran dataset training, algoritma pembelajaran disebut mini-batch gradient descent.

  • Batch Gradient Descent. Batch Size = Size dari Training Set
  • Stochastic Gradient Descent. Batch Size = 1
  • Mini-Batch Gradient Descent. 1 < Batch Size < Size dari Training Set

Dalam kasus mini-batch gradient descent, ukuran batch populer termasuk 32, 64, dan 128 sampel. Kita dapat melihat nilai-nilai ini digunakan dalam model dalam literatur dan tutorial.

Bagaimana jika dataset tidak di pecah secara merata oleh batch size?

Ini bisa dan memang sering terjadi ketika training sebuah model. Ini berarti bahwa batch akhir memiliki sampel lebih sedikit daripada batch lainnya.

Sebagai alternatif, kita dapat menghapus beberapa sampel dari dataset atau mengubah ukuran batch sehingga jumlah sampel dalam dataset terbagi rata dengan ukuran batch.

Untuk lebih lanjut tentang perbedaan antara variasi gradient descent ini, lihat tulisan:

Untuk lebih lanjut tentang pengaruh ukuran batch pada proses learning, lihat tulisan:

   How to Control the Speed and Stability of Training Neural Networks Batch Size

Batch melibatkan pembaruan ke model menggunakan sampel; selanjutnya, mari kita lihat epoch.

What Is an Epoch?

The number of epochs is a hyperparameter that defines the number times that the learning algorithm will work through the entire training dataset.

One epoch means that each sample in the training dataset has had an opportunity to update the internal model parameters. An epoch is comprised of one or more batches. For example, as above, an epoch that has one batch is called the batch gradient descent learning algorithm.

You can think of a for-loop over the number of epochs where each loop proceeds over the training dataset. Within this for-loop is another nested for-loop that iterates over each batch of samples, where one batch has the specified “batch size” number of samples.

The number of epochs is traditionally large, often hundreds or thousands, allowing the learning algorithm to run until the error from the model has been sufficiently minimized. You may see examples of the number of epochs in the literature and in tutorials set to 10, 100, 500, 1000, and larger.

It is common to create line plots that show epochs along the x-axis as time and the error or skill of the model on the y-axis. These plots are sometimes called learning curves. These plots can help to diagnose whether the model has over learned, under learned, or is suitably fit to the training dataset.

For more on diagnostics via learning curves with LSTM networks, see the post:

   A Gentle Introduction to Learning Curves for Diagnosing Model Performance

In case it is still not clear, let’s look at the differences between batches and epochs.

What Is the Difference Between Batch and Epoch?

The batch size is a number of samples processed before the model is updated.

The number of epochs is the number of complete passes through the training dataset.

The size of a batch must be more than or equal to one and less than or equal to the number of samples in the training dataset.

The number of epochs can be set to an integer value between one and infinity. You can run the algorithm for as long as you like and even stop it using other criteria besides a fixed number of epochs, such as a change (or lack of change) in model error over time.

They are both integer values and they are both hyperparameters for the learning algorithm, e.g. parameters for the learning process, not internal model parameters found by the learning process.

You must specify the batch size and number of epochs for a learning algorithm.

There are no magic rules for how to configure these parameters. You must try different values and see what works best for your problem.

Worked Example

Finally, let’s make this concrete with a small example.

Assume you have a dataset with 200 samples (rows of data) and you choose a batch size of 5 and 1,000 epochs.

This means that the dataset will be divided into 40 batches, each with five samples. The model weights will be updated after each batch of five samples.

This also means that one epoch will involve 40 batches or 40 updates to the model.

With 1,000 epochs, the model will be exposed to or pass through the whole dataset 1,000 times. That is a total of 40,000 batches during the entire training process. Further Reading

This section provides more resources on the topic if you are looking to go deeper.

   Gradient Descent For Machine Learning
   How to Control the Speed and Stability of Training Neural Networks Batch Size
   A Gentle Introduction to Mini-Batch Gradient Descent and How to Configure Batch Size
   A Gentle Introduction to Learning Curves for Diagnosing Model Performance
   Stochastic gradient descent on Wikipedia
   Backpropagation on Wikipedia

Summary

In this post, you discovered the difference between batches and epochs in stochastic gradient descent.

Specifically, you learned:

  • Stochastic gradient descent is an iterative learning algorithm that uses a training dataset to update a model.
  • The batch size is a hyperparameter of gradient descent that controls the number of training samples to work through before the model’s internal parameters are updated.
  • The number of epochs is a hyperparameter of gradient descent that controls the number of complete passes through the training dataset.


Referensi

Pranala Menarik