2016年10月3日 星期一

POSTMAN -> 測試 API 的好工具

Ref : http://blog.roachking.net/blog/2012/11/07/postman-restful-client/


因為工作的關係,常常寫一些 API 供 APP 使用。
以前傻傻的,每次測試的時候都會自己刻一個 HTML 的表單,一個一個填入 input ,接著送出。 後來覺得這樣太慢了,就用 JavaScript 寫了一個程式來送, 可是效率都沒有很好,尤其是需要反覆測試更改條件的時候。
之後在同事的推薦下用了 Burpsuite ,而這套軟體確實是可以做到沒錯,但是讓人有一種「殺雞焉用牛刀」的感覺。 因此又陸續找了幾個模擬 HTTP requests 的工具,卻都不甚理想。最近終於找到一套滿意的,也就是今天要介紹的 Postman。
Postman 是一個 Chrome 的 Extension,安裝以後可以在分頁欄裡面看到 Postman 的 Icon:

Postman 的主要功能

  • 模擬各種 HTTP requests:從常用的 GET、POST 到 RESTful 的 PUT 、 DELETE …等等。 甚至還可以送出檔案、送出額外的 header。
  • Collection 功能:Collection 是 requests的集合,在做完單一個測試的時候, 你可以把這次的 request 存到特定的 Collection 裡面,如此一來,下次要測試的時候,就不需要重新輸入。
    養成習慣以後,網站 API 的每個方法都寫好存進去,以後在開發的時候,就可以迅速得看到結果。 而 Collection 還可以 Import 或是 Share 出來,讓團隊裡面的其他人,可以一起使用你建立起來的 Collection。
  • 整理過後的回傳結果:一般在用其他工具來測試的時候,回傳的東西通常都是純文字的 raw, 但如果是 JSON ,就是塞成一整行的 JSON。這會造成閱讀時的障礙 ,而 Postman 可以針對回傳資料的格式自動美化。 JSON、 XML 或是 HTML 都會整理成人類可以閱讀的型態。
  • 設定環境:Postman 可以自由新增 Environment,一般我們可能會有多種環境, development 、 staging 或 local, 而這幾種環境的 request URL 也各不相同。新增 Environment,可以讓我們設定一些環境變數,使得切換環境測試的時候, 不用重寫 request。

小測試

我們丟一個 Request 到 Google Geocoding API ,讓大家看看實際的執行結果:




可以 一目瞭然地看到,送出什麼要求、得到什麼回應, 而 JSON 也整理好,且上好了顏色。

小結

自從用了 Postman 很滿意以後,也陸續推薦給其他同事,現在整個公司都在用 Postman 了!
如果你的專案也常常在處理 request,而且苦無測試工具,你可以試試 Postman。









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開始