机器学习入门(4):Ubuntu如何在Jupyter Notebook导入Tensorflow

本文详细介绍了在Ubuntu环境下设置虚拟环境tf1,安装并配置TensorFlow和Jupyter的过程,包括激活环境、安装所需库、配置内核以及解决过程中遇到的鼠标问题。通过这些步骤,可以实现在JupyterNotebook中顺利导入和使用TensorFlow。

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

我们先进入到tf1这个虚拟环境

(1)激活环境

source activate tf1

(2)查看有哪些库

conda list

source deactivate tf1

遇到问题:

ubuntu 遇到鼠标变为十字 无法点击的问题

原因:我在终端输入python,然后输入import tensorflow as tf,然后打开jupyter notebook。鼠标根本就动不了!

参考https://blog.youkuaiyun.com/qq_42393859/article/details/85247935

遇到之后请Ctrl + Alt + F1切到命令行界面下,然后输入命令

ps -e | grep import

kill -9 21526(import的进程)

在ctrl + alt + F7切回gnome下,然后重新登录即可

之后问题解决。

(3)依次执行以下命令

1.(tf1) rookie@ubuntu:~$ conda install ipython

结果:

Downloading and Extracting Packages

。。。(省略)

ipython-6.1.0 | 1013 KB | ################################################################################################################################################################# | 100%

Preparing transaction: done

Verifying transaction: done

Executing transaction: done

2.(tf1) rookie@ubuntu:~$ conda install jupyter

结果如下:

_libgcc_mutex-0.1 | 3 KB | ################################################################################################################################################################# | 100%

Preparing transaction: done

Verifying transaction: done

Executing transaction: done

3.(tf1) rookie@ubuntu:~$ ipython kernelspec install-self --user

(tf1) rookie@ubuntu:~$ ipython kernelspec install-self --user

[TerminalIPythonApp] WARNING | Subcommand `ipython kernelspec` is deprecated and will be removed in future versions.

[TerminalIPythonApp] WARNING | You likely want to use `jupyter kernelspec` in the future

[InstallNativeKernelSpec] WARNING | `jupyter kernelspec install-self` is DEPRECATED as of 4.0. You probably want `ipython kernel install` to install the IPython kernelspec.

[InstallNativeKernelSpec] Installed kernelspec python3 in /home/rookie/.local/share/jupyter/kernels/python3

4.(tf1) rookie@ubuntu:~$ mv ~/.local/share/jupyter/kernels/python3 ~/.ipython/kernels/tfkernel

(tf1) rookie@ubuntu:~$ ipython kernelspec install-self --user

[TerminalIPythonApp] WARNING | Subcommand `ipython kernelspec` is deprecated and will be removed in future versions.

[TerminalIPythonApp] WARNING | You likely want to use `jupyter kernelspec` in the future

[InstallNativeKernelSpec] WARNING | `jupyter kernelspec install-self` is DEPRECATED as of 4.0. You probably want `ipython kernel install` to install the IPython kernelspec.

[InstallNativeKernelSpec] Installed kernelspec python3 in /home/rookie/.local/share/jupyter/kernels/python3

我这里得到的结果(1)是Installed kernelspec python3 in /home/rookie/.local/share/jupyter/kernels/python3

5.(tf1) rookie@ubuntu:~$ mkdir -p ~/.ipython/kernels

然后运行下边的命令,使用你选择的名字来代替<kernel_name>(我使用的tfkernel),并且刚才得到的结果(1)路径(例如,~/.local/share/jupyter/kernels/python3)来替换下方命令中的第一个路径。

mv ~/.local/share/jupyter/kernels/pythonX ~/.ipython/kernels/<kernel_name>

6.(tf1) rookie@ubuntu:~$ mv ~/.local/share/jupyter/kernels/python3 ~/.ipython/kernels/tfkernel

7.现在,打开Jupyter Notebook,选择Kernel -> Change kernel,你将看到一个新的kernel。但是,新的kernel与你之前的kernel拥有相同点名字,运行下边的命令,给你的新kernel起一个不同的名字。cd ~/.ipython/kernels/tfkernel/。接着,运行vim kernel.json来编辑kernel.json文件,将"display_name"中的默认值Python 3替换为你的新名字,然后保存,并退出。

(tf1) rookie@ubuntu:~$ cd ~/.ipython/kernels/tfkernel/

(tf1) rookie@ubuntu:~/.ipython/kernels/tfkernel$ vim kernel.json

(tf1) rookie@ubuntu:~/.ipython/kernels/tfkernel$ jupyter noteboo

8.打开一个新的Jupyter Notebook,输入一行import tensorflow as tf并运行,如果没有出现任何错误,那么就搞定了。

参考:

https://blog.youkuaiyun.com/xue_wenyuan/article/details/51545845

9.最后试验一下tensorflow是不是可以用

import tensorflow as tf

sess = tf.Session()

a = tf.constant(2)

b = tf.constant(3)

print(sess.run(a+b)) 

然后输出如下:
机器学习入门(4):Ubuntu如何在Jupyter Notebook导入Tensorflow

输出5,这样就成功啦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骨子带刺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值