Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python kernel dies on Jupyter Notebook with tensorflow 2

I installed tensorflow 2 on my mac using conda according these instructions:

conda create -n tf2 tensorflow

Then I installed ipykernel to add this new environment to my jupyter notebook kernels as follows:

conda activate tf2
conda install ipykernel
python -m ipykernel install --user --name=tf2

That seemed to work well, I am able to see my tf2 environment on my jupyter notebook kernels.

Then I tried to run the simple MNIST example to check if all was working properly and I when I execute this line of code:

model.fit(x_train, y_train, epochs=5)

The kernel of my jupyter notebook dies without more information.

dead kernel

I executed the same code on my terminal via python mnist_test.py and also via ipython (command by command) and I don't have any issues, which let's me assume that my tensorflow 2 is correctly installed on my conda environment.

Any ideas on what went wrong during the install?

Versions:

python==3.7.5
tensorboard==2.0.0
tensorflow==2.0.0
tensorflow-estimator==2.0.0
ipykernel==5.1.3
ipython==7.10.2
jupyter==1.0.0
jupyter-client==5.3.4
jupyter-console==5.2.0
jupyter-core==4.6.1

Here I put the complete script as well as the STDOUT of the execution:

import tensorflow as tf
import matplotlib.pyplot as plt
import seaborn as sns

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train, x_test = x_train / 255.0, x_test / 255.0

nn_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, activation='softmax')
])

nn_model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

nn_model.fit(x_train, y_train, epochs=5)

nn_model.evaluate(x_test,  y_test, verbose=2)

(tf2) ➜ tensorflow2 python mnist_test.py 2020-01-03 10:46:10.854619: I tensorflow/core/platform/cpu_feature_guard.cc:145] This TensorFlow binary is optimized with Intel(R) MKL-DNN to use the following CPU instructions in performance critical operations: SSE4.1 SSE4.2 AVX AVX2 FMA To enable them in non-MKL-DNN operations, rebuild TensorFlow with the appropriate compiler flags. 2020-01-03 10:46:10.854860: I tensorflow/core/common_runtime/process_util.cc:115] Creating new thread pool with default inter op setting: 8. Tune using inter_op_parallelism_threads for best performance. Train on 60000 samples Epoch 1/5 60000/60000 [==============================] - 6s 102us/sample - loss: 0.3018 - accuracy: 0.9140 Epoch 2/5 60000/60000 [==============================] - 6s 103us/sample - loss: 0.1437 - accuracy: 0.9571 Epoch 3/5 60000/60000 [==============================] - 6s 103us/sample - loss: 0.1054 - accuracy: 0.9679 Epoch 4/5 60000/60000 [==============================] - 6s 103us/sample - loss: 0.0868 - accuracy: 0.9729 Epoch 5/5 60000/60000 [==============================] - 6s 103us/sample - loss: 0.0739 - accuracy: 0.9772 10000/1 - 1s - loss: 0.0359 - accuracy: 0.9782 (tf2) ➜ tensorflow2

like image 997
Oscar Mike Avatar asked Jan 03 '20 09:01

Oscar Mike


People also ask

How do you fix a dead kernel in Jupyter Notebook?

Once you've closed the other notebooks, you can restart your dead kernel by going to "Kernel" > "Restart" within Jupyter.

What is Ipykernel in Jupyter Notebook?

The IPython kernel is the Python execution backend for Jupyter. The Jupyter Notebook and other frontends automatically ensure that the IPython kernel is available. However, if you want to use a kernel with a different version of Python, or in a virtualenv or conda environment, you'll need to install that manually.

What happened to the kernel of my Jupyter?

Jupyter, Python: the kernel appears to have died while training a model on a big amount of data

How to update Jupyter Notebook and IPython kernel?

Update jupyter notebook, IPython, IPython kernel; It’s only successful when all three are updated) 1. Open Anaconda navigator 2. Find the updatable package in the corresponding environment

Is it safe to use Jupyter Notebook?

Don't use Jupyter notebook. Use a simple .py script. I know a known problem with notebook. It doesn't allow cuda to deallocate resources once the session is over which creates problems.

Is it possible to fit a CNN model using TensorFlow?

I'm using Tensorflow to fit a CNN model (the total input datasize is only 9.8MB) I did not have this issue in my previous run on the same code (however I did have errors where it said "dst tensor is not initialized". That was when I made the dataset really small to attempt to fit it.


2 Answers

After trying different things I run jupyter notebook on debug mode by using the command:

jupyter notebook --debug

Then after executing the commands on my notebook I got the error message:

OMP: Error #15: Initializing libiomp5.dylib, but found libiomp5.dylib already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can
degrade performance or cause incorrect results. The best thing to do
is to ensure that only a single OpenMP runtime is linked into the
process, e.g. by avoiding static linking of the OpenMP runtime in any
library. As an unsafe, unsupported, undocumented workaround you can
set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the
program to continue to execute, but that may cause crashes or silently
produce incorrect results. For more information, please see
http://www.intel.com/software/products/support/.

And following this discussion, installing nomkl on the virtual environment worked for me.

conda install nomkl
like image 64
Oscar Mike Avatar answered Oct 20 '22 09:10

Oscar Mike


I can't exactly guess the problem you are having but looks like it has do with some version clash. Do the following (that's what I did and it works for me):

  1. conda create -n tf2 python=3.7 ipython ipykernel
  2. conda activate tf2
  3. conda install -c anaconda tensorflow
  4. python -m ipykernel install --user --name=tf2
  5. Run the model again and see if it is working.
like image 39
YOLO Avatar answered Oct 20 '22 09:10

YOLO