rses-dl-course / rses-dl-course.github.io

Other
8 stars 4 forks source link

deprecated call on predict_classes in R notebook R02 #12

Open EdwinB12 opened 1 year ago

EdwinB12 commented 1 year ago
preds_classes <- model %>%
  predict_classes(x = pred_images)
preds_classes

gives a depreciated call error.

Reported by a course participant and reproduced. Full traceback below:

Warning message in predict_classes(., x = pred_images):
“`predict_classes()` is deprecated and and was removed from tensorflow in version 2.6.
Please update your code:
  * If your model does multi-class classification:
    (e.g. if it uses a `softmax` last-layer activation).

      model %>% predict(x) %>% k_argmax()

  * if your model does binary classification
    (e.g. if it uses a `sigmoid` last-layer activation).

      model %>% predict(x) %>% `>`(0.5) %>% k_cast("int32")
”
Error in py_get_attr_impl(x, name, silent): AttributeError: 'Sequential' object has no attribute 'predict_classes'

Traceback:

1. model %>% predict_classes(x = pred_images)
2. predict_classes(., x = pred_images)
3. do.call(object$predict_classes, args)
4. object$predict_classes
5. `$.python.builtin.object`(object, "predict_classes")
6. py_get_attr_or_item(x, name, TRUE)
7. py_get_attr(x, name)
8. py_get_attr_impl(x, name, silent)
ubdbra001 commented 1 year ago

Just had a look at this, the fix suggested by the error message works. But if you want to access the predicted classes as an array of integers (which the following cells suggest you do), then you'll want to add as.integer() as the final step in the pipe.

Happy to fix it, just want to make sure this is what you want it to do

EdwinB12 commented 1 year ago

Yes fairly sure that is the desired behaviour @ubdbra001.

ubdbra001 commented 1 year ago

Also need to update this text:

This prediction approach can be useful if you want to know the probability distribution for each prediction across all classes. However, if you are only interested in the predicted class, there is a shorthand keras function that can return predicted classes instead, predict_classes.

The code following it will be: preds_classes <- model %>% predict(x = pred_images) %>% k_argmax() %>% as.integer()