Skip to content

Instantly share code, notes, and snippets.

@grantwwoodford
Created June 1, 2018 17:29
Show Gist options
  • Save grantwwoodford/9ca8184b6a17a244836f816d2753c9ef to your computer and use it in GitHub Desktop.
Save grantwwoodford/9ca8184b6a17a244836f816d2753c9ef to your computer and use it in GitHub Desktop.
uncertainty_leak_issue
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