转: TensorFlow学习一:源码安装

地址:

http://blog.csdn.net/u013832707/article/details/73161071


费劲那么长时间,终于把它安装好了,不知参考了多少安装tensorflow的网站, 最后是在这个网站安装成功的。


获取源码

在安装目录下运行:

git clone --recurse-submodules https://github.com/tensorflow/tensorflow
 
 
  • 1
  • 1

其中–recurse-submodules 参数是必须的, 用于获取 TesorFlow 依赖的 protobuf 库.

安装Bazel

首先依照教程安装 Bazel 的依赖.

sudo apt-get install pkg-config zip g++ zlib1g-dev unzip
 
 
  • 1
  • 1

安装openjdk8:

sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-8-jdk
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

配置openjdk 8为默认Java环境:

sudo update-alternatives --config java
sudo update-alternatives --config javac
 
 
  • 1
  • 2
  • 1
  • 2

然后下载 Bazel 的源码,解压之后在根目录运行:

./compile.sh
sudo cp output/bazel /usr/local/bin
 
 
  • 1
  • 2
  • 1
  • 2

安装其他依赖

sudo apt-get install python-numpy swig python-dev
sudo apt-get install python-numpy python-dev python-pip python-wheel
 
 
  • 1
  • 2
  • 1
  • 2

其中通过apt-get install 安装的pip太老了,需要升级

sudo pip install --upgrade pip
 
 
  • 1
  • 1

安装CUDA以及CUDNN

参考http://blog.csdn.net/u013832707/article/details/53157976

安装MKL

https://registrationcenter.intel.com/en/products/postregistration/?sn=33RM-MPPRMLGB&EmailID=gphsmail%40163.com&Sequence=2038178下载MKL 
解压,运行./install.sh即可,完成后添加环境变量:

export LD_LIBRARY_PATH=/opt/intel/mkl/lib/intel64:$LD_LIBRARY_PATH
 
 
  • 1
  • 1

配置安装tensorflow

从源码树的根路径执行:

./configure 
进行一些配置,不知道怎么选的就直接回车默认就好,如下:

gph@gph-pc:~/tensorflow-master/tensorflow $ sudo ./configure 
You have bazel 0.5.1- installed.
Please specify the location of python. [Default is /usr/bin/python]: 
Found possible Python library paths:
  /usr/local/lib/python2.7/dist-packages
  /usr/lib/python2.7/dist-packages
Please input the desired Python library path to use.  Default is [/usr/local/lib/python2.7/dist-packages]

Using python library path: /usr/local/lib/python2.7/dist-packages
Do you wish to build TensorFlow with MKL support? [y/N] y
MKL support will be enabled for TensorFlow
Do you wish to download MKL LIB from the web? [Y/n] n
Please specify the location where MKL is installed. [Default is /opt/intel/mklml]: /opt/intel/mkl
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: 
Do you wish to use jemalloc as the malloc implementation? [Y/n] 
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] 
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N] 
No Hadoop File System support will be enabled for TensorFlow
Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N] 
No XLA support will be enabled for TensorFlow
Do you wish to build TensorFlow with VERBS support? [y/N] 
No VERBS support will be enabled for TensorFlow
Do you wish to build TensorFlow with OpenCL support? [y/N] 
No OpenCL support will be enabled for TensorFlow
Do you wish to build TensorFlow with CUDA support? [y/N] y
CUDA support will be enabled for TensorFlow
Do you want to use clang as CUDA compiler? [y/N] 
nvcc will be used as CUDA compiler
Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 8.0]: 
Please specify the location where CUDA  toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: 
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: 
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]: 5.1
Please specify the location where cuDNN 5.1 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: 
Invalid path to cuDNN  toolkit. Neither of the following two files can be found:
/usr/local/cuda-8.0/lib64/libcudnn.so.5.1
/usr/local/cuda-8.0/libcudnn.so.5.1
.5.1
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]: 5.1.10
Please specify the location where cuDNN 5.1.10 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: 
Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size.
[Default is: "3.5,5.2"]: 6.1
Do you wish to build TensorFlow with MPI support? [y/N] 
MPI support will not be enabled for TensorFlow
Configuration finished
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

编译目标程序, 开启 GPU 支持:

bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
 
 
  • 1
  • 1

输出如下:

At global scope: 
cc1plus: warning: unrecognized command line option “-Wno-self-assign” [enabled by default] 
Target //tensorflow/tools/pip_package:build_pip_package up-to-date: 
bazel-bin/tensorflow/tools/pip_package/build_pip_package 
INFO: Elapsed time: 1737.072s, Critical Path: 175.18s

bazel编译命令建立了一个名为build_pip_package的脚本。运行如下的命令将会在 /tmp/tensorflow_pkg路径下生成一个.whl文件:

bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
 
 
  • 1
  • 1

输出如下:

~/tensorflow-master/tensorflow 2017年 06月 13日 星期二 20:14:17 CST : === 
Output wheel file is in: /tmp/tensorflow_pkg

安装pip包

使用如下命令安装生成的pip包。注意.whl的名字与你的平台有关:

sudo pip install /tmp/tensorflow_pkg/tensorflow-1.1.0-py2-none-any.whl
 
 
  • 1
  • 1

验证你的安装

打开任意一个新的终端,注意不要在tensorflow的安装路径下,运行

python
 
 
  • 1
  • 1

输入一下代码

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

得到输出:

Hello, TensorFlow!

错误记录

一遇到如下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "tensorflow/python/pywrap_tensorflow.py", line 52, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "tensorflow/python/pywrap_tensorflow.py", line 41, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
ImportError: No module named pywrap_tensorflow_internal


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_problems

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named tensorflow
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

解决办法: 
如果你是按照中文社区的源码编译方式,那么这就是因为教程太旧了的原因,请按照上文的方法安装。

二遇到如下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py", line 23, in <module>
    from tensorflow.python import *
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py", line 53, in <module>
    from tensorflow.core.framework.graph_pb2 import *
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/core/framework/graph_pb2.py", line 9, in <module>
    from google.protobuf import symbol_database as _symbol_database
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/symbol_database.py", line 164, in <module>
    _DEFAULT = SymbolDatabase(pool=descriptor_pool.Default())
AttributeError: 'module' object has no attribute 'Default'
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

请升级你的protobuf

sudo pip install --upgrade protobuf

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TensorFlow.js 是一个用于在浏览器和 Node.js 中训练和部署机器学习模型的开源库。要实现聊天功能,我们可以利用 TensorFlow.js 中的自然语言处理模型,如文本生成模型或情感分析模型。以下是一个简单的实现聊天功能的示例源码: 首先,我们需要引入 TensorFlow.js 库和训练好的自然语言处理模型: ```javascript import * as tf from '@tensorflow/tfjs'; import '@tensorflow/tfjs-node'; import * as nlp from '@tensorflow-models/universal-sentence-encoder'; ``` 然后,我们可以编写一个函数来处理用户输入的文本,并根据模型的预测结果生成回复: ```javascript async function chat(inputText) { // 加载预训练的自然语言处理模型 const model = await nlp.load(); // 将用户输入的文本换为向量 const inputEmbedding = await model.embed(inputText); // 使用模型预测下一步回复的文本 const responseEmbedding = // 从数据库或模型中获取回复向量 const responseText = // 从数据库或模型中获取回复文本 return responseText; } ``` 最后,我们可以在用户输入文本后调用该函数,获取模型生成的回复,并显示在页面上: ```javascript const userInput = // 获取用户输入的文本 const response = chat(userInput); // 在页面上显示模型生成的回复 console.log(response); ``` 通过以上代码示例,我们可以利用 TensorFlow.js 中的自然语言处理模型来实现聊天功能,从而让用户能够与模型进行对话交流。这种方式可以通过在浏览器中部署模型来实现实时响应,为用户提供更加智能和互动的聊天体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值