tensorflow with virtualenv and Hello world

安裝一個虛擬器,

參考書本 TensorFlow学习指南:深度学习系统构建详解 ch2安装TensorFlow

內容為書上節錄,如果有侵權,我會馬上刪除此篇!!!! 

網路上也有一篇提到很多個安裝虛擬環境的方式

這邊是書上提到 輕鬆安裝使用 pip:

 pip install tensorflow

有個缺點,就是會覆蓋原本已安裝的python版本,

如果有其他用途使用,會很麻煩,所以可以考慮安裝虛擬器

pip install virualenv

直接參考書中安裝虛擬環境 ~/ens 儲存名為 tensorflow 的虛擬環境,

虛擬環境在此為(~/envs/tensorflow 文件中),可以自己改名稱~

cd ~

mkdir envs

virtualenv ~/envs/tensorflow

安裝之後進入虛擬機的使用方式:

source ~/envs/tensorflow/bin/activate

teminal會變成

(tensorflow)$

在此再安裝 TensorFlow, 不會影響原本電腦上安裝過的包,cpu&gpu版本

(tensorflow)$ pip install tensorflow 

(tensorflow)$ pip install --upgrade tensorflow-gpu

檢查方式跟結果會跟上一篇一樣:

(tensorflow)$ python

>>> from tensorflow.python.client import device_lib
>>> device_lib.list_local_devices()

結束之後出虛擬機的方式

(tensorflow)$ deactivate

*覺得每次都要輸入  source ~/envs/tensorflow/bin/activate 很煩的話

 可以考慮在 ~/.bashrc加入虛擬的路徑:

alias tensorflow="source ~/envs/tensorflow/bin/activate"

以後都只要輸入 tensorflow就可以進入tensorflow的虛擬環境了。

跳出方式還是一樣輸入 deactivate .

 

 測試Hello world in python

import tensorflow as tf

 print(tf.__version__)
1.9.0

h = tf.constant("Hello")

w = tf.constant("World!")

hw = h+w   

with tf.Session() as sess:
...  ans = sess.run(hw)

...
2018-07-22 18:02:39.945291: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-07-22 18:02:40.010564: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:897] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-07-22 18:02:40.010922: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1392] Found device 0 with properties:
name: GeForce GTX 950M major: 5 minor: 0 memoryClockRate(GHz): 1.124
pciBusID: 0000:01:00.0
totalMemory: 1.96GiB freeMemory: 198.62MiB
2018-07-22 18:02:40.010941: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1471] Adding visible gpu devices: 0
2018-07-22 18:02:40.499964: I tensorflow/core/common_runtime/gpu/gpu_device.cc:952] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-07-22 18:02:40.499994: I tensorflow/core/common_runtime/gpu/gpu_device.cc:958]      0
2018-07-22 18:02:40.500013: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 0:   N
2018-07-22 18:02:40.500135: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1084] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 141 MB memory) -> physical GPU (device: 0, name: GeForce GTX 950M, pci bus id: 0000:01:00.0, compute capability: 5.0)
>>> print ans
HelloWorld!

Its have to add (space) before ans = sess.run(hw).

and express Enter twice then print ans.

This is the first example using TensorFLow .

it's a little bit driffer between pyhon.

>>> print hw
Tensor("add:0", shape=(), dtype=string)

ph = "Hello"

pw = "World!"

phw = ph + pw

>>>print phw

Hello World! 

Tensorflow 計算圖模型的操作方式:

先定義計算操作,之後使用外部機制去觸發這個計算!

在 hw = h + w 的時候,其實還沒實際計算求合的操作,

Session為外部計算機的接口,允許我們計算圖中的部分,

 with tf.Session() as sess:
...  ans = sess.run(hw)

最後print ans 才會是我們要的結果!!

 

 



 

 


 

留言