# Tensorflow ## import tensorflow as tf ## tf.add() 進行兩個變數的加減。 <span style = "color:red">tf.math.add(x, y, name=None)</span> ```python= x = [1,2,3,4,5] y = 2 print(tf.add(x,y))#output:tf.Tensor([3 4 5 6 7], shape=(5,), dtype=int32) ``` 上述程式可以看做下面這段程式: ```python= import tensorflow as tf x = tf.convert_to_tensor([1,2,3,4,5]) y = tf.convert_to_tensor(2) print(x+y) ``` 或者可以相加兩個一維張量: ```python= import tensorflow as tf x = tf.constant([1,2,3,4,5]) y = tf.constant([1,1,1,1,1]) tf.add(x,y)#output = <tf.Tensor: shape=(5,), dtype=int32, numpy=array([2, 3, 4, 5, 6], dtype=int32)> ``` ## convert_to_tensor() <span style = "color:red">tf.convert_to_tensor(value, dtype=None, dtype_hint=None, name=None)</span> 將一個變數轉成tensor的形式。 如果將一個tensor和一個non-tensor做運算,可能會發生不可預期的事。 ```python= def func(val): val = tf.convert_to_tensor(val) return val v1 = func(tf.constant([[1,2,3],[4,5,6]])) print(v1) ``` ## TF變數 常見的TF的變數主要有兩種,tf.constant、tf.Variable,後面會一個一個介紹。 ## tf.constant() <span style = "color:red">tf.constant(value, dtype=None, shape=None, name='Const')</span> 在constant()裡可以放個種資料型態例如: ```python= a = tf.constant([1,2,3,4,5]) b = tf.constant(5.5) c = tf.constant("string") print(a) print(b) print(c) ``` ## tf.Variable() tf.Variable()通常是放置可學習的變數,或者將可求導的變數,例如神經網路中的權重(weight)或偏值(bias), <span style = "color:red">tf.Variable( initial_value=None, trainable=None, validate_shape=True, caching_device=None, name=None, variable_def=None, dtype=None, import_scope=None, constraint=None, synchronization=tf.VariableSynchronization.AUTO, aggregation=tf.compat.v1.VariableAggregation.NONE, shape=None )</span> ```python= import tensorflow as tf a = tf.ones(3,dtype = tf.int64) b = tf.Variable(a,name = 'one') print(a) print(b) ``` 接著如果要輸出一個變數的數字,可以用下面這段code,要注意,下方的code有一個地方一定要加,否則會error。 ```python= import tensorflow.compat.v1 as tf tf.disable_v2_behavior() a = tf.Variable(5) b = tf.Variable(10) sess = tf.Session() sess.run(tf.global_variables_initializer()) print(sess.run(a)) print(sess.run(b)) sess.close() ``` 上述的code一定要加sess.run(tf.global_variables_initializer()),這是將variable初始化,所以在輸出前一定要加上這行。 接著下面那段程式碼是在進行輸出1~5。 ```python= import tensorflow.compat.v1 as tf import numpy as np tf.disable_v2_behavior() state = tf.Variable(0,name = 'counter') one = tf.constant(1) new_value = tf.add(state,one) update = tf.assign(state,new_value) init = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init) for i in range(5): sess.run(update) print(sess.run(state)) ``` 首先我們設一個變量叫state,接著設一個常量叫one,而我們將state+one等於new_value,接著要直接改變state,所以將new_value的值直接賦予給state,然後看到15行,我們將state一直更新,最後將它輸出,即可得到我們要的結果。 ## tf.Session() tensorflow是基於graph的架構,而Session是graph和執行者之間的媒介,由Session來啟動graph,而Session.run()是用來進行操作的,而且Session用完需要close()來釋放資源,而在之前相信各位有用過with as來開起的file,在這邊可以使用這個方式,使用這個方式可以不用輸入close()。 首先先看沒有Session()的時候宣告一個常量: ```python= import tensorflow.compat.v1 as tf tf.disable_v2_behavior() c = tf.constant(10) print(c)#output:Tensor("Const_11:0", shape=(), dtype=int32) ``` 可以看到雖然我們有賦值,但他依然顯示0,這時我們要使用Session來使他依照我們要求輸出: ```python= import tensorflow.compat.v1 as tf tf.disable_v2_behavior() c = tf.constant(10) sess = tf.Session() print(sess.run(c)) sess.close() ``` ```python= import tensorflow.compat.v1 as tf tf.disable_v2_behavior() c = tf.constant(10) with tf.Session() as sess: c = sess.run(c) print(c) ``` 上面兩種效果皆相同,輸出皆為10。 ## tf.matmul() 做矩陣乘法,用法: ```python= import tensorflow.compat.v1 as tf tf.disable_v2_behavior() m1 = tf.constant([[3,3]]) m2 = tf.constant([[2],[2]]) multi = tf.matmul(m1,m2) sess = tf.Session() output = sess.run(multi) print(output) sess.close() ``` ## tf.reduce_mean() 將張量tensor取平均,而我可以在第二個參數中加入一個數代表我要沿著哪個維度做平均。 ```python= import tensorflow.compat.v1 as tf tf.disable_v2_behavior() sess = tf.Session() a = tf.constant([[1,10,1],[2,2,2]]) b = tf.reduce_mean(a) c = tf.reduce_mean(a,1) print(sess.run(b))# output = 3 print(sess.run(c))# output = [4 2] sess.close() ``` ## tf.reduce_sum() 將張量tensor做加總,而我可以在第二個參數中加入一個數代表我要沿著哪個維度做加總。 ```python= import tensorflow.compat.v1 as tf tf.disable_v2_behavior() sess = tf.Session() a = tf.constant([[1,2,3],[4,5,6]]) all = tf.reduce_sum(a) second = tf.reduce_sum(a,1) print(sess.run(all)) print(sess.run(second)) sess.close() ``` ## tf.reshape() 將tensor的維度重新塑造,-1代表自動計算該維度的數量。 ```python import tensorflow.compat.v1 as tf tf.disable_v2_behavior() sess = tf.Session() a = tf.constant([1,2,3,4,5,6,7,8,9]) new_martix = tf.reshape(a,[3,-1]) print(sess.run(new_martix)) # output = # [[1 2 3] # [4 5 6] # [7 8 9]] sess.close() ``` ## tf.argmin() 從指定的維度進行尋找最小值的索引(index),索引由0開始。 ```python= import tensorflow.compat.v1 as tf tf.disable_v2_behavior() sess = tf.Session() a = tf.constant([[48,24,64],[32,90,6]]) index = tf.argmin(a,1) print(sess.run(index))# output [1 2] sess.close() ``` ## tf.Variable.eval() 顯示出tensor變數的值。 ```python= import tensorflow.compat.v1 as tf tf.disable_v2_behavior() a = tf.Variable([1,2]) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(a.eval()) sess.close() ``` ## tf.placeholder() placeholder是一種類似於Variable的變數,但placeholder可以不給初始值並在sess.run()的時候把值給予placeholder。 ```python= import tensorflow.compat.v1 as tf tf.disable_v2_behavior() x = tf.placeholder(tf.float32) y = tf.placeholder(tf.float32) output = tf.multiply(x,y) with tf.Session() as sess: print(sess.run(output,feed_dict = {x:[10.],y:[7.]}))#output = [70.] ``` 在給予placeholder值時要給一個字典(dict),key擺placeholder,value擺你要賦予的值。