import torch


def convert_to_numpy(dictionary):
    """Convert all torch tensors to numpy for JSON serialization."""
    converted = {}

    for key in dictionary.keys():
        item = dictionary[key]

        if isinstance(item, dict):
            converted_item = convert_to_numpy(item)
        elif isinstance(item, torch.Tensor):
            converted_item = item.detach().cpu().numpy()
        else:
            raise ValueError(f"Unknown conversion for key {key} with item {item}")

        converted[key] = converted_item

    return converted