reloading
v1.1.2
상태를 잃지 않고 각 반복의 소스에서 루프 바디를 다시로드하는 Python 유틸리티
딥 러닝 모델 교육 중에 소스 코드를 편집하는 데 유용합니다. 이를 통해 교육을 다시 시작하지 않고 로깅을 추가하거나 통계를 인쇄하거나 모델을 저장할 수 있습니다.

pip install reloading
각 반복 전에 소스에서 for 의 본문을 다시로드하려면 반복자를 reloading 로드하여 마무리하십시오.
from reloading import reloading
for i in reloading ( range ( 10 )):
# this code will be reloaded before each iteration
print ( i ) 각 실행 전에 소스에서 함수를 다시로드하려면 @reloading 으로 함수 정의를 장식하십시오.
from reloading import reloading
@ reloading
def some_function ():
# this code will be reloaded before each invocation
pass 모든 N 번째 호출 또는 반복마다 다시로드 할 때 every 키워드 인수를 전달하십시오. 예를 들어
for i in reloading ( range ( 1000 ), every = 10 ):
# this code will only be reloaded before every 10th iteration
# this can help to speed-up tight loops
pass
@ reloading ( every = 10 )
def some_function ():
# this code with only be reloaded before every 10th invocation
pass Pass forever=True , 예를 들어
for i in reloading ( forever = True ):
# this code will loop forever and reload from source before each iteration
pass 다음은 좋아하는 라이브러리와 함께 재 장전을 사용하는 방법의 짧은 스 니펫입니다. 완전한 예를 보려면 예제 폴더를 확인하십시오.
for epoch in reloading ( range ( NB_EPOCHS )):
# the code inside this outer loop will be reloaded before each epoch
for images , targets in dataloader :
optimiser . zero_grad ()
predictions = model ( images )
loss = F . cross_entropy ( predictions , targets )
loss . backward ()
optimiser . step ()다음은 전체 Pytorch 예입니다.
@ reloading
def update_learner ( learner ):
# this function will be reloaded from source before each epoch so that you
# can make changes to the learner while the training is running
pass
class LearnerUpdater ( LearnerCallback ):
def on_epoch_begin ( self , ** kwargs ):
update_learner ( self . learn )
path = untar_data ( URLs . MNIST_SAMPLE )
data = ImageDataBunch . from_folder ( path )
learn = cnn_learner ( data , models . resnet18 , metrics = accuracy ,
callback_fns = [ LearnerUpdater ])
learn . fit ( 10 )다음은 전체 Fastai 예입니다.
@ reloading
def update_model ( model ):
# this function will be reloaded from source before each epoch so that you
# can make changes to the model while the training is running using
# K.set_value()
pass
class ModelUpdater ( Callback ):
def on_epoch_begin ( self , epoch , logs = None ):
update_model ( self . model )
model = Sequential ()
model . add ( Dense ( 64 , activation = 'relu' , input_dim = 20 ))
model . add ( Dense ( 10 , activation = 'softmax' ))
sgd = SGD ( lr = 0.01 , decay = 1e-6 , momentum = 0.9 , nesterov = True )
model . compile ( loss = 'categorical_crossentropy' ,
optimizer = sgd ,
metrics = [ 'accuracy' ])
model . fit ( x_train , y_train ,
epochs = 200 ,
batch_size = 128 ,
callbacks = [ ModelUpdater ()])다음은 전체 keras 예입니다.
for epoch in reloading ( range ( NB_EPOCHS )):
# the code inside this outer loop will be reloaded from source
# before each epoch so that you can change it during training
train_loss . reset_states ()
train_accuracy . reset_states ()
test_loss . reset_states ()
test_accuracy . reset_states ()
for images , labels in tqdm ( train_ds ):
train_step ( images , labels )
for test_images , test_labels in tqdm ( test_ds ):
test_step ( test_images , test_labels )다음은 전체 텐서 플로 예입니다.
경로에서 python 과 python3 사용할 수 있는지 확인한 다음 실행하십시오.
$ python3 reloading/test_reloading.py