Bird
Raised Fist0
TensorFlowml~10 mins

GPU vs CPU tensor placement in TensorFlow - Interactive Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a tensor on the GPU device.

TensorFlow
import tensorflow as tf
with tf.device('[1]'):
    a = tf.constant([1.0, 2.0, 3.0])
Drag options to blanks, or click blank then click option'
A/CPU:0
B/TPU:0
C/GPU:1
D/GPU:0
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/CPU:0' places the tensor on CPU, not GPU.
Using '/GPU:1' when only one GPU is available causes an error.
2fill in blank
medium

Complete the code to check if a tensor is placed on GPU.

TensorFlow
import tensorflow as tf
x = tf.constant([1, 2, 3])
print(x.device.endswith('[1]'))
Drag options to blanks, or click blank then click option'
AGPU:0
BTPU:0
CGPU:1
DCPU:0
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 'CPU:0' when the tensor is on GPU.
Using 'GPU:1' when the tensor is on 'GPU:0'.
3fill in blank
hard

Fix the error in the code to place a tensor on CPU explicitly.

TensorFlow
import tensorflow as tf
with tf.device('[1]'):
    b = tf.constant([4, 5, 6])
Drag options to blanks, or click blank then click option'
A/CPU:0
B/TPU:0
C/GPU:0
D/GPU:1
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/GPU:0' places the tensor on GPU, not CPU.
Using '/TPU:0' causes an error if TPU is not available.
4fill in blank
hard

Fill both blanks to create a tensor on GPU and then move it to CPU.

TensorFlow
import tensorflow as tf
with tf.device('[1]'):
    c = tf.constant([7, 8, 9])
with tf.device('[2]'):
    d = tf.identity(c)
Drag options to blanks, or click blank then click option'
A/GPU:0
B/CPU:0
C/GPU:1
D/TPU:0
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/CPU:0' for the first tensor places it on CPU, not GPU.
Using '/GPU:1' when only one GPU is available causes errors.
5fill in blank
hard

Fill all three blanks to create a tensor on CPU, then move it to GPU, and finally check its device.

TensorFlow
import tensorflow as tf
with tf.device('[1]'):
    e = tf.constant([10, 11, 12])
with tf.device('[2]'):
    f = tf.identity(e)
print(f.device.endswith('[3]'))
Drag options to blanks, or click blank then click option'
A/CPU:0
B/GPU:0
CGPU:0
DCPU:0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'CPU:0' instead of '/CPU:0' in device context causes errors.
Checking device with '/GPU:0' instead of 'GPU:0' in string endswith.

Practice

(1/5)
1. What is the main reason to use tf.device() in TensorFlow when working with GPUs and CPUs?
easy
A. To change the data type of a tensor
B. To save the model to disk
C. To initialize variables automatically
D. To specify whether a tensor or operation runs on CPU or GPU

Solution

  1. Step 1: Understand the purpose of tf.device()

    This function is used to tell TensorFlow where to place tensors or operations, either on CPU or GPU.
  2. Step 2: Compare options with the function's purpose

    Changing data types, initializing variables, or saving models are unrelated to device placement.
  3. Final Answer:

    To specify whether a tensor or operation runs on CPU or GPU -> Option D
  4. Quick Check:

    tf.device() controls device placement = B [OK]
Hint: tf.device() sets CPU or GPU for tensors [OK]
Common Mistakes:
  • Confusing device placement with data type changes
  • Thinking tf.device() initializes variables
  • Assuming tf.device() saves models
2. Which of the following is the correct syntax to place a tensor on GPU device 0 in TensorFlow?
easy
A. with tf.device('/GPU:0'): x = tf.constant([1, 2, 3])
B. with tf.device('device:GPU0'): x = tf.constant([1, 2, 3])
C. with tf.device('GPU0'): x = tf.constant([1, 2, 3])
D. with tf.device('/CPU:0'): x = tf.constant([1, 2, 3])

Solution

  1. Step 1: Recall TensorFlow device naming conventions

    TensorFlow uses '/GPU:0' to refer to the first GPU device.
  2. Step 2: Check each option's device string

    The correct format for GPU device 0 is with tf.device('/GPU:0'): x = tf.constant([1, 2, 3]). Formats like '/CPU:0', 'device:GPU0', and 'GPU0' are incorrect.
  3. Final Answer:

    with tf.device('/GPU:0'): x = tf.constant([1, 2, 3]) -> Option A
  4. Quick Check:

    Correct GPU device string = D [OK]
Hint: Use '/GPU:0' to specify first GPU device [OK]
Common Mistakes:
  • Using 'GPU0' without slash and colon
  • Confusing CPU and GPU device strings
  • Missing the 'with' context for tf.device
3. What will be the output device placement of the tensor x in the following code if a GPU is available?
with tf.device('/CPU:0'):
    x = tf.constant([1, 2, 3])
print(x.device)
medium
A. It will show a GPU device string like '/job:localhost/replica:0/task:0/device:GPU:0'
B. It will show a CPU device string like '/job:localhost/replica:0/task:0/device:CPU:0'
C. It will raise an error because GPU is available
D. It will show an empty string

Solution

  1. Step 1: Analyze the device context used

    The code uses with tf.device('/CPU:0'), so the tensor x is forced to be on CPU.
  2. Step 2: Understand device string output

    Printing x.device will show the full device string indicating CPU, regardless of GPU availability.
  3. Final Answer:

    It will show a CPU device string like '/job:localhost/replica:0/task:0/device:CPU:0' -> Option B
  4. Quick Check:

    Device context forces CPU = C [OK]
Hint: Device context overrides default device placement [OK]
Common Mistakes:
  • Assuming GPU is used automatically if available
  • Expecting error when CPU is forced
  • Thinking device string can be empty
4. Identify the error in this TensorFlow code snippet that tries to place a tensor on GPU:
with tf.device('/GPU:1'):
    x = tf.constant([4, 5, 6])
print(x.device)
Assuming the system has only one GPU device.
medium
A. Syntax error in tf.device string
B. No error, code runs fine on GPU 1
C. Error because GPU device '/GPU:1' does not exist
D. TensorFlow automatically switches to CPU without error

Solution

  1. Step 1: Check available GPU devices

    The system has only one GPU, which is '/GPU:0'. Trying to use '/GPU:1' refers to a non-existent second GPU.
  2. Step 2: Understand TensorFlow behavior on invalid device

    TensorFlow raises an error if the specified device does not exist.
  3. Final Answer:

    Error because GPU device '/GPU:1' does not exist -> Option C
  4. Quick Check:

    Invalid GPU index causes error = A [OK]
Hint: Check GPU count before using device index [OK]
Common Mistakes:
  • Assuming GPU indices start at 1
  • Expecting automatic fallback to CPU
  • Ignoring device existence errors
5. You want to speed up a large matrix multiplication in TensorFlow using GPU if available, but fall back to CPU if no GPU exists. Which code snippet correctly implements this logic?
hard
A. if tf.config.list_physical_devices('GPU'): with tf.device('/GPU:0'): result = tf.matmul(a, b) else: with tf.device('/CPU:0'): result = tf.matmul(a, b)
B. with tf.device('/GPU:0'): result = tf.matmul(a, b)
C. result = tf.matmul(a, b) # TensorFlow auto-chooses device
D. with tf.device('/CPU:0'): result = tf.matmul(a, b)

Solution

  1. Step 1: Check for GPU availability

    Use tf.config.list_physical_devices('GPU') to detect if GPU exists.
  2. Step 2: Use conditional device placement

    If GPU exists, place operation on '/GPU:0', else place on '/CPU:0' to ensure fallback.
  3. Step 3: Verify other options

    Forcing GPU without checking availability risks errors if no GPU. Auto-placement lacks explicit conditional control. Forcing CPU ignores available GPU.
  4. Final Answer:

    if tf.config.list_physical_devices('GPU'): with tf.device('/GPU:0'): result = tf.matmul(a, b) else: with tf.device('/CPU:0'): result = tf.matmul(a, b) -> Option A
  5. Quick Check:

    Conditional device placement with fallback = A [OK]
Hint: Check GPU presence before device placement [OK]
Common Mistakes:
  • Not handling fallback when GPU missing
  • Assuming TensorFlow always picks GPU
  • Forcing CPU even if GPU is available