開始使用免費開始

將資料定義為常數

在整個課程中,我們會使用 tensorflow 2.6.0,並只匯入完成每個練習所需的子模組。多數情況會替你處理,不過在這個練習中,請自行從 tensorflow 匯入 constant

匯入 constant 之後,請把 numpy 陣列 credit_numpy 轉換成 tensorflow 的常數 credit_constant。這個陣列包含信用卡持有者資料集的特徵欄位,預覽如下面的圖片所示。我們會在後續章節再回到這個資料集。

請注意,tensorflow 2 允許你使用 numpy 陣列或 tensorflowconstant 物件作為資料。使用 constant 可以確保對該物件進行的任何運算都在 tensorflow 中執行。

這張圖片顯示信用卡違約資料集中的四個特徵欄位:education、marriage、age 與 bill amount。

本練習屬於課程

Python 的 TensorFlow 入門

檢視課程

練習說明

  • tensorflow 模組匯入子模組 constant
  • credit_numpy 陣列轉換為 tensorflow 中的 constant 物件。不要設定資料型別。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import constant from TensorFlow
from tensorflow import ____

# Convert the credit_numpy array into a tensorflow constant
credit_constant = constant(____)

# Print constant datatype
print('\n The datatype is:', credit_constant.dtype)

# Print constant shape
print('\n The shape is:', credit_constant.shape)
編輯並執行程式碼