-
Notifications
You must be signed in to change notification settings - Fork 0
/
ratio_estimation.py
74 lines (56 loc) · 2.32 KB
/
ratio_estimation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import hypothesis as h
import torch
import numpy as np
from hypothesis.nn import build_ratio_estimator
from hypothesis.nn.ratio_estimation import BaseRatioEstimator
from hypothesis.util.data import NamedDataset
from hypothesis.util.data import NumpyDataset
from torch.utils.data import TensorDataset
class RatioEstimator(BaseRatioEstimator):
def __init__(self, denominator):
random_variables = {
"configs": (1,),
"inputs": (1,),
"outputs": (1,)}
Class = build_ratio_estimator("mlp", random_variables, denominator=denominator)
activation = torch.nn.SELU
trunk = [128] * 3
r = Class(activation=activation, trunk=trunk)
super(RatioEstimator, self).__init__(r=r)
self._r = r
def log_ratio(self, configs, inputs, outputs, **kwargs):
configs = configs - 45
configs = configs / 5
return self._r.log_ratio(configs=configs, inputs=inputs, outputs=outputs)
class RatioEstimatorTypeI(RatioEstimator):
def __init__(self):
denominator = "inputs|outputs,configs"
super(RatioEstimatorTypeI, self).__init__(denominator)
class RatioEstimatorTypeII(RatioEstimator):
def __init__(self):
denominator = "inputs,outputs|configs"
super(RatioEstimatorTypeII, self).__init__(denominator)
class DatasetTrain(NamedDataset):
def __init__(self):
inputs = np.load("data/train/inputs.npy")
configs = np.load("data/train/configs.npy")
outputs = np.load("data/train/outputs.npy")
inputs = TensorDataset(torch.from_numpy(inputs))
configs = TensorDataset(torch.from_numpy(configs))
outputs = TensorDataset(torch.from_numpy(outputs))
super(DatasetTrain, self).__init__(
configs=configs,
inputs=inputs,
outputs=outputs)
class DatasetTest(NamedDataset):
def __init__(self):
inputs = np.load("data/test/inputs.npy")
configs = np.load("data/test/configs.npy")
outputs = np.load("data/test/outputs.npy")
inputs = TensorDataset(torch.from_numpy(inputs))
configs = TensorDataset(torch.from_numpy(configs))
outputs = TensorDataset(torch.from_numpy(outputs))
super(DatasetTest, self).__init__(
configs=configs,
inputs=inputs,
outputs=outputs)