jupyter学习手记

本文记录了在Jupyter环境中显示图片、管理conda环境、解决cuDNN初始化错误、配置Theano、监控GPU资源及在VSCode中使用Jupyter Notebook的技巧和问题解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 怎样在jupyter中显示图片
from matplotlib.pyplot import imshow
import numpy as np
from PIL import Image

%matplotlib inline
pil_im = Image.open('data/empire.jpg', 'r').convert('RGB')
imshow(np.asarray(pil_im))
  1. 怎样在jupyter中使用多个conda环境
conda create -n keras2_py3 python=3.6

pip install ipykernel

python -m ipykernel install --user --name keras2_py3 --display-name 'keras2_py3'

# remove
conda remove -n 'name' --all
  1. Error : Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
tf.keras.backend.set_session(tf.Session(config=config))
  1. theano配置
in case you passed the cudnn test [https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#verify], yet still facing the same problem for Theano:

in ~/.theanorc, ensure you have:
[cuda]
root=/usr/local/cuda/bin
[dnn]
include_path=/usr/local/cuda/include
library_path=/usr/local/cuda/lib64
cd /usr/local/cuda/include
sudo cp /usr/include/cudnn.h .
cd /usr/local/cuda/lib64
sudo cp /usr/lib/x86_64-linux-gnu/libcudnn* .

python -c 'import theano'
Using cuDNN version 7103 on context None
Mapped name None to device cuda: GeForce GTX 1080 Ti (0000:02:00.0)
passed.
Error (theano.gpuarray): could not initialize pygpu, support disabled



1) Clean-up:

theano-cache purge



2) Install:

#conda install numpy scipy mkl-service libpython m2w64-toolchain nose

#pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git

#conda install pygpu

#REM Create a new conda env named "python2" with python 2.7
conda create -n python2 python=2

#REM activate python2
activate python2

#REM Then install basic required packages for Theano
conda install numpy scipy mkl-service libpython m2w64-toolchain

#REM Now install theano and pygpu
conda install theano pygpu

#conda install theano=0.9.0

#REM And check if all is OK.
import theano, pygpu, numpy

print(theano.__version__, pygpu.__version__, numpy.__version__)

#REM If all is OK, consider using this conda env ("python2") for your works.
#REM You can deactivate it at anytime:
deactivate

#REM If you want to delete the env, be sure it is deactivated and then run:
conda remove --all -n python2



3) Modify backend:

"image_data_format": "channels_first"

"backend": "theano"

#https://github.com/keras-team/keras/blob/master/keras/backend/common.py#L111



4) Configure to use GPU:

[global]
device=cuda
floatX = float32
optimizer_including=cudnn
openmp=False
allow_input_downcast=True
cxx=C:Anaconda2Librarymingw-w64bing++.exe

[dnn]
enabled=True
include_path=C:Program FilesNVIDIA GPU Computing ToolkitCUDAv8.0include
library_path=C:Program FilesNVIDIA GPU Computing ToolkitCUDAv8.0libx64

[cuda]
root = C:Program FilesNVIDIA GPU Computing ToolkitCUDAv8.0



5) Test http://deeplearning.net/software/theano/tutorial/using_gpu.html

from theano import function, config, shared, sandboximporttheano.tensorasfrom theano import function, config, shared, sandbox
import theano.tensor as T
import numpy
import time
vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000
rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], T.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(iters):
   r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
   print('Used the cpu')
else:
   print('Used the gpu')





Sources:

https://github.com/Theano/libgpuarray/issues/264

https://github.com/Theano/Theano/issues/5831

https://github.com/Theano/Theano/issues/5838

https://zhuanlan.zhihu.com/p/26473699
  1. 周期性输出GPU资源利用率
watch -n 10 nvidia-smi
  1. 在Vscode中Jupyter Notebook的一些技巧(快捷键)
  2. 升级jupyter后提示 500: Internal Server Error,查看log发现ImportError: cannot import name configparser,解决办法:
pip install configparser==3.5.0b2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值