-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
60 lines (50 loc) · 1.91 KB
/
Copy pathmodel.py
File metadata and controls
60 lines (50 loc) · 1.91 KB
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
#############################################################################
# YOUR GENERATIVE MODEL
# ---------------------
# Should be implemented in the 'generative_model' function
# !! *DO NOT MODIFY THE NAME OF THE FUNCTION* !!
#
# You can store your parameters in any format you want (npy, h5, json, yaml, ...)
# <!> *SAVE YOUR PARAMETERS IN THE parameters/ DICRECTORY* <!>
#
# See below an example of a generative model
# Z,x |-> G_\theta(Z,x)
############################################################################
# <!> DO NOT ADD ANY OTHER ARGUMENTS <!>
import numpy as np
from joblib import load
def generative_model(noise, scenario):
"""
Generative model
Parameters
----------
noise : ndarray with shape (n_samples, n_dim=4)
input noise of the generative model
scenario: ndarray with shape (n_samples, n_scenarios=9)
input categorical variable of the conditional generative model
"""
# See below an example
# ---------------------
latent_dims = 4
# choose the appropriate latent dimension of your model
latent_variable = noise[:, :latent_dims]
# Loading the model
scen = np.argmax(scenario[0]) + 1
# OLD
# model = load(f'parameters/gmm_part2/gmm_by_scenario/model_{scen}.joblib')
# BEST
model = load(f'parameters/gmm_part2/tuned_best/model_{scen}.joblib')
# Getting the parameters
weights = model.weights_
means = model.means_
covariances = model.covariances_
# Simulating
simul = np.zeros((4, noise.shape[0]))
for j in range(noise.shape[0]):
component_idx = np.random.choice(np.arange(len(weights)), p=weights)
S = np.linalg.cholesky(covariances[component_idx])
simul[:, j] = S @ latent_variable[j].T + means[component_idx]
simul = np.where(simul < 0, 0, simul)
simul = np.where(simul > 15.75, 15.75, simul)
return simul.T
# return model(latent_variable, scenario) # G(Z, x)