问题

在Jupyter notebook中用matplotlib.pyplot出现服务器挂掉、崩溃

import torch
from torch.autograd import Variable
import matplotlib.pyplot as plt
tensor = torch.linspace(-6,6,200)
tensor = Variable(tensor)
np_data = tensor.numpy()
#定义激活函数
y_relu = torch.relu(tensor).data.numpy()
y_sigmoid =torch.sigmoid(tensor).data.numpy()
y_tanh = torch.tanh(tensor).data.numpy()

运行:

%matplotlib inline
plt.figure(1, figsize=(8, 6))

解决

1.先尝试三种方式:

%matplotlib notebook
#在开头加上如下代码可以在jupyter notebook行内形成交互式的图表
%matplotlib
#在开头加上如下代码可以在mac内形成交互式的图表,即会弹出图像窗口
%matplotlib inline
#开头加上如下代码可以显示图像,但无交互功能

仍然崩溃

2 查看原因

点击启动Jupyter notebook的终端程序,终端上会记录Jupyter notebook的运行信息,可以从中查看错误原因

OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll 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/.
[I 21:55:26.126 NotebookApp] KernelRestarter: restarting kernel (1/5), keep random ports
kernel 4e65633e-7300-4a63-a0fc-1df4e657672f restarted

解决:
在pytorch中导入以下模块:

import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

重新运行即可。

本文地址:https://blog.csdn.net/weixin_44671418/article/details/110453108