Coverage for tvo/utils/param_init.py: 0%
17 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-01 11:33 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-01 11:33 +0000
1# -*- coding: utf-8 -*-
2# Copyright (C) 2019 Machine Learning Group of the University of Oldenburg.
3# Licensed under the Academic Free License version 3.0
5import tvo
6import numpy as np
7import torch as to
8from torch import Tensor
11def init_W_data_mean(
12 data: Tensor,
13 H: int,
14 std_factor: float = 0.25,
15 dtype: to.dtype = to.float64,
16 device: to.device = None,
17) -> Tensor:
18 """Initialize weights W based on noisy mean of the data points.
20 :param data: Data set, is (N, D).
21 :param H: Number of basis functions to be generated.
22 :param std_factor: Scalar to control amount of standard deviation of additive noise
23 :param dtype: dtype of output Tensor. Defaults to torch.float64.
24 :param device: torch.device of output Tensor. Defaults to tvo.get_device().
25 :returns: Weight matrix W with shape (D,H).
26 """
27 device_ = tvo.get_device() if device is None else device
28 data_nanmean = to.from_numpy(np.nanmean(data.detach().cpu().numpy(), axis=0)).to(
29 dtype=dtype, device=device_
30 )
31 var = init_sigma2_default(data, dtype, device_)
32 return data_nanmean.repeat((H, 1)).t() + std_factor * to.sqrt(var) * to.randn(
33 (len(data_nanmean), H), dtype=dtype, device=device_
34 )
37def init_sigma2_default(
38 data: Tensor, dtype: to.dtype = to.float64, device: to.device = None
39) -> Tensor:
40 """Initialize scalar sigma parameter based on variance of the data points.
42 :param data: Data set, is (N, D).
43 :param dtype: dtype of output Tensor. Defaults to torch.float64.
44 :param device: torch.device of output Tensor. Defaults to tvo.get_device().
45 :returns: Scalar sigma parameter.
47 Returns the mean of the variance in each dimension d=1,...,D.
48 """
49 _device = tvo.get_device() if device is None else device
50 var = to.from_numpy(np.nanvar(data.detach().cpu().numpy(), axis=0)).to(
51 device=_device, dtype=dtype
52 )
53 return to.mean(var) + to.tensor([0.001], device=_device, dtype=dtype)
56def init_pies_default(
57 H: int, crowdedness: float = 2.0, dtype: to.dtype = to.float64, device: to.device = None
58):
59 """Initialize pi parameter based on given crowdedness.
61 :param H: Length of pi vector.
62 :param crowdedness: Average crowdedness corresponding to sum of elements in vector pi.
63 :param dtype: dtype of output Tensor. Defaults to torch.float64.
64 :param device: torch.device of output Tensor. Defaults to tvo.get_device().
65 :returns: Vector pi.
66 """
68 if device is None:
69 device = tvo.get_device()
70 return to.full((H,), fill_value=crowdedness / H, dtype=dtype, device=device)