Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

python - Error when editing a model in Keras - ValueError: Input tensors to a Functional must come from `tf.keras.Input`

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...