Last active
February 22, 2022 17:36
-
-
Save sergeyprokudin/bb66fff8c672f8caab6bbb1056c7bd20 to your computer and use it in GitHub Desktop.
Simple Keras DNN with probabilistic Gaussian output (mean + variance)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from keras.layers import Input, Dense | |
from keras.models import Model, Sequential | |
def dnn(n_inputs, n_outputs, n_hidden_layers=3, hlayer_size=128, probabilistic=True): | |
"""Defines simple DNN model | |
""" | |
x_input = Input(shape=[n_inputs]) | |
model = Sequential() | |
for i in range(0, n_hidden_layers): | |
model.add(Dense(hlayer_size, activation='relu')) | |
if probabilistic: | |
model.add(Dense(n_outputs * 2, activation='linear')) | |
else: | |
model.add(Dense(n_outputs, activation='linear')) | |
model = Model(x_input, model(x_input)) | |
model.summary() | |
return model | |
def predict_prob(model, x, batch_size=2048): | |
"""Make predictions given model and 2d data | |
""" | |
ypred = model.predict(x, batch_size=batch_size, verbose=1) | |
n_outs = int(ypred.shape[1] / 2) | |
mean = ypred[:, 0:n_outs] | |
sigma = np.exp(ypred[:, n_outs:]) | |
return mean, sigma |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how can i use mean, sigma value to predict new input?