scikit-mdn
Mixture density networks, from PyTorch, for scikit-learn
Usage
To get this tool working locally you will first need to install it:
Then you can use it in your code. Here is a small demo example.
import numpy as np
from sklearn.datasets import make_moons
from skmdn import MixtureDensityEstimator
# Generate dataset
n_samples = 1000
X_full, _ = make_moons(n_samples=n_samples, noise=0.1)
X = X_full[:, 0].reshape(-1, 1) # Use only the first column as input
Y = X_full[:, 1].reshape(-1, 1) # Predict the second column
# Add some noise to Y to make the problem more suitable for MDN
Y += 0.1 * np.random.randn(n_samples, 1)
# Fit the model
mdn = MixtureDensityEstimator()
mdn.fit(X, Y)
# Predict some quantiles on the train set
means, quantiles = mdn.predict(X, quantiles=[0.01, 0.1, 0.9, 0.99], resolution=100000)
plt.scatter(X, Y)
plt.scatter(X, quantiles[:, 0], color='orange')
plt.scatter(X, quantiles[:, 1], color='green')
plt.scatter(X, quantiles[:, 2], color='green')
plt.scatter(X, quantiles[:, 3], color='orange')
plt.scatter(X, means, color='red')
This is what the chart looks like:
You can see how it is able to predict the quantiles of the distribution, and the mean.
API
This is the main object that you'll interact with.
Bases: BaseEstimator
A scikit-learn compatible Mixture Density Estimator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hidden_dim |
hidden layer dimension |
10
|
|
n_gaussians |
number of gaussians in the mixture model |
5
|
|
epochs |
number of epochs |
1000
|
|
lr |
learning rate |
0.01
|
|
weight_decay |
weight decay for regularisation |
0.0
|
Source code in skmdn/__init__.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
|
cdf(X, resolution=100)
Compute the cumulative probability density function of the model.
This function computes the cdf for each sample in Xd. It also returns the y values for which the cdf is computed to help with plotting.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
(n_samples, n_features) |
required | |
resolution |
number of intervals to compute the quantile over |
100
|
Returns:
Name | Type | Description |
---|---|---|
cdf |
(n_samples, resolution) |
|
ys |
(resolution,) |
Source code in skmdn/__init__.py
fit(X, y)
Fit the model to the data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
(n_samples, n_features) |
required | |
y |
(n_samples, 1) |
required |
Source code in skmdn/__init__.py
forward(X)
Calculate the \(\pi\), \(\mu\) and \(\sigma\) outputs n for each sample in X.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
(n_samples, n_features) |
required |
Returns:
Name | Type | Description |
---|---|---|
pi |
(n_samples, n_gaussians) |
|
mu |
(n_samples, n_gaussians) |
|
sigma |
(n_samples, n_gaussians) |
Source code in skmdn/__init__.py
partial_fit(X, y, n_epochs=1)
Fit the model to the data for a set number of epochs. Can be used to continue training on new data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
(n_samples, n_features) |
required | |
y |
(n_samples, 1) |
required | |
n_epochs |
number of epochs |
1
|
Source code in skmdn/__init__.py
pdf(X, resolution=100, y_min=None, y_max=None)
Compute the probability density function of the model.
This function computes the pdf for each sample in X. It also returns the y values for which the pdf is computed to help with plotting.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
(n_samples, n_features) |
required | |
resolution |
number of intervals to compute the quantile over |
100
|
Returns:
Name | Type | Description |
---|---|---|
pdf |
(n_samples, resolution) |
|
ys |
(resolution,) |
Source code in skmdn/__init__.py
predict(X, quantiles=None, resolution=100)
Predicts the variance at risk at a given quantile for each datapoint X.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
(n_samples, n_features) |
required | |
quantile |
quantile value |
required | |
resolution |
number of intervals to compute the quantile over |
100
|
Returns:
Name | Type | Description |
---|---|---|
pred |
(n_samples,) |
|
quantiles |
(n_samples, n_quantiles) |