pytorch-transformer icon indicating copy to clipboard operation
pytorch-transformer copied to clipboard

Issue with latest_weights_file_path() function

Open Xie-yx opened this issue 9 months ago • 0 comments

Hi @hkproj ,

I found an issue with the latest_weights_file_path() function in config.py.

The original code uses weights_files.sort() to sort the files and selects the last one as the latest file. However, sort() uses lexicographical (dictionary) order, which can lead to incorrect results in some cases.

Here is the original code:

def latest_weights_file_path(config):
    model_folder = f"{config['datasource']}_{config['model_folder']}"
    model_filename = f"{config['model_basename']}*"
    weights_files = list(Path(model_folder).glob(model_filename))
    if len(weights_files) == 0:
        return None
    weights_files.sort()
    return str(weights_files[-1])

A corrected version, suggested by ChatGPT, is as follows:

def latest_weights_file_path(config):
    model_folder = f"{config['datasource']}_{config['model_folder']}"
    model_filename = f"{config['model_basename']}*.pt"  # 确保匹配 `.pt` 文件
    weights_files = list(Path(model_folder).glob(model_filename))
    
    if not weights_files:
        return None

    def extract_number(f):
        match = re.search(r'_(\d+)\.pt$', f.name)
        return int(match.group(1)) if match else -1

    latest_file = max(weights_files, key=extract_number) 
    return str(latest_file)

Xie-yx avatar Mar 07 '25 09:03 Xie-yx