計算圖是排列成節點圖的一系列TensorFlow操作。我們來構建一個簡單的計算圖。每個節點將零個或多個張量作為輸入,並產生張量作為輸出。一種類型的節點是一個常量。像所有的TensorFlow常量一樣,它不需要輸入,而是輸出一個內部存儲的值。我們可以創建兩個浮點Tensors張量node1和node2,如下所示:
範例程式01_constant.py
- import tensorflow as tf # 匯入tensorFlow 函示庫
- node1 = tf.constant(3.0,dtype = tf.float32) # 設定變數node1 的資料為3.0
- node2 = tf.constant(4.0) # 系統內定為tf.float32的資料型態
- print(node1) # 印出該資料型態
- print(node1, node2) # 印出該資料型態
圖1 輸出結果
執行結果會印出。
Tensor(“Const:0”, shape=(), dtype=float32)
Tensor(“Const:0”, shape=(), dtype=float32) Tensor(“Const_1:0”, shape=(), dtype=float32)
TensorFlow在指定變數值時,不會像一般變數會直接印數3.0和4.0。
而是產生3.0和4.0的節點,再透過session的方法,在TensorFlow運行時的控制和狀態。