How to make predictions on new dataset with tensorflow's gradient tape

Viewed 58

While I'm able to understand how to use model.fit(x_train, y_train), I can't figure out how to make predictions on new data using tensorflow's gradient tape. My github repository with runnable code (up to an error) can be found here. What is currently working is that I get the trained model "network_output", however it appears that with gradient tape, argmax is being used on the model itself, where I'm used to model.fit() taking the test data as an input:

network_output = trained_network(input_images,input_number)

preds = np.argmax(network_output, axis=1)

Where "input_images" is an ndarray: (20,3,3,1) and "input_number" is an ndarray: (20,5).

Now I'm taking network_output as the trained model and would like to use it to predict similarly typed data of test_images, and test_number respectively.

The error 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'predict' here:

predicted_number = network_output.predict(test_images)

Which is because I don't know how to use the tape to make predictions. However once the prediction works I would guess I can compare the resulting "predicted_number" against the "test_number" as would usually be done using the model.fit method.

acc = 0

    for i in range(len(test_images)):
        if (predicted_number[i] == test_number[i]):
            acc += 1

    print("Accuracy: ", acc / len(input_images) * 100, "%")
1 Answers

In order to obtain prediction I usually iterate through batches manually like this:

predictions = []
for batch in range(num_batch):
    logits = trained_network(x_test[batch * batch_size: (batch + 1) * batch_size], training=False)
    # first obtain probabilities 
    # (if the last layer of the network has no activation, otherwise skip the softmax here)
    prob = tf.nn.softmax(logits)
    # putting back together predictions for all batches
    predictions.extend(tf.argmax(input=prob, axis=1))

If you don't have a lot of data you can skip the loop, this is faster than using predict because you directly invoke the __call__ method of the model:

logits = trained_network(x_test, training=False)
prob = tf.nn.softmax(logits)
predictions = tf.argmax(input=prob, axis=1)

Finally you could also use predict. In this case the batches are handled automatically. It is easier to use when you have lots of data since you don't have to create a loop to interate through batches. The result is a numpy array of predictions. In can be used like this:

predictions = trained_network.predict(x_test)  # you can set a batch_size if you want

What you're doing wrong is this part:

network_output = trained_network(input_images,input_number)
predicted_number = network_output.predict(test_images)

You have to call predict directly on your model trained_network.

Related