2016年9月30日 星期五

install tensor flow on raspberry

Ref ;  https://github.com/samjabrahams/tensorflow-on-raspberry-pi


This is the easiest way to get TensorFlow onto your Raspberry Pi 3. Note that currently, the pre-built binary is targeted for Raspberry Pi 3 running Raspbian 8.0 ("Jessie"), so this may or may not work for you.
First, install the dependencies for TensorFlow:
# For Python 2.7
$ sudo apt-get install python-pip python-dev

# For Python 3.3+
$ sudo apt-get install python3-pip python3-dev
Next, download the wheel file from this repository and install it:
# For Python 2.7
$ wget https://github.com/samjabrahams/tensorflow-on-raspberry-pi/raw/master/bin/tensorflow-0.9.0-cp27-none-linux_armv7l.whl
$ sudo pip install tensorflow-0.9.0-cp27-none-linux_armv7l.whl

# For Python 3.3+
$ wget https://github.com/samjabrahams/tensorflow-on-raspberry-pi/raw/master/bin/tensorflow-0.9.0-py3-none-any.whl
$ sudo pip install tensorflow-0.9.0-py3-none-any.whl


Troubleshooting

This section will attempt to maintain a list of remedies for problems that may occur while installing from pip

"tensorflow-0.9-cp27-none-linux_armv7l.whl is not a supported wheel on this platform."

This wheel was built with Python 2.7, and can't be installed with a version of pip that uses Python 3. If you get the above message, try running the following command instead:
$ sudo pip2 install tensorflow-0.9-cp27-none-linux_armv7l.whl
Vice-versa for trying to install the Python 3 wheel. If you get the error "tensorflow-0.9-py3-none-any.whl is not a supported wheel on this platform.", try this command:
$ sudo pip3 install tensorflow-0.9-py3-none-any.whl

Building from Source

If you aren't able to make the wheel file from the previous section work, you may need to build from source. Additionally, if you want to use features that have not been included in an official release, such as the initial distributed runtime, you'll have to build from source. Don't worry, as we've figured out most of the quirks of getting it right. The guide will be updated as needed to be as correct as possible.
See the step-by-step guide hereWarning: it takes a while.

https://github.com/jikexueyuanwiki/tensorflow-zh/blob/master/SOURCE/get_started/os_setup.md

尝试你的第一个 TensorFlow 程序

(可选) 启用 GPU 支持

如果你使用 pip 二进制包安装了开启 GPU 支持的 TensorFlow, 你必须确保 系统里安装了正确的 CUDA sdk 和 CUDNN 版本. 请参间 CUDA 安装教程
你还需要设置 LD_LIBRARY_PATH 和 CUDA_HOME 环境变量. 可以考虑将下面的命令 添加到 ~/.bash_profile 文件中, 这样每次登陆后自动生效. 注意, 下面的命令 假定 CUDA 安装目录为 /usr/local/cuda:
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64"
export CUDA_HOME=/usr/local/cuda

运行 TensorFlow

打开一个 python 终端:
$ 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
>>>







tensor_flow 的一些資料

Ref : http://darren1231.pixnet.net/blog/post/332022975-tensorflow%E6%95%99%E5%AD%B8----%E5%BB%BA%E7%BD%AE%E4%B8%80%E5%80%8B%E5%9F%BA%E7%A4%8E%E7%A5%9E%E7%B6%93%E7%B6%B2%E8%B7%AF



python  語法

程式碼:

引用必要函數
import tensorflow as tf
import numpy as np

# creat data
創造 亂數100個亂數 值介於0~1之間
x_data = np.random.rand(100).astype(np.float32)
訂出要學習的函數  這裡要學的是 weight:0.3  biases:0.3
y_data = x_data*0.1+0.3
###creat tensorflow structure start###

定立weights 的 範圍 和初始化
Weights = tf.Variable(tf.random_uniform([1],-1.0,1.0))
biases = tf.Variable(tf.zeros([1]))

定立 給 tensorflow 學習的函數
y = Weights*x_data + biases

建立 loss 規則
loss = tf.reduce_mean(tf.square(y-y_data))

reduce_mean 用法:
可以參考這裡,tensorflow常用函數


選擇學習機制
optimizer = tf.train.GradientDescentOptimizer(0.5)  #learning rate

向tensorflow 說 訓練規則就是把 loss 減到最小 最好是0
train = optimizer.minimize(loss)

初始化所有變數
init = tf.initialize_all_variables()

###creat tensorflow structure end###
上面都是建立規則


*****************************************************開始訓練
建立sess
sess = tf.Session()
記得初始化
sess.run(init)     #Very important

跑 for 迴圈 更新wight201次
然後每訓練20次 印出一次weight
for step in range(201):
    sess.run(train)
    if step % 20 ==0:
        print(step,sess.run(Weights),sess.run(biases))
 


程式輸出:
(0, array([ 0.30737674], dtype=float32), array([ 0.25841016], dtype=float32))
(20, array([ 0.13939281], dtype=float32), array([ 0.27776307], dtype=float32))
(40, array([ 0.10886393], dtype=float32), array([ 0.29499638], dtype=float32))
(60, array([ 0.10199451], dtype=float32), array([ 0.29887414], dtype=float32))
(80, array([ 0.10044879], dtype=float32), array([ 0.29974666], dtype=float32))
(100, array([ 0.10010101], dtype=float32), array([ 0.299943], dtype=float32))
(120, array([ 0.10002275], dtype=float32), array([ 0.29998717], dtype=float32))
(140, array([ 0.10000515], dtype=float32), array([ 0.29999712], dtype=float32))
(160, array([ 0.10000117], dtype=float32), array([ 0.29999936], dtype=float32))
(180, array([ 0.10000026], dtype=float32), array([ 0.29999986], dtype=float32))
(200, array([ 0.10000011], dtype=float32), array([ 0.29999995], dtype=float32))

==================================================================
一些tensor flow 常用的function



Tensorflow 常用函數解說

tf.split(split_dim, num_split, value, name='split')

函數解說:將大的tensor分割成更小的tensor第一個參數代表沿著那一維開始分割,第二個參數代表切成幾段,如下面例子,「5,30」沿著第一維也就是column開始切割,切成三段,因此就有3個「5,10」的tensor被分割出來

函數範例:


tf.reduce_mean(input_tensor,reduction_indices=None,keep_dims=False, name=None)


函數解說:將tensor取平均,第二個參數代表沿著那一維取平均,例如範例第二個,沿著第0維也就是row取平均得到「1.5,1.5」,手指沿著row的方向掃過,再如第3個範例,沿著第1維也就是column取平均得到[1,2],手指沿著column方向掃過

函數範例:



tf.reduce_mean(x) ->  (1 + 1 + 2 + 2) / 4 = 1.5

tf.reduce_mean(x,0) ->  (1 + 2) / 2 = 1.5
                                        (1 + 2) / 2 = 1.5


tf.reduce_mean(x,1) ->  (1 + 1) / 2 = 1
                                        (2 + 2) / 2 = 2


tf.reduce_sum(input_tensor,reduction_indices=None, keep_dims=False, name=None)

函數解說:將tensor加總起來,第二個參數代表沿著那一維加總,例如範例第二個,沿著第0維也就是row加總得到[2,2,2],手指沿著row的方向掃過,再如第3個範例,沿著第1維也就是column加總得到[3,3],手指沿著column方向掃過,第3個參數如果為True的話,那麼所切出來的長度會回復到1,如同第四個例子一樣,第五個例子表示可以不只沿著其中一個維度加總。

函數範例:


f.reshape(tensor, shape, name=None)

函數解說:將tensor的維度重新改寫,-1代表自動計算該維度的數量

函數範例:




tf.matmul(a,b,ranspose_a=False,transpose_b=False,_is_sparse=False,_is_sparse=False,ame=None)

函數解說:將a,b兩個矩陣相乘,如果需要事先轉置的話,可以把個別的選項調成True,如果矩陣裏面包括很多0的話,可以調用spare=True轉為更有效率的演算法

函數範例:

tf.argmin(input, dimension, name=None)

函數解說:沿著需要的維度找尋最小值的索引值,最小由0開始