TensorFlow+Keras结合Matplotlib时内核崩溃的解决求助
TensorFlow/Keras结合Matplotlib时内核崩溃问题解决
问题重现
作为TensorFlow和Keras新手,运行以下代码时,执行到绘图部分(最后4行)内核崩溃:
import numpy as np import os np.random.seed(42) import matplotlib as mpl import matplotlib.pyplot as plt mpl.rc('axes', labelsize=14) mpl.rc('xtick', labelsize=12) mpl.rc('ytick', labelsize=12) def save_fig(fig_id, tight_layout=True, fig_extension="png", resolution=300): print("Saving figure", fig_id) if tight_layout: plt.tight_layout() plt.savefig(fig_id, format=fig_extension, dpi=resolution) import tensorflow as tf from tensorflow import keras (X_train_full, y_train_full), (X_test, y_test) = keras.datasets.mnist.load_data() X_valid, X_train = X_train_full[:5000] / 255., X_train_full[5000:] / 255. y_valid, y_train = y_train_full[:5000], y_train_full[5000:] X_test = X_test / 255. print(y_train) print(X_valid.shape) print(X_test.shape) # Multinomial logistic regression np.random.seed(42) tf.random.set_seed(42) model = keras.models.Sequential([ keras.layers.Flatten(input_shape=[28, 28]), keras.layers.Dense(10, activation="softmax") ]) model.summary() model.compile(loss="sparse_categorical_crossentropy", optimizer="sgd", metrics=["accuracy"]) history = model.fit(X_train, y_train, epochs=3, validation_data=(X_valid, y_valid)) import pandas as pd pd.DataFrame(history.history).plot(figsize=(8, 5)) plt.grid(True) plt.gca().set_ylim(0, 1) save_fig("learning_curves_plot") plt.show()
错误日志
2023-05-07 11:16:49.401786: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2023-05-07 11:16:49.403200: I tensorflow/core/common_runtime/process_util.cc:146] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance. OMP: Error #15: Initializing `libiomp5md.dll`, but found libiomp5 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/.` Fatal Python error: Aborted
主线程调用栈:
Current thread 0x000036b0 (most recent call first): File "<__array_function__ internals>", line 180 in dot File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\matplotlib\transforms.py", line 2441 in get_affine File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\matplotlib\transforms.py", line 2415 in transform_affine File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\matplotlib\transforms.py", line 1490 in transform File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\matplotlib\transforms.py", line 479 in transformed File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\matplotlib\axis.py", line 2475 in get_tick_space File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\matplotlib\ticker.py", line 2083 in _raw_ticks File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\matplotlib\ticker.py", line 2144 in tick_values File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\matplotlib\ticker.py", line 2136 in __call__ File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\matplotlib\axis.py", line 1484 in get_majorticklocs File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\matplotlib\axis.py", line 1262 in _update_ticks File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\matplotlib\axis.py", line 1413 in get_majorticklabels File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\pandas\plotting\_matplotlib\core.py", line 741 in _apply_axis_properties File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\pandas\plotting\_matplotlib\core.py", line 652 in _post_plot_logic_common File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\pandas\plotting\_matplotlib\core.py", line 454 in generate File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\pandas\plotting\_matplotlib\__init__.py", line 71 in plot File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\pandas\plotting\_core.py", line 975 in __call__ File "c:\users\frang\.spyder-py3\tutor\spyder\f06_mnist.py", line 94 in <module> File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\spyder_kernels\py3compat.py", line 356 in compat_exec File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 469 in exec_code File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 611 in _exec_file File "C:\Users\frang\anaconda3\envs\tf\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 524 in runfile File "C:\Users\frang\AppData\Local\Temp\ipykernel_2248\1988908460.py", line 1 in <module> Restarting kernel...
问题原因
这是OpenMP运行时库冲突导致的:TensorFlow(依赖oneDNN)和Matplotlib/NumPy等库各自链接了一份libiomp5md.dll,程序运行时加载了多个重复的OpenMP库实例,引发崩溃。
解决方法
1. 临时绕过(快速生效)
设置环境变量KMP_DUPLICATE_LIB_OK=TRUE,允许重复的OpenMP库加载:
- 代码内设置:在代码最开头添加以下内容:
import os os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" - 系统环境变量设置:Windows用户可在命令行先执行
set KMP_DUPLICATE_LIB_OK=TRUE,再启动Python/Spyder;Linux/macOS则执行export KMP_DUPLICATE_LIB_OK=TRUE。
2. 彻底解决(推荐)
清理重复的OpenMP库,统一依赖版本:
- 检查Anaconda环境下的
libiomp5md.dll文件(通常在envs/tf/Library/bin和envs/tf/lib/site-packages/numpy/.libs等路径),删除重复的文件,仅保留一份(优先保留TensorFlow目录下的版本)。 - 用conda重新安装所有相关库,避免pip和conda混合安装导致的依赖冲突:
conda install tensorflow matplotlib pandas numpy
3. 切换Matplotlib后端
尝试更换Matplotlib的绘图后端,避免触发OpenMP冲突:
在代码开头添加:
import matplotlib matplotlib.use('TkAgg') # 或'Agg'(非交互式,仅保存图片) import matplotlib.pyplot as plt
内容的提问来源于stack exchange,提问作者Francisco Gonzalez
相关产品推荐
相关产品推荐

