Keras, how do I predict after I trained a model?

前端 未结 6 2110
星月不相逢
星月不相逢 2020-12-02 08:19

I\'m playing with the reuters-example dataset and it runs fine (my model is trained). I read about how to save a model, so I could load it later to use again. But how do I

6条回答
  •  长情又很酷
    2020-12-02 08:49

    You can just "call" your model with an array of the correct shape:

    model(np.array([[6.7, 3.3, 5.7, 2.5]]))
    

    Full example:

    from sklearn.datasets import load_iris
    from tensorflow.keras.layers import Dense
    from tensorflow.keras.models import Sequential
    import numpy as np
    
    X, y = load_iris(return_X_y=True)
    
    model = Sequential([
        Dense(16, activation='relu'),
        Dense(32, activation='relu'),
        Dense(1)])
    
    model.compile(loss='mean_absolute_error', optimizer='adam')
    
    history = model.fit(X, y, epochs=10, verbose=0)
    
    print(model(np.array([[6.7, 3.3, 5.7, 2.5]])))
    
    
    

提交回复
热议问题