# ImportError: cannot import name '\_initialize\_variables' from 'keras.src.backend'

**URL:** <https://discuss.ai.google.dev/t/importerror-cannot-import-name-initialize-variables-from-keras-src-backend/30669>\
**Category:** Keras\
**Tags:** ubuntu, tfkeras, cuda\
**Created:** [February 19, 2024, 12:27pm UTC](https://discuss.ai.google.dev/t/importerror-cannot-import-name-initialize-variables-from-keras-src-backend/30669 "2024-02-19T12:27:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![delad](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/delad/32/27189_2.png) [@delad](https://discuss.ai.google.dev/u/delad)\
**Post date:** [February 19, 2024, 12:27pm UTC](https://discuss.ai.google.dev/t/importerror-cannot-import-name-initialize-variables-from-keras-src-backend/30669/1 "2024-02-19T12:27:32Z")

</div>

I had to install `tf-nightly[and-cuda]` which solved one of my issues (cuda) on my system running WSL2 and ubuntu 22.04. Now, running a jupyter notebook and simply trying to import the MNIST dataset as follows:

`from tensorflow.keras.datasets import mnist`

I get this error:

`ImportError: cannot import name '_initialize_variables' from 'keras.src.backend'`

I tried several other hints I found searching elsewhere, which did not help:

```auto
import os
os.environ["KERAS_BACKEND"] = "tensorflow"
from keras import initializers

```

Has anyone else had a similar issue and how did you resolve it? This is what I currently have installed:

`tf_keras-nightly 2.16.0.dev2024021410`

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![delad](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/delad/32/27189_2.png) [@delad](https://discuss.ai.google.dev/u/delad)\
**Post date:** [February 19, 2024, 5:59pm UTC](https://discuss.ai.google.dev/t/importerror-cannot-import-name-initialize-variables-from-keras-src-backend/30669/3 "2024-02-19T17:59:52Z")

</div>

And so it appears that the issue exists anytime you use `tensorflow` in the import statement. If instead you use `import keras as keras` or from keras.datasets import mnist then it works for most statements until I arrive at my original cuda problem (see below…):

For example, instead of:

```auto
y_train = tensorflow.keras.utils.to_categorical(y_train, num_categories)
y_valid = tensorflow.keras.utils.to_categorical(y_valid, num_categories)
...
from tensorflow.keras.models import Sequential
...
from tensorflow.keras.layers import Dense
...

```

use:

```auto
y_train = keras.utils.to_categorical(y_train, num_categories)
y_valid = keras.utils.to_categorical(y_valid, num_categories)
...
from keras.models import Sequential
...
from keras.layers import Dense
...

```

and so on. That is, until you get to:

```auto
history = model.fit(
    x_train, y_train, epochs=5, verbose=1, validation_data=(x_valid, y_valid)
)

```

which produces:

```auto
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1708348871.971183 150951 service.cc:145] XLA service 0x7f1ad4002f00 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
I0000 00:00:1708348871.971259 150951 service.cc:153] StreamExecutor device (0): NVIDIA GeForce GTX 960M, Compute Capability 5.0
2024-02-19 08:21:12.004627: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:268] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
2024-02-19 08:21:13.329554: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:458] Loaded runtime CuDNN library: 8.2.4 but source was compiled with: 8.9.6. CuDNN library needs to have matching major version and equal or higher minor version. If using a binary install, upgrade your CuDNN library. If building from sources, make sure the library loaded at runtime is compatible with the version specified during compile configuration.
2024-02-19 08:21:13.348512: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:458] Loaded runtime CuDNN libra
ry: 8.2.4 but source was compiled with: 8.9.6. CuDNN library needs to have matching major version and equal or higher minor version. If using a binary install, upgrade your CuDNN library. If building from sources, make sure the library loaded at runtime is compatible with the version specified during compile configuration.
2024-02-19 08:21:13.357030: W tensorflow/core/framework/op_kernel.cc:1839] OP_REQUIRES failed at xla_ops.cc:580 : FAILED_PRECONDITION: DNN library initialization failed. Look at the errors above for more details.
2024-02-19 08:21:13.357160: W tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is aborting with status: FAILED_PRECONDITION: DNN library initialization failed. Look at the errors above for more details.
	 [[{{node StatefulPartitionedCall}}]]
...

```

Looks like I may need to start from scratch and try different versions of Tensorflow. This is so frustrating!

---

<div class="post-metadata">

**Author:** ![Kiran\_Sai\_Ramineni](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/kiran_sai_ramineni/32/8859_2.png) [@Kiran\_Sai\_Ramineni](https://discuss.ai.google.dev/u/Kiran_Sai_Ramineni)\
**Post date:** [February 20, 2024, 6:36am UTC](https://discuss.ai.google.dev/t/importerror-cannot-import-name-initialize-variables-from-keras-src-backend/30669/5 "2024-02-20T06:36:06Z")

</div>

Hi @delad, Could you please share the `!nvidia-smi` details. Thank You.

---

<div class="post-metadata">

**Author:** ![delad](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/delad/32/27189_2.png) [@delad](https://discuss.ai.google.dev/u/delad)\
**Post date:** [February 22, 2024, 2:29pm UTC](https://discuss.ai.google.dev/t/importerror-cannot-import-name-initialize-variables-from-keras-src-backend/30669/7 "2024-02-22T14:29:15Z")

</div>

Thank you for your help. Sorry this took so long to get back to you. turns out somehow the nvidia-smi app got removed. Anyway, here’s the output:

```auto
Thu Feb 22 01:46:43 2024
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 550.40.07 Driver Version: 551.52 CUDA Version: 12.4 |
|-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA GeForce GTX 960M On | 00000000:01:00.0 Off | N/A |
| N/A 0C P8 N/A / 200W | 3270MiB / 4096MiB | 2% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| 0 N/A N/A 667 G /Xwayland N/A |
| 0 N/A N/A 129420 C /python3.10 N/A |
+-----------------------------------------------------------------------------------------+

```

It also appears the nvidia-smi (/usr/lib/wsl/lib/nvidia-smi) is not in my jupyter local path. Looking into this …

---

<div class="post-metadata">

**Author:** ![delad](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/delad/32/27189_2.png) [@delad](https://discuss.ai.google.dev/u/delad)\
**Post date:** [February 25, 2024, 11:32pm UTC](https://discuss.ai.google.dev/t/importerror-cannot-import-name-initialize-variables-from-keras-src-backend/30669/9 "2024-02-25T23:32:57Z")

</div>

I backed out my changes of installing tf-nightly and reinstalled the latest official tensorflow. However, I then upgraded the nvidia-\* packages to avoid the issue that “forced” me to try the tf-nightly. So, this issue no longer exists for me. I now face a different issue that, although is a nuisance and still allows the code to complete, I’d like to understand how to resolve it. I’ll open a different case for that. Thank you.
