This error occurs because the 'zeroshot_weights' list is empty when trying to stack the tensors using 'torch.stack' function.

To fix this error, you need to make sure that the 'zeroshot_weights' list is not empty before calling 'torch.stack'. You can add a check to verify if the list is empty and handle it accordingly.

Here's an example of how you can modify the code to handle the empty list case:

def zeroshot_classifier(idx_to_label, idx_to_text, templates):
    zeroshot_weights = []
    for label_idx in range(len(idx_to_label)):
        label = idx_to_label[label_idx]
        text = idx_to_text[label_idx]
        if text in templates:
            zeroshot_weights.append(templates[text])
        else:
            # Handle the case when the template is not available
            # You can either skip this label or assign a default value
            # For example, you can assign a tensor of zeros
            zeroshot_weights.append(torch.zeros_like(templates['default_template']))
    
    # Check if the zeroshot_weights list is empty
    if not zeroshot_weights:
        raise ValueError("No zeroshot weights found")
    
    # Stack the tensors using torch.stack
    zeroshot_weights = torch.stack(zeroshot_weights, dim=1).cuda()
    
    return zeroshot_weights

Make sure to adjust the code according to your specific requirements and available templates.

解决 'RuntimeError: stack expects a non-empty TensorList' 错误

原文地址: https://www.cveoy.top/t/topic/iqfg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录