04 Constant 常數

計算圖是排列成節點圖的一系列TensorFlow操作。我們來構建一個簡單的計算圖。每個節點將零個或多個張量作為輸入,並產生張量作為輸出。一種類型的節點是一個常量。像所有的TensorFlow常量一樣,它不需要輸入,而是輸出一個內部存儲的值。我們可以創建兩個浮點Tensors張量node1node2,如下所示:

範例程式01_constant.py

  1. import tensorflow as tf # 匯入tensorFlow 函示庫
  2. node1 = tf.constant3.0dtype = tf.float32# 設定變數node1 的資料為3.0
  3. node2 = tf.constant4.0# 系統內定為tf.float32的資料型態
  4. print(node1) # 印出該資料型態
  5. 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.04.0

而是產生3.04.0的節點,再透過session的方法,在TensorFlow運行時的控制和狀態。