ยูทิลิตี้ Python เพื่อโหลดร่างกายวนซ้ำจากแหล่งที่มาในการวนซ้ำแต่ละครั้งโดยไม่สูญเสียสถานะ
มีประโยชน์สำหรับการแก้ไขซอร์สโค้ดระหว่างการฝึกอบรมแบบจำลองการเรียนรู้อย่างลึกซึ้ง สิ่งนี้จะช่วยให้คุณเพิ่มการบันทึกสถิติการพิมพ์หรือบันทึกแบบจำลองโดยไม่ต้องเริ่มการฝึกอบรมและดังนั้นโดยไม่สูญเสียความคืบหน้าการฝึกอบรม

pip install reloading
ในการโหลดร่างกายของ A for loop จากแหล่งที่มาก่อนการวนซ้ำแต่ละครั้งเพียงแค่ห่อตัววนซ้ำด้วย 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 หรือการทำซ้ำทุกครั้งเท่านั้น เช่น
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 )นี่คือตัวอย่าง Tensorflow เต็มรูปแบบ
ตรวจสอบให้แน่ใจว่าคุณมี python และ python3 ในเส้นทางของคุณจากนั้นเรียกใช้:
$ python3 reloading/test_reloading.py