param_dict¶
- snippets.param_dict.from_param_dict(params: ParamDict, shapes: Dict[str, Tuple[int]]) TensorLike¶
Convert a dictionary of parameters to a tensor of parameters for batch processing.
- Parameters:
params – Dictionary mapping parameter names to tensors.
shapes – Dictionary mapping parameter names to shapes.
- Returns:
Tensor of concatenated, raveled parameters ordered by parameter name.
Example
>>> import torch >>> from snippets.param_dict import from_param_dict >>> params = {"b": torch.arange(12).reshape((3, 4)), "a": torch.arange(3)} >>> shapes = {"b": (3, 4), "a": (3,)} >>> from_param_dict(params, shapes) tensor([ 0, 1, 2, 0, ..., 11])
- snippets.param_dict.to_param_dict(params: TensorLike, shapes: Dict[str, Tuple[int]]) ParamDict¶
Convert a tensor of parameters to a dictionary of parameters.
- Parameters:
params – Tensor of concatenated, raveled parameters ordered by parameter name.
shapes – Dictionary mapping parameter names to shapes.
- Returns:
Dictionary mapping parameter names to tensors with the specified shape.
Example
>>> import torch >>> from snippets.param_dict import to_param_dict >>> params = torch.arange(15) >>> shapes = {"b": (3, 4), "a": (3,)} >>> to_param_dict(params, shapes) {'a': tensor([0, 1, 2]), 'b': tensor([[ 3, 4, 5, 6], ...])}