简体   繁体   English

Python:如何选择最新创建的文件(将Keras检查点上传到GCS)

[英]Python: How to select the latest created file (Uploading Keras Checkpoints to GCS)

I'm using Keras and CheckPoint() callback to create a list of checkpoints in the following format: 我正在使用Keras和CheckPoint()回调以以下格式创建检查点列表:

- app
--script.py
--checkpoint.01-{model-loss}.h5
--checkpoint.02-{model-loss}.h5
--checkpoint.03-{model-loss}.h5
--checkpoint.02-{model-loss}.h5
...
--checkpoint.10-{model-loss}.h5
..
--checkpoint.99-{model-loss}.h5
--checkpoint.100-{model-loss}.h5
..

I define the checkpoints as: 我将检查点定义为:

model_path_glob = 'checkpoint.*'
checkpoints = glob.glob(model_path_glob) # gives me a list but in weird order

The above is executed in a loop, so checkpoints is continuously updated. 上面是循环执行的,因此checkpoints会不断更新。 I can see that the len(checkpoints) changes correctly with every iteration, so they are all there, but in a weird order. 我可以看到len(checkpoints)每次迭代都正确更改,因此它们都在那儿,但是顺序很奇怪。

However, it seems that I'm uploading the same file to the server using checkpoint[-1] , for example the first 15 iterations will upload checkpoint 1,2,3,.. but then it will always select the 16th iteration. 但是,似乎我正在使用checkpoint[-1]将相同的文件上传到服务器,例如前15个迭代将上载检查点1,2,3,..,但随后它将始终选择第16个迭代。

On my local machine, it selects 99th iteration and doesn't go past it. 在我的本地计算机上,它选择第99次迭代,并且不会越过它。

checkpoints = sorted(glob.glob(model_path_glob), key=os.path.getmtime) will sort by last time modification. checkpoints = sorted(glob.glob(model_path_glob), key=os.path.getmtime)将按上次修改时间进行排序。 I'm yet to try on cloud VM 我尚未尝试使用云虚拟机

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM