Making predictions with a TensorFlow model

前端 未结 4 1166
天命终不由人
天命终不由人 2020-12-01 00:53

I followed the given mnist tutorials and was able to train a model and evaluate its accuracy. However, the tutorials don\'t show how to make predictions given a model. I\'m

4条回答
  •  感情败类
    2020-12-01 01:12

    2.0 Compatible Answer: Suppose you have built a Keras Model as shown below:

    model = keras.Sequential([
        keras.layers.Flatten(input_shape=(28, 28)),
        keras.layers.Dense(128, activation='relu'),
        keras.layers.Dense(10, activation='softmax')
    ])
    
    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
    

    Then Train and Evaluate the Model using the below code:

    model.fit(train_images, train_labels, epochs=10)
    test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
    

    After that, if you want to predict the class of a particular image, you can do it using the below code:

    predictions_single = model.predict(img)
    

    If you want to predict the classes of a set of Images, you can use the below code:

    predictions = model.predict(new_images)
    

    where new_images is an Array of Images.

    For more information, refer this Tensorflow Tutorial.

提交回复
热议问题