P41 張量的定義以及資料
阿新 • • 發佈:2020-12-20
註解:
- 張量其實就是numpy裡面的陣列,是對numpy中陣列的封裝。
註解:
- "add:0"裡面的0沒有啥意義
- 如果再定義一個add加法,會顯示"add_1:0","add_2:0","add_3:0","add_4:0"......
- 張量的形狀是非常重要的。
import tensorflow as tf #實現一個加法運算 a=tf.constant(5.0) b=tf.constant(6.0) sum1=tf.add(a,b) #圖的定義,預設的這張圖,相當於是給程式分配一段記憶體 graph=tf.get_default_graph() #placeholder是一個佔位符,在程式執行時提供資料 plt=tf.placeholder(tf.float32,[None,3]) #也是一個op(操作、運算),只是佔位,沒有具體的資料,在sess.run()執行的時候提供資料 #[2,3]代表將填充一個2行3列的資料 #[None,3]代表訓練的時候,樣本數可能不固定 with tf.Session() as sess: print(a.graph) #列印張量所屬的預設圖 print("-----張量的形狀:-------") print(a.shape)#列印張量的形狀 print(plt.shape)#列印張量的形狀 print("-----張量的字串描述:-------") print(a.name)#列印張量的名字 print("-----張量的操作名:-------") print(a.op)#列印張量的操作名
執行結果:
<tensorflow.python.framework.ops.Graph object at 0x000000001258DA08> -----張量的形狀:------- () (?, 3) -----張量的字串描述:------- Const:0 -----張量的操作名:------- name: "Const" op: "Const" attr { key: "dtype" value { type: DT_FLOAT } } attr { key:"value" value { tensor { dtype: DT_FLOAT tensor_shape { } float_val: 5.0 } } } Process finished with exit code 0
註解:
- ()代表0維張量,就是一個數。
註解:
- 1維:5代表有5個數字
- 3維:(2,3,4)代表2張、3行、4列,一共有24個數據,每張表12個數據。