Complete the code to create a TensorFlow dataset from a tensor.
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())
The correct method to create a dataset from a tensor is from_tensor_slices. It slices the tensor along the first dimension to create elements.
Complete the code to create a dataset from two tensors representing features and labels.
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())
from_tensor_slices creates a dataset by slicing each tensor element-wise, pairing features and labels correctly.
Fix the error in the code to correctly create a dataset from a tensor.
import tensorflow as tf numbers = tf.constant([10, 20, 30]) dataset = tf.data.Dataset.[1](numbers) for num in dataset: print(num.numpy())
The method from_tensor_slices correctly creates a dataset by slicing the tensor into individual elements.
Fill both blanks to create a dataset from features and labels and iterate over it.
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())
Use from_tensor_slices to create the dataset. The loop variable for features is named feature to match the print statement.
Fill all three blanks to create a dataset from tensors, shuffle it, and batch it.
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())
First, create the dataset with from_tensor_slices. Then shuffle it with shuffle and group elements with batch.