I am reading a csv file using tf.contrib.data.make_csv_dataset
to form a dataset, and then I use the command take()
to form another dataset with just one element, but still it returns all elments.
What is wrong here? I brought the code below:
import tensorflow as tf
import os
tf.enable_eager_execution()
# Constants
column_names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species']
class_names = ['Iris setosa', 'Iris versicolor', 'Iris virginica']
batch_size = 1
feature_names = column_names[:-1]
label_name = column_names[-1]
# to reorient data strucute
def pack_features_vector(features, labels):
"""Pack the features into a single array."""
features = tf.stack(list(features.values()), axis=1)
return features, labels
# Download the file
train_dataset_url = "http://download.tensorflow.org/data/iris_training.csv"
train_dataset_fp = tf.keras.utils.get_file(fname=os.path.basename(train_dataset_url),
origin=train_dataset_url)
# form the dataset
train_dataset = tf.contrib.data.make_csv_dataset(
train_dataset_fp,
batch_size,
column_names=column_names,
label_name=label_name,
num_epochs=1)
# perform the mapping
train_dataset = train_dataset.map(pack_features_vector)
# construct a databse with one element
train_dataset= train_dataset.take(1)
# inspect elements
for step in range(10):
features, labels = next(iter(train_dataset))
print(list(features))