I'm trying to generate a prediction interval for a neural network with Keras. I found this code and want to replicate it: https://medium.com/hal24k-techblog/how-to-generate-neural-network-confidence-intervals-with-keras-e4c0b78ebbdf
This is the model from which I need to extract the prediction interval:
model = Sequential()
model.add(LSTM(1, batch_input_shape=(1, x_train.shape[1], x_train.shape[2]), dropout=0.5, stateful=True))
model.add(Dropout(rate=0.5))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x_train, y_train, epochs=10, batch_size=1, verbose=1)
This is the function I need to apply to the previous model:
def create_dropout_predict_function(model, dropout):
"""
Create a keras function to predict with dropout
model : keras model
dropout : fraction dropout to apply to all layers
Returns
predict_with_dropout : keras function for predicting with dropout
"""
# Load the config of the original model
conf = model.get_config()
# Add the specified dropout to all layers
for layer in conf['layers']:
# Dropout layers
if layer["class_name"]=="Dropout":
layer["config"]["rate"] = dropout
# Recurrent layers with dropout
elif "dropout" in layer["config"].keys():
layer["config"]["dropout"] = dropout
# Create a new model with specified dropout
if type(model)==Sequential:
# Sequential
model_dropout = Sequential.from_config(conf)
else:
# Functional
model_dropout = Model.from_config(conf)
model_dropout.set_weights(model.get_weights())
# Create a function to predict with the dropout on
predict_with_dropout = K.function(model_dropout.inputs+[K.learning_phase()], model_dropout.outputs)
return predict_with_dropout
To then execute this loop for:
dropout = 0.5
num_iter = 20
num_samples = input_data[0].shape[0]
predict_with_dropout = create_dropout_predict_function(model, dropout)
predictions = np.zeros((num_samples, num_iter))
for i in range(num_iter):
predictions[:,i] = predict_with_dropout(input_data+[1])[0].reshape(-1)
However, the following error is occurring:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-40-000a60ea05b8> in <module>()
6 model = load_model(path_to_model)
7
----> 8 predict_with_dropout = create_dropout_predict_function(model, dropout)
9
10 predictions = np.zeros((num_samples, num_iter))
6 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py in _validate_graph_inputs_and_outputs(self)
689 'must come from `tf.keras.Input`. '
690 'Received: ' + str(x) +
--> 691 ' (missing previous layer metadata).')
692 # Check that x is an input tensor.
693 # pylint: disable=protected-access
ValueError: Input tensors to a Functional must come from tf.keras.Input
. Received: 0 (missing previous layer metadata).
It could be that the input data is incorrect, can someone help me?
training data shape: numpy.array (824, 30, 10)
training target data: numpy.array (824, 1)
validation data shape: numpy.array (391, 30, 10)
validation target data: numpy.array (391, 1)
question from:
https://stackoverflow.com/questions/66056319/error-when-editing-a-model-in-keras-valueerror-input-tensors-to-a-functional