def parameters_to_tensor_groups(parameter_groups, attribute):
    """ Function to reduce a parameter group from an optimizer to tensor_group with
    the same structure and just the requested parameter
    :param parameter_groups: The parameter group to extract from
    :param attribute: The attribute from the parameter to extrect, e.g. data or grad
    :return: a tensor_group of the same shape ocnting the requested attribute
    """

    tensor_group_return = []

    for group in parameter_groups:
        group_attrs = []
        for ps in group["params"]:
            group_attrs.append(ps.__getattribute__(attribute))
            tensor_group_return.append(group_attrs)

    return tensor_group_return