# Can't run tensorflow anymore

**URL:** <https://discuss.ai.google.dev/t/cant-run-tensorflow-anymore/31496>\
**Category:** General Discussion\
**Tags:** python, model-training, tfdata\
**Created:** [March 7, 2024, 2:13am UTC](https://discuss.ai.google.dev/t/cant-run-tensorflow-anymore/31496 "2024-03-07T02:13:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wanderson\_Silva](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/wanderson_silva/32/17674_2.png) [@Wanderson\_Silva](https://discuss.ai.google.dev/u/Wanderson_Silva)\
**Post date:** [March 7, 2024, 2:13am UTC](https://discuss.ai.google.dev/t/cant-run-tensorflow-anymore/31496/1 "2024-03-07T02:13:58Z")

</div>

Hello to all. I am trying to train a model for testing purposes but am facing the issue:

python mnist\_distributed.py  
Traceback (most recent call last):  
File “C:\Users\pc\Desktop\New folder\mnist\_distributed.py”, line 3, in   
import tensorflow as tf  
File “C:\Users\pc\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\__init_\_.py”, line 45, in   
from tensorflow.python import tf2 as \_tf2  
File “C:\Users\pc\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\tf2.py”, line 21, in   
from tensorflow.python.platform import \_pywrap\_tf2  
ImportError: DLL load failed while importing \_pywrap\_tf2: A dynamic link library (DLL) initialization routine failed.

Done everything I have found in the internet, including C++ redistribuitable, but no success. Am using Python 3.9.11 and tensorflow 2.15.0. The thing is that it was working fine before.  
This is the script for the training:

```auto
import os
import datetime
import tensorflow as tf
from tensorflow.keras.callbacks import TensorBoard, Callback
# Atualizado para usar a nova API de precisão mista
from tensorflow.keras.mixed_precision import set_global_policy

# Configuração para precisão mista
set_global_policy('mixed_float16') # Atualizado para definir a política global

# Configuração de logging para visualizar o processo de distribuição
tf.get_logger().setLevel('INFO')

# Defina a estratégia de treinamento distribuído
strategy = tf.distribute.MultiWorkerMirroredStrategy()

print("Number of devices: {}".format(strategy.num_replicas_in_sync))

class CustomCallback(Callback):
    def on_epoch_begin(self, epoch, logs=None):
        print(f"Iniciating epoch number {epoch+1}")

    def on_epoch_end(self, epoch, logs=None):
        print(f"End of epoch {epoch+1}")

with strategy.scope():
    # Construa o modelo com precisão mista
    model = tf.keras.models.Sequential([
        tf.keras.layers.Flatten(input_shape=(28, 28)),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dropout(0.2),
        tf.keras.layers.Dense(10, dtype='float32') # Garantir a última camada com float32
    ])

    # Compilação do modelo com ajustes para treinamento distribuído
    # Atualizado para usar a função de perda recomendada
    model.compile(optimizer='adam',
                  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                  metrics=['accuracy'])

# Função para preprocessamento dos dados
def preprocess(image, label):
    image = tf.cast(image, tf.float32) / 255.0
    return image, label

# Carregamento e preparação do dataset MNIST com tf.data
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()

# Aplicação do preprocessamento e batching com prefetching
BATCH_SIZE = 64 * strategy.num_replicas_in_sync # Ajuste o tamanho do lote conforme o número de réplicas
train_dataset = tf.data.Dataset.from_tensor_slices((train_images, train_labels)).map(preprocess).batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)
test_dataset = tf.data.Dataset.from_tensor_slices((test_images, test_labels)).map(preprocess).batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)

# Configuração do TensorBoard para monitoramento
log_dir = os.path.join("logs", "fit", datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
tensorboard_callback = TensorBoard(log_dir=log_dir, histogram_freq=1)

# Treinamento do modelo com o dataset preparado e CustomCallback para monitoramento
model.fit(train_dataset, epochs=10, validation_data=test_dataset, callbacks=[tensorboard_callback, CustomCallback()])

```

Appreciate any help!

---

<div class="post-metadata">

**Author:** ![Wanderson\_Silva](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/wanderson_silva/32/17674_2.png) [@Wanderson\_Silva](https://discuss.ai.google.dev/u/Wanderson_Silva)\
**Post date:** [March 7, 2024, 2:48pm UTC](https://discuss.ai.google.dev/t/cant-run-tensorflow-anymore/31496/3 "2024-03-07T14:48:55Z")

</div>

I was wandering if there is a correct sequence for the installation of the required components. For example: Visual C++ → Python → TensorFlow.

---

<div class="post-metadata">

**Author:** ![Renu\_Patel](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/renu_patel/32/8957_2.png) [@Renu\_Patel](https://discuss.ai.google.dev/u/Renu_Patel)\
**Post date:** [March 12, 2024, 10:10am UTC](https://discuss.ai.google.dev/t/cant-run-tensorflow-anymore/31496/5 "2024-03-12T10:10:51Z")

</div>

Hi @Wanderson_Silva

It seems, The correct compatible version of TensorFlow, Python and CUDA, cuDNN are not installed in your system to support GPU. Please try agian by upgrading the `python` version to 3.10 or downgrading the `TensorFlow` to the one older version.

I am able to run the above code in Google Colab using `Python 3.10` and `TensorFlow 2.15` without any error. (Please find the replicated [gist](https://colab.sandbox.google.com/gist/RenuPatelGoogle/b2687d2035546c66467aac2fb3c21beb/23105.ipynb) here).

You can refer to this [TF install](https://www.tensorflow.org/install/pip) official document to verify if you have followed all the instructed steps to install TensorFlow with GPU support as per your system OS.

Let us know if the issue still persists. Thank you.
