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 将关键字参数传递every每个n-th-th-thin in-tectocation或迭代中。例如
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 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 )这是一个完整的TensorFlow示例。
确保您的路径中有python和python3 ,然后运行:
$ python3 reloading/test_reloading.py