Get startedGet started for free

Defining data as constants

Throughout this course, we will use tensorflow version 2.6.0 and will exclusively import the submodules needed to complete each exercise. This will usually be done for you, but you will do it in this exercise by importing constant from tensorflow.

After you have imported constant, you will use it to transform a numpy array, credit_numpy, into a tensorflow constant, credit_constant. This array contains feature columns from a dataset on credit card holders and is previewed in the image below. We will return to this dataset in later chapters.

Note that tensorflow 2 allows you to use data as either a numpy array or a tensorflow constant object. Using a constant will ensure that any operations performed with that object are done in tensorflow.

This image shows four feature columns from a dataset on credit card default: education, marriage, age, and bill amount.

This exercise is part of the course

Introduction to TensorFlow in Python

View Course

Exercise instructions

  • Import the constant submodule from the tensorflow module.
  • Convert the credit_numpy array into a constant object in tensorflow. Do not set the data type.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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)
Edit and Run Code