tensorflow学习笔记(一)安装
1.tensorflow介绍
中文社区地址 http://www.tensorfly.cn/
TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库。
数据流图就是这个东西:
每一个节点(Nodes)都表示一个数学操作,也可以表示数据输入(feed in)的起点/输出(push out)的终点,或者是读取/写入持久变量(persistent variable)的终点。
2.安装
linux上
看完了介绍与一个不怎么看得懂的python计算程序,开始安装,选择在linux上基于 VirtualEnv 的安装
我的Ubuntu18.04上已经有了python2.7,运行下面命令安装依赖
#在使用apt安装任何软件包之前,建议使用以下命令更新软件包列表 $ sudo apt update # 在 Linux 上: $ sudo apt-get install python-pip python-dev python-virtualenv
安装过程比较慢,等待安装完成
在阿里云的linux貌似CPU100%炸了,卡住了,网速出问题了,不如来安装一下Node版的
注意,Windows构建支持node-gyp
需要Python 2.7。请务必在安装之前,这个版本@tensorflow/tfjs-node
还是@tensorflow/tfjs-node-gpu
。使用Python 3.x的计算机无法正确安装绑定。
Installing CPU TensorFlow.js for Node:
npm install @tensorflow/tfjs-node
Installing Linux/Windows GPU TensorFlow.js for Node:
npm install @tensorflow/tfjs-node-gpu
安装node-gpu时出错了,因为windows版不支持GPU
C:\JXH\NodeWorkSpace\TensorFlow\node_modules\@tensorflow\tfjs-node-gpu\scripts\install.js:165 throw new Error('node-gyp rebuild failed with: ' + err); ^ Error: node-gyp rebuild failed with: Error: Command failed: node-gyp rebuild gyp ERR! build error gyp ERR! stack Error: `C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe` failed with exit code: 1 gyp ERR! stack at ChildProcess.onExit (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\build.js:258:23) gyp ERR! stack at emitTwo (events.js:126:13) gyp ERR! stack at ChildProcess.emit (events.js:214:7) gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12) gyp ERR! System Windows_NT 6.1.7601 gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" gyp ERR! cwd C:\JXH\NodeWorkSpace\TensorFlow\node_modules\@tensorflow\tfjs-node-gpu gyp ERR! node -v v8.9.1 gyp ERR! node-gyp -v v3.6.2 gyp ERR! not ok
好像是版本不匹配,回头一看,linux上的终于完成了,由于node的版本暂时不能换,先这样吧,回头换个版本再试,继续linux的
看一下安装的pip及virtualenv版本
root@jixh:~# pip --version pip 19.0.2 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7) root@jixh:~# virtualenv --version 15.1.0 root@jixh:~#
接下来, 建立一个全新的 virtualenv 环境. 为了将环境建在 ~/tensorflow
目录下, 执行:
$ virtualenv --system-site-packages ~/tensorflow
$ cd ~/tensorflow
然后, 激活 virtualenv:
$ source bin/activate # 如果使用 bash $ source bin/activate.csh # 如果使用 csh (tensorflow)$ # 终端提示符应该发生变化
然而并不起作用
Ubuntu上以virtualenv方式安装TensorFlow
按专业的也不行,是阿里云的服务器无法访问,报了个404错误。
只好直接使用二进制安装,一步到位。
# 仅使用 CPU 的版本 $ pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl
示例测试成功
$ python >>> import tensorflow as tf >>> hello = tf.constant('Hello, TensorFlow!') >>> sess = tf.Session() >>> print sess.run(hello) Hello, TensorFlow! >>> a = tf.constant(10) >>> b = tf.constant(32) >>> print sess.run(a+b) 42 >>>
windows上
python3.6.5下安装
pip install --user tensorflow 不指定版本会默认安装1.3.0
pip install --user --upgrade tensorflow==1.4.0 或者安装/更新需要的版本
外网下载,安装过程巨慢
如果出现找不到对tensorflow版本,说明python不支持1.4.0
查看Windows下TensorFlow对python版本的要求
终于安装完成,结果如下
Installing collected packages: wheel, absl-py, termcolor, grpcio, gast, werkzeug, protobuf, markdown, tensorboard, h5py, keras-applications, keras-preprocessing, astor, tensorflow Running setup.py install for absl-py ... done Running setup.py install for termcolor ... done Running setup.py install for gast ... done Successfully installed absl-py-0.7.1 astor-0.7.1 gast-0.2.2 grpcio-1.19.0 h5py-2.9.0 keras-applications-1.0.7 keras-preprocessing-1.0.9 markdown-3.1 protobuf-3.7.1 tensorboard-1.11.0 tensorflow-1.11.0 termcolor-1.1.0 werkzeug-0.15.2 wheel-0.33.1
继续往下
VsCode中Python环境配置
改上两个自己的安装路径就可以愉快的开始Hello world了。
测试安装的tensorflow
import tensorflow as tf sess = tf.Session() a = tf.constant(10) b = tf.constant(22) print(sess.run(a + b))
运行结果如下
2019-04-15 17:28:28.979407: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 32
安装完毕,毫无问题。
基本用法看完,半脸懵逼。
开始入门教程吧
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探