Skip to content

pytorch mini batch size

2019.12.19 06:12

WHRIA 조회 수:82

https://stackoverflow.com/questions/52518324/how-to-compensate-if-i-cant-do-a-large-batch-size-in-neural-network/52523847

 

 

4

In pytorch, when you perform the backward step (calling loss.backward() or similar) the gradients are accumulated in-place. This means that if you call loss.backward() multiple times, the previously calculated gradients are not replaced, but in stead the new gradients get added on to the previous ones. That is why, when using pytorch, it is usually necessary to explicitly zero the gradients between minibatches (by calling optimiser.zero_grad() or similar).

If your batch size is limited, you can simulate a larger batch size by breaking a large batch up into smaller pieces, and only calling optimiser.step() to update the model parameters after all the pieces have been processed.

For example, suppose you are only able to do batches of size 64, but you wish to simulate a batch size of 128. If the original training loop looks like:

optimiser.zero_grad()
loss = model(batch_data) # batch_data is a batch of size 128
loss.backward()
optimiser.step()

then you could change this to:

optimiser.zero_grad()

smaller_batches = batch_data[:64], batch_data[64:128]
for batch in smaller_batches:
    loss = model(batch) / 2
    loss.backward()

optimiser.step()

and the updates to the model parameters would be the same in each case (apart maybe from some small numerical error). Note that you have to rescale the loss to make the update the same.

번호 제목 글쓴이 날짜 조회 수
1641 Visual Studio 에서 SVN 업로드를 위해 불필요한 파일 삭제 file WHRIA 2013.01.13 8721
1640 slide 메뉴 WHRIA 2009.01.19 8717
1639 쉬운 투자 시대의 종말 WHRIA 2009.09.14 8694
1638 개업 WHRIA 2009.10.19 8660
1637 승석오빠 Fighting!!! 이슬이 2000.06.05 8656
1636 내가 요즘 집중하는 일 WHRIA 2009.01.20 8611
1635 서버 이전 WHRIA 2012.10.03 8606
1634 ZeroBoard XE WHRIA 2007.08.20 8602
1633 아산 사회 복지 재단 WHRIA 2009.02.25 8593
1632 MedicalPhoto Release 1.0.6 WHRIA 2008.06.19 8569
1631 Choose substance before appearance WHRIA 2007.08.28 8569
1630 주소 WHRIA 2011.04.20 8533
1629 MedicalPhoto Release 1.0.7 WHRIA 2008.09.21 8529
1628 오빠 낭~~*^^* KINOKO[[승욱이별명.. 2000.06.06 8513
1627 skin tone file WHRIA 2008.10.18 8507

Powered by Xpress Engine / Designed by Sketchbook

sketchbook5, 스케치북5

sketchbook5, 스케치북5

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소