0
0
TensorFlowml~10 mins

Dataset from tensors in TensorFlow - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a TensorFlow dataset from a tensor.

TensorFlow
import tensorflow as tf

data = tf.constant([1, 2, 3, 4, 5])
dataset = tf.data.Dataset.[1](data)

for item in dataset:
    print(item.numpy())
Drag options to blanks, or click blank then click option'
Afrom_tensor_slices
Bfrom_tensor
Cfrom_list
Dfrom_array
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like from_tensor or from_list.
Confusing from_tensor_slices with other dataset creation methods.
2fill in blank
medium

Complete the code to create a dataset from two tensors representing features and labels.

TensorFlow
import tensorflow as tf

features = tf.constant([[1, 2], [3, 4], [5, 6]])
labels = tf.constant([0, 1, 0])
dataset = tf.data.Dataset.[1]((features, labels))

for feature, label in dataset:
    print(feature.numpy(), label.numpy())
Drag options to blanks, or click blank then click option'
Afrom_list
Bfrom_array
Cfrom_tensors
Dfrom_tensor_slices
Attempts:
3 left
💡 Hint
Common Mistakes
Using from_tensors which creates a dataset with one element containing all data.
Using from_list which is not a TensorFlow dataset method.
3fill in blank
hard

Fix the error in the code to correctly create a dataset from a tensor.

TensorFlow
import tensorflow as tf

numbers = tf.constant([10, 20, 30])
dataset = tf.data.Dataset.[1](numbers)

for num in dataset:
    print(num.numpy())
Drag options to blanks, or click blank then click option'
Afrom_tensor
Bfrom_array
Cfrom_tensor_slices
Dfrom_list
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like from_tensor.
Confusing from_tensor_slices with other dataset creation methods.
4fill in blank
hard

Fill both blanks to create a dataset from features and labels and iterate over it.

TensorFlow
import tensorflow as tf

features = tf.constant([[7, 8], [9, 10]])
labels = tf.constant([1, 0])
dataset = tf.data.Dataset.[1]((features, labels))

for [2], label in dataset:
    print([2].numpy(), label.numpy())
Drag options to blanks, or click blank then click option'
Afrom_tensor_slices
Bfrom_tensors
Cfeature
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using from_tensors which creates a dataset with one element.
Using a loop variable name that does not match the print statement.
5fill in blank
hard

Fill all three blanks to create a dataset from tensors, shuffle it, and batch it.

TensorFlow
import tensorflow as tf

features = tf.constant([[1, 2], [3, 4], [5, 6], [7, 8]])
labels = tf.constant([0, 1, 0, 1])
dataset = tf.data.Dataset.[1]((features, labels))
dataset = dataset.[2](buffer_size=4)
dataset = dataset.[3](batch_size=2)

for batch_features, batch_labels in dataset:
    print(batch_features.numpy(), batch_labels.numpy())
Drag options to blanks, or click blank then click option'
Afrom_tensor_slices
Bshuffle
Cbatch
Drepeat
Attempts:
3 left
💡 Hint
Common Mistakes
Using repeat instead of shuffle or batch.
Not shuffling before batching.