打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
TensorFlow学习笔记[1]: TensorFlow安装及在Jupyter notebook中用Pythond代码并生成散列点图
TensorFlow学习笔记[1]: TensorFlow安装及在Jupyter notebook中用Pythond代码并生成散列点图
作者:雨水/家辉,日期:2016-10-15,CSDN博客:http://blog.csdn.net/gobitan

摘要:现在不掌握点人工智能方面的技术,简单都不好意思出门了。前面写《Google人工智能平台TensorFlow介绍》的时候,对TensorFlow有了初步的认识,最近抽空进行较为深入的学习。这将是一个系列笔记,以《TensorFlow for Machine Intelligence》这本书为主线,将该书的精华摘录下来,同时做相应的练习!本文是本系列的第一篇,主要记录TensorFlow的简单安装及使用Jupyter notebook执行Python代码并生成散列图的过程。

TensorFlow基本知识
与TensorFlow类似的机器学习库还有Theano和Caffe。学习TensorFlow最好具有如下基础知识:导数,微积分,线性代数,程序知识,懂Python和C/C++更好!
DistBelief是TensorFlow的前身,后者是为了克服前者的不足而开发的,于2015年11月开源发布。目前TensorFlow被应用于自然语言处理,人工智能,计算机视觉和预测分析。
什么是Tensor? 简单地说,Tensor就是N维矩阵。TensorFlow 0.8.0版本之后才具有分布式特性。TensorFlow的API主要以Python语言提供,但实际地计算代码是由C++语言写的。TensorFlow能够运行在任何大小设备的系统平台上,这里是一个Android平台运行的例子,https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android

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介绍》已经介绍过安装,这里为了记录的完整性,给出一个简化版的安装过程。
[plain] view plain copy
  1. $ sudo apt-get install python-pip python-dev python-virtualenv  
  2. $ virtualenv --system-site-packages ~/tensorflow  
  3. $ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0rc0-cp27-no ne-linux_x86_64.whl  
  4. $ sudo pip install --upgrade $TF_BINARY_URL  
#安装Jupyter Notebook
[plain] view plain copy
  1. $ sudo python2 -m pip install ipykernel  
  2. $ sudo python2 -m ipykernel install  
  3. $ sudo apt-get install build-essential  
  4. $ sudo pip install jupyter  
#安装matplotlib
[plain] view plain copy
  1. $ sudo apt-get build-dep python-matplotlib python-tk  
#激活tensorflow虚拟环境
[plain] view plain copy
  1. $ source tensorflow/bin/activate  
  2.   
  3. #如下这步必不可少(书中没有),否则会报ImportError: No module named matplotlib.pyplot  
  4. $ sudo apt-get install python-matplotlib  
  5.   
  6. (tensorflow)$ mkdir tf-notebooks  
  7. (tensorflow)$ cd tf-notebooks  
#启动 jupyter notebook
[plain] view plain copy
  1. (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启动成功后会有类似如下提示:
The Jupyter Notebook is running at: http://120.25.234.XX:8888/

在Jupyter nootebook中用Python代码生成散列点图
可以通过提示的网址在浏览器里进入Jupyter nootebook。在右侧可以创建notebook,在里面可执行如下代码片段:
代码片段,可直接在Jupyter中执行,结果就是一些点散列图
[python] view plain copy
  1. import tensorflow as tf  
  2. import numpy as np  
  3. import matplotlib.pyplot as plt  
  4.   
  5. #表示直接在浏览器中显示matplotlib图表  
  6. %matplotlib inline   
  7.   
  8. a = tf.random_normal([2,20]) #定义2x20的随机数矩阵  
  9. sess = tf.Session()  #启动一个tensorflow会话  
  10. out = sess.run(a)    # 用在sess会话里执行a,结果放out里  
  11. x, y = out                 
  12.   
  13. plt.scatter(x, y)    #用pyplot创建一系列散列点,坐标为x和y  
  14. plt.show()  

在文档中生成可视化的散列点图,如下所示:

注:本文中提到的《TensorFlow for Machine Intelligence》的英文pdf版已上传到CSDN资源,地址为:http://download.csdn.net/detail/gobitan/9654931

参考资料:
[1] <TensorFlow for Machine Intelligence>  A Hands-On Introduction to Learning Algorithms  Bleeding Edge Press 2016
[2] http://blog.csdn.net/gobitan/article/details/51712207  Google人工智能平台TensorFlow介绍
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
小白入门Python数据科学全教程
超实用的机器学习 & 数据分析代码小抄,送你了!
启动 jupyter notebook
如何在Jupyter Notebook中使用Python虚拟环境?
在jupyter notebook中添加conda创建的环境(kernel)
Python开发神器jupyter notebook #python #编程
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服