Created
June 1, 2018 17:29
-
-
Save grantwwoodford/9ca8184b6a17a244836f816d2753c9ef to your computer and use it in GitHub Desktop.
uncertainty_leak_issue
This file contains hidden or 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
from keras.layers import Dense, Dropout | |
from keras.models import Sequential | |
import numpy as np | |
import keras.backend as K | |
def model(): | |
# create model | |
model = Sequential() | |
model.add(Dense(100, input_dim=36, kernel_initializer='uniform', activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(1, kernel_initializer='normal')) | |
return model | |
m = model() | |
m.compile(loss='mse', optimizer='adam', metrics=['mean_absolute_error']) | |
def predict_with_uncertainty(mdl, x, n_iter=30): | |
f = K.function([mdl.layers[0].input, K.learning_phase()], | |
[mdl.layers[-1].output]) | |
result = np.zeros((n_iter, x.shape[0])) | |
for iter in range(n_iter): | |
result[iter] = np.array(f([x, 1])[0]).reshape(-1) | |
return result | |
while True: | |
predict_with_uncertainty(m, np.zeros((1000, 36))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment