iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 4
0
Google Developers Machine Learning

Towards Tensorflow 2.0系列 第 4

[Day-4] Tensorflow 基本語法 - Part III

今天會開始說明一下資料處理及數值運算。接下來就會開始進入基本ML及DL了。大夥撐著!把一些基本語法弄熟,之後會更容易上手,且更知道如何去修改一些tutorial,並運用於自己商務面上。不多說,開始今天的說明。

Sort

從字面來看就是排序,有時候我們會需要透過sort來排序feature並針對排序後的資料來畫圖或者來檢驗您對資料的假設。又或者我今天講把預測不好的後100筆資料拿出來驗證模型模型問題。都可以使用這樣的方法。這邊會介紹兩種方法,一種是純粹的sort,一種是sort後output是index,你就可以拿index來針對特定feature畫圖。

#Example for sort
data = tf.random.normal([10],mean=0,stddev=1)
tf.sort(data,direction='DESCENDING')
#or
tf.gather(data,tf.argsort(data,direction='ASCENDING'))

https://ithelp.ithome.com.tw/upload/images/20190919/20119971YWQoV88zMn.png

更簡單一點的話,你純粹只想拿top k 個就可以使用tf.math.top_k,他會同時return numer以及index

#Example top_k
top_data = tf.top_k(data,5)
#Get indies or values
top_data.indies
#or
top_data.values

https://ithelp.ithome.com.tw/upload/images/20190919/20119971JAEHxo9MfG.png

Padding

在做影像辨識的時候,有時候我們會做padding,而padding的作用是什麼呢?1. 針對差異圖片的大小做補齊,有時候可能輸入和filter不一致時,可以透過padding補齊。2. 有時候我們希望增加邊界訊息量,一般來說,沒做padding邊界只會被掃到一次,若做padding可以增加被掃到的次數,這邊簡單的說明padding及example,之後講CNN的時候會更詳細說明。

#Example padding
data = tf.random.normal([3,3],mean=0,stddev=1)
tf.pad(data,[[1,1],[1,1]])

https://ithelp.ithome.com.tw/upload/images/20190919/20119971BWYF8abbPD.png

Clipping

有時候我們在做Deep learning在計算gradient的時候,有時候會遇到gradient exploding。我們可預期當一個數字累乘 (1.01**100),你的gradient會變超大。所以,解決的方法簡單來說就是超過一個門檻值就固定,或者重新使用tf.clip_by_norm,這個方法就比較進階一點,他是針對整個data做縮放,有興趣的朋友可以參考這個連結

#Example clipping
tf.clip_by_value(data,0,1)
#range if number less or above fix at 0 or 1

基本數學運算

首先我們先從基本數學運算開始談起,這些運算您將會大量使用於DL or ML專案。雖然TF有很多已經寫好的實用function,但有時候可能還是無法達到一些客製化(老闆)需求,所以您可能還是需要自己寫function來運算一些指標或者修改loss function等等。接下來會討論一下基本運算。[ + - X % sqrt ...等等],此外,運算時也要注意用法。

  • Element wise: 直接對相對應的元素相加,Ex: +-
  • Matrix wise: 矩陣相乘,Ex: [6,32,4] @ [6,4,6] = [6,32,6]
  • Dimention wise: 針對特定dimention做運算。Ex: 對整個dimention做reduce_mean。
#Example for element wise
a = tf.fill([2,2],5)
b = tf.fill([2,2],6)
a+b,a*b

https://ithelp.ithome.com.tw/upload/images/20190919/20119971uBA7J4h7F8.png

#Example for Matrix wise
a = tf.fill([2,32,4],5)
b = tf.fill([2,4,6],6)
(a@b).shape # or tf.matmul(a,b)
#Output:TensorShape([2, 32, 6])

接下來就以Dimension wise的來做sample,為來在算一些loss,或者計算accuracy會常用

#Example for dimension wise
data = [[1,2,3],
      [1,2,3]]

tf_data = tf.cast(x,tf.float32)

mean_all = tf.reduce_mean(tf_data, keepdims=False)
mean_0 = tf.reduce_mean(tf_data, axis=0, keepdims=False)
mean_1 = tf.reduce_mean(tf_data, axis=1, keepdims=False)

mean_all,mean_0,mean_1

https://ithelp.ithome.com.tw/upload/images/20190919/201199711vxxQZiXis.png

可以清楚的看到他會去計算by你所設定的axis,針對特定axis計算mean。

小結:

感謝大家漫長的閱讀,若有問題都可以留言交流。這幾天將一些TF的語法複習過,接下來會說明一Python視覺化的東西,我想之後在跑一些資料處理或者跑模型,可能大家會試圖視覺化,來確認自己的假設或者看模型的performance(也可以使用tensorboard)。所以明天會來說明一下Python的視覺化套件。

感謝大家!

一日一梗圖:
https://ithelp.ithome.com.tw/upload/images/20190919/20119971WnR6TFqAy1.png


上一篇
[Day-3] Tensorflow 基本語法 - Part II
下一篇
[Day-5] Python視覺化實戰
系列文
Towards Tensorflow 2.030
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0

很棒的文章。

Dan iT邦新手 5 級 ‧ 2019-10-11 15:27:12 檢舉

感謝您!!!

0
we684123
iT邦研究生 5 級 ‧ 2019-11-29 15:53:53

hmmm... 感謝大大教學文
不過這邊想請問是文章少打還是我這邊有點問題呢?

照著文章做
https://ithelp.ithome.com.tw/upload/images/20191129/20112304iHn1GKAF78.png

我自己修改一下
https://ithelp.ithome.com.tw/upload/images/20191129/20112304FQv2a7nkzj.png

we684123 iT邦研究生 5 級 ‧ 2019-11-29 16:05:41 檢舉

還有一個 x 未被宣告
tf_data = tf.cast(x, tf.float32) # x -> data
https://ithelp.ithome.com.tw/upload/images/20191129/20112304mR8m2pK9ke.png

Dan iT邦新手 5 級 ‧ 2019-11-29 16:27:47 檢舉

感謝您!當時候太急可能未把較為完整的程式放上來,只取了片段。之後會在整理一下放上來。謝謝您~

我要留言

立即登入留言