iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 2
2
Google Developers Machine Learning

Towards Tensorflow 2.0系列 第 2

[Day-2] Tensorflow 基本語法 - Part I

接下來幾天將會介紹TF的語法,並均會使用TF2.0來示範。我們會從基本的data type到變數類型,以及資料的處理。首先先從基本的data type開始說明

數據類型

​ 什麼是tensor,tensor在數學裡主要講的就是 “向量”,簡單來說就是一個n維度的data。主要支援資料格式就是一般的data type。包含: int , float , double , string , bool

所以我們要建立一個tf.tensor可以直接如下來建立tensor
https://ithelp.ithome.com.tw/upload/images/20190917/20119971JcE9bN1elB.png
圖:簡易的資料格式

變數類型

TF變數類型主要有三種,tf.constant、tf.Variable以及tf.placeholder。接下來會一一說明三類。

  1. tf.constant:
    顧名思義,他就是一個constant的類別,你可以放入各式的資料型態,做一些基礎運算,Ex: tf.add 等等。使用時機像是有時候我們會放accuracy或者一些運算完的模型資訊等等。因此,在創建一些tf.constant的可以簡單的用上面語法或者是你假如有一個numpy array也可以直接使用 tf.convert_to_tensor
#Create tf.constant
a = tf.constant([1,2,3])
#Convert np arry to tf
a = np.array([1,2,3])
b = tf.convert_to_tensor(a)
  1. tf.Variable
    簡單來說,tf.variable就是放置可學習的變數或者說將可求導的變數,例如: Neural Network的wieght或者bias。因此,在tf.variable中會紀錄他是learnable 或者在autograph中可被求導變數。之後,會實作簡單的一個NN會更清楚他的用途。
#Create tf.Variable
a = tf.ones(6)
b = tf.Variable(a,name='Data_point')
#Check tf.Variable
b.dtype #check data type
b.name #check name
b.trainable #check weather trainable

https://ithelp.ithome.com.tw/upload/images/20190917/20119971ZIKaXAsmGN.png
圖: tf.variable

  1. tf.placeholder

主要是TF在計算Gaph的方法。在使用時,我們是不會先給定一個初始值,而是給定長度或者資料格式先佔有一個空間。簡單來說就是,和Variable或者constant不一樣的是,不需給定初始值,但必須預先定義好資料類型與長寬深度。主要使用為在train模型,我們會放 x 與 y 的資料。在看TF1.X的程式會很常看到這個變數。在TF2.0中,已經不使用這個變數,蠻多都改成使用tf.data的api在導資料。而且在TF2.0中,在eager execution下,也不需要去預先定義這些資料空間。Source

簡單建立資料

有時候我們會想簡單建立有些都是0的資料,所以我們可以用一些簡單的TF語法來建立

#Example create zero
a = tf.zeros([6,6])
#or
b = tf.zeros_like(a)
#or fill -> shape and fill nums
c = tf.fill([6,6],0)

假如,今天我想要建立一個random matrix,此時我們可以使用下面各式不同的distribution來建立,其中,這樣的random方法我們常用於initial Deep learning中的weight或bias。因為,當你的weight不是random產生,而是一個齊一性的matrix,會讓您所學出來的權重都是一樣,網路會變成純粹的線性[註一]。因此,權重均必須random產生。

#Example random by normal distribution
a = tf.random.normal([6,6],mean=0,stddev=1)
#random by truncated normal distribution
b = tf.random.truncated_normal([6,6],mean=0,stddev=1)
#random by uniform
c = tf.random.uniform([6,6],minval=0,maxval=1)

選取資料

有時候,我們可能想將資料選取資料,看一下資料長什麼樣子。尤其是當我們想選出特定的row或者index該如何做呢?可以利用下面的語法

#Example for select instance
a = tf.ones([1,5,5,3])
a[0][0].shape
#Output shape:TensorShape([5, 3])
a[...,2].shape
#Output shape:TensorShape([1, 5, 5])

#use index select
tf.gather_nd(a,[0]).shape
#Output shape: TensorShape([5, 5, 3])

#select axis=1 , row 2 and 3
tf.gather(a,axis=1,indices=[2,3]).shape
#Output shape: TensorShape([1, 2, 5, 3])

小結:

這次是簡易的一些TF的語法,大家可以玩玩看,並且我下面連結我會放上是示範Colab[註2],可以複製出來自己玩玩。
若有任何問題大家可以討論交流,感謝您的閱讀。

梗圖:
https://ithelp.ithome.com.tw/upload/images/20190917/20119971uYKnYpBMKo.jpg

補充:

truncated normal distribution,若random數值在門檻值外(Ex: 2倍標準值外),就重新random,可以稍微的防止gradient vanshing or gradient exploding
Xavier initialization是有些tf layer default的initalization方法,這部分之後提到model的部分會在補充,他的initalization會牽涉到輸入及輸出,較為複雜。效果和普通的normal distribution有較好。

註一:為什麼網路參數不能設定為0

註二 TF_basic_Colab


上一篇
[Day-1] Tensorflow 介紹 及 Tensorflow 2.0相關知識
下一篇
[Day-3] Tensorflow 基本語法 - Part II
系列文
Towards Tensorflow 2.030
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言