TensorFlow学习笔记[1]: TensorFlow安装及在Jupyter notebook中用Pythond代码并生成散列点图
摘要:现在不掌握点人工智能方面的技术,简单都不好意思出门了。前面写《Google人工智能平台TensorFlow介绍》的时候,对TensorFlow有了初步的认识,最近抽空进行较为深入的学习。这将是一个系列笔记,以《TensorFlow for Machine Intelligence》这本书为主线,将该书的精华摘录下来,同时做相应的练习!本文是本系列的第一篇,主要记录TensorFlow的简单安装及使用Jupyter notebook执行Python代码并生成散列图的过程。
TensorFlow基本知识
与TensorFlow类似的机器学习库还有Theano和Caffe。学习TensorFlow最好具有如下基础知识:导数,微积分,线性代数,程序知识,懂Python和C/C++更好!
DistBelief是TensorFlow的前身,后者是为了克服前者的不足而开发的,于2015年11月开源发布。目前TensorFlow被应用于自然语言处理,人工智能,计算机视觉和预测分析。
NumPy is the fundamental package for scientific computing with Python
Jupyter is a web application that allows you to create and share documents that contain live code, equations, visualizations and explanatory text.
Matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms.
Matplotlib is a charting library that allows you to create dynamic, custom visualizations in Python.
TensorFlow安装
虽然在《Google人工智能平台TensorFlow介绍》已经介绍过安装,这里为了记录的完整性,给出一个简化版的安装过程。
- $ sudo apt-get install python-pip python-dev python-virtualenv
- $ virtualenv --system-site-packages ~/tensorflow
- $ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0rc0-cp27-no ne-linux_x86_64.whl
- $ sudo pip install --upgrade $TF_BINARY_URL
#安装Jupyter Notebook
- $ sudo python2 -m pip install ipykernel
- $ sudo python2 -m ipykernel install
- $ sudo apt-get install build-essential
- $ sudo pip install jupyter
#安装matplotlib
- $ sudo apt-get build-dep python-matplotlib python-tk
#激活tensorflow虚拟环境
- $ source tensorflow/bin/activate
-
- #如下这步必不可少(书中没有),否则会报ImportError: No module named matplotlib.pyplot
- $ sudo apt-get install python-matplotlib
-
- (tensorflow)$ mkdir tf-notebooks
- (tensorflow)$ cd tf-notebooks
#启动 jupyter notebook
- (tensorflow)$ jupyter notebook
坑1:直接按照书中的命令"jupyter notebook"执行,会报socket.error: [Errno 99] Cannot assign requested address错误,就指定IP地址启动。如下:
(tensorflow)$ jupyter notebook --ip=120.25.234.39
坑2:必须以sudo启动,否则后面操作的是会报权限错误
(tensorflow)$ sudo jupyter notebook --ip=120.25.234.xx
注:120.25.234.xx是你的IP地址。
坑3:根据上面的安装,后面会报如下错误:
ImportError: No module named matplotlib.pyplot
解决办法:sudo apt-get install python-matplotlib
Juypyter notebook启动成功后会有类似如下提示:
在Jupyter nootebook中用Python代码生成散列点图
可以通过提示的网址在浏览器里进入Jupyter nootebook。在右侧可以创建notebook,在里面可执行如下代码片段:
代码片段,可直接在Jupyter中执行,结果就是一些点散列图
- import tensorflow as tf
- import numpy as np
- import matplotlib.pyplot as plt
-
- #表示直接在浏览器中显示matplotlib图表
- %matplotlib inline
-
- a = tf.random_normal([2,20]) #定义2x20的随机数矩阵
- sess = tf.Session() #启动一个tensorflow会话
- out = sess.run(a) # 用在sess会话里执行a,结果放out里
- x, y = out
-
- plt.scatter(x, y) #用pyplot创建一系列散列点,坐标为x和y
- plt.show()
在文档中生成可视化的散列点图,如下所示:
参考资料:
[1] <TensorFlow for Machine Intelligence> A Hands-On Introduction to Learning Algorithms Bleeding Edge Press 2016
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。