tensorflow环境搭建
2019.03.03 13:31:58字数 337阅读 187
anaconda
首先安装anaconda,官网下载,直接安装,可参考网上教程
python虚拟环境创建
conda create -n tensorflow python=3.7 ipykernel
执行上述命令创建名字为tensorflow的环境,指定该环境python版本为3.7;安装ipykernel包,便于后续jupyter的使用;
conda env list
查看所有环境,带*表示当前激活环境,默认为base环境
conda activate tensorflow
激活tensorflow环境
conda deactivate tensorflow
关闭tensorflow环境,回到base环境
tensorflow安装
conda search ‘*tensorflow*’
conda search ‘*tensorflow-gpu*’
搜索tensorflow版本
conda install -n tensorflow tensorflow-gpu=1.12.0
指定为tensorflow环境安装1.12.0的gpu版本的tensorflow
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print sess.run(hello)
验证是否安装成功
jupyter notebook使用
jupyter notebook
启动jupyter notebook,点击新建,选择kernel(上面conda create时安装ipykernel会自动创建和该虚拟环境关联的kernel),即可创建并在页面上运行python代码。若kernel不是指定环境的,会无法import该环境的module。
若创建环境时忘记安装ipykernel,可在激活环境后安装
conda activate tensorflow
conda install ipykernel
python -m ipykernel install --name tensorflow
最后一行为创建名为tersorflow的kernel,重启jupyter notebook后即可看见新建的kernel。