1、知識點
""" tensorflow前端系統:定義程序的圖結構,主要是利用一些API實現 tensorflow后端系統:運算圖結構 numpy的reshape,在原始數據做修改,並沒有創建新的數據對象 1、安裝:按照官方文檔安裝 a)安裝python,pip b)升級 python -m pip install --upgrade pip c)win10安裝CPU版本tensorflow ,pip install https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.12.0-cp36-cp36m-win_amd64.whl 2、報警告1:FutureWarning: Conversion of the second argument of issubdtype from 解決方法:pip install h5py==2.8.0rc1 報警告2:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 解決方法: import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 3、tensorflow結構 橢圓代表張量,簡稱tensor:tensorflow中所有的數據都封裝在tensor中 方塊,簡稱operation(op):專門運算的操作節點,所有的操作都是op 圖(graph):整個程序的結構 會話(Session):運算程序的圖 4、a)圖:運行程序的結構 圖默認已經注冊,一組表示 tf.Operation計算單位的對象和tf.Tensor。表示操作之間流動的數據單元的對象 獲取調用: tf.get_default_graph() op、sess或者tensor 的graph屬性 圖的作用:主要給程序分配內存 圖的創建: 1、創建默認的圖:graph = tf.get_default_graph() 2、自定義圖: g = tf.Graph() b)op:只要使用tensorflow的api定義的函數都是OP c)張量(tensor):就指代的是數據,op是tensor的載體 d)會話(Session):相當於橋梁,將圖和計算資源連接起來 會話作用: 1、運行圖的結構 2、分配資源計算 3、掌握資源(變量的資源,隊列,線程) 注意:一個會話只能運行一個圖結構 ,但可以在Session中指定圖去運算 查看會話的資源情況:tf.Session(config=tf.ConfigProto(log_device_placement=True)) 啟動會話:sess.run() 關閉會話:sess.close() 啟動會話(sess.run()): 1、run()中的參數必須是tensor或者op類型 2、如果變量想轉換為tensor類型,可以和tensor做運算,提升數據類型 3、實時的提供數據進行訓練,feed_dict與placeholder結合使用 4、placeholder是一個占位符 Session一般返回異常值: 1、RuntimeError:如果它Session處於無效狀態(例如已關閉)。 2、TypeError:如果fetches或feed_dict鍵是不合適的類型。 3、ValueError:如果fetches或feed_dict鍵無效或引用 Tensor不存在。 5、張量(tensor):是tensorflow基本的數據格式。類似於numpy 張量的階和數據類型: 包含:name,shape,dype 張量的屬性:graph,op,name,shape 張量形狀表示: 0維:() 1維:(5) 2維:(5,6) 3維:(2,3,4) 張量形狀:包含動態形狀和靜態形狀 1、動態形狀:一種描述張量在執行過程中的一種形狀 tf.reshape:創建一個具有不同形狀的新張量 2、靜態形狀:創建一個張量,初始狀態的形狀 tf.Tensor.get_shape:獲取靜態形狀 tf.Tensor.set_shape():更新tensor對象的靜態形狀 注意:1、靜態形狀固定了,就不能再次設置形狀,只能使用動態形狀,但會創建一個新的張量 2、動態形狀改變,數據數量一定要匹配 6、運算API:正太分布主要通過平均值和標准差進行表示 1、生成張量:tf.zeros() 、tf.ones() 、tf.constant() 2、隨機值張量: 正太分布(高斯分布)隨機值:tf.random_normal() 截斷的正太分布(高斯分布)隨機值:tf.truncated_normal() 3、張量數據類型變換: tf.cast(x,dtype) 萬能轉換類型 tf.squeeze() 4、拼接:tf.concat(values,axis) 7、變量:tf.Variable(),是一種OP,是一種特殊的張量,能夠進行存儲持久化,他的值就是張量,默認被訓練 1、變量必須都初始化,且要在session中運行 tf.global_variables_initializer() 8、視圖(tensorboard):首先將程序圖結構序列化成一個event文件,然后通過tensorboard讀取這個event文件 1、tf.summary.FileWriter("",graph=) SCALARS:顯示0維讀值,如准確率,損失值 GRAPHS:顯示程序圖圖結構 HISTOGRAPHS:高維的值,如權重,偏置 2、報錯:No dashboards are active for the current data set 原因:因為沒有讀取到event文件,所以檢查運行語句中logdir是否正確: tensorboard --logdir="./tmp/" """
2、代碼
# coding = utf-8 import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' #變量op def vari(): a = tf.constant([1,2,3]) var = tf.Variable(tf.random_normal([2,3],mean=0.0,stddev=1.0)) print(a,var) init_op = tf.global_variables_initializer() #變量初始化 with tf.Session() as sess: sess.run(init_op) #運行初始化變量 file = tf.summary.FileWriter("./tmp", graph=sess.graph) print(sess.run([a, var])) #圖的創建 def createGraph(): g = tf.Graph() #with表示設定上下文環境 with g.as_default(): c = tf.constant(11.0) print(c.graph) def addTest(): a = tf.constant(5.0) b = tf.constant(6.0) s = tf.add(a,b) # print(s) graph = tf.get_default_graph() #主要是分配內存 # print(graph) plt = tf.placeholder(tf.float32,[None,3]) #None表示樣本數不固定 with tf.Session() as sess: print(sess.run(plt,feed_dict={plt:[[1,2,3],[4,5,6]]})) #print(sess.run(s)) print(s.eval()) #使用eval()取值 #打印的地址一致 print(a.graph) print(s.graph) print(sess.graph) def shape(): plt = tf.placeholder(tf.float32,[None,2]) plt.set_shape([3,2]) #靜態修改 if __name__ == '__main__': vari()