adriangb / scikeras

Scikit-Learn API wrapper for Keras.
https://www.adriangb.com/scikeras/
MIT License
242 stars 50 forks source link

scikeras (tensorflow_cpu) using only one CPU core #293

Closed maxrpunkt closed 7 months ago

maxrpunkt commented 1 year ago

Hi, apparently scikeras (tensorflow_cpu) uses only one CPU core. A working example is your example code for the MLPRegressor (https://www.adriangb.com/scikeras/stable/notebooks/MLPClassifier_MLPRegressor.html). It would considerably improve the training performance when using all CPU cores - or am I overlooking something? Cheers Max

adriangb commented 1 year ago

SciKeras isn't doing anything special with CPU cores or GPUs. That depends on how you configure TensorFlow. We just call TensorFlow for the actual training.

maxrpunkt commented 1 year ago

Many thanks for the answer. I just did "pip install scikeras tensorflow-cpu" - no other special configuration of tensorflow-cpu.

adriangb commented 1 year ago

If you run the model out of SciKeras what do you see?

maxrpunkt commented 1 year ago

2023-03-13 18:36:31.033240: I tensorflow/core/util/port.cc:110] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0. 2023-03-13 18:36:31.050409: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. ... 2023-03-13 18:36:38.694386: I tensorflow/core/common_runtime/executor.cc:1197] [/device:CPU:0] (DEBUG INFO) Executor start aborting (this does not indicate an error and you can ignore this message): INVALID_ARGUMENT: You must feed a value for placeholder tensor 'Placeholder/_11' with dtype float and shape [704198] [[{{node Placeholder/_11}}]] 2023-03-13 18:36:38.694517: I tensorflow/core/common_runtime/executor.cc:1197] [/device:CPU:0] (DEBUG INFO) Executor start aborting (this does not indicate an error and you can ignore this message): INVALID_ARGUMENT: You must feed a value for placeholder tensor 'Placeholder/_11' with dtype float and shape [704198] [[{{node Placeholder/_11}}]]

After this, the training starts. In the meantime I found the tensorflow version 2.11.0 of Intel (https://www.intel.com/content/www/us/en/developer/articles/guide/optimization-for-tensorflow-installation-guide.html). I installed this in my Python 3.10 environment and voila it uses all CPU cores. However, interestingly the standard version of tensorflow-cpu installed in my Python 3.11 environment which makes use of only one CPU core is much faster producing the same results :)

smith558 commented 1 year ago

@maxrpunkt This may be expected behaviour, not related to SciKeras. A lot of TensorFlow CPU routines use a single core only.

adriangb commented 7 months ago

I don't think this is related to SciKeras. And with Keras 3 support the whole story for TensorFlow compatibility and what to do if you wante CPU only has changed (maybe use JAX? I have not tried other backends).