0
0
TensorFlowml~10 mins

GPU vs CPU tensor placement in TensorFlow - Interactive Practice

Choose your learning style9 modes available
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.