Tensorflow:将预取数据集转换为BatchDataset
Tensorflow:将预取数据集转换为BatchDataset 对于最新的Tensorflow版本2.3.1I,我尝试在以下位置遵循基本文本分类示例:。我使用csv文件,而不是像示例中那样从目录创建数据集:Tensorflow:将预取数据集转换为BatchDataset,dataset,tensorflow2.0,Dataset,Tensorflow2.0,Tensorflow:将预取数据集转换为BatchDataset 对于最新的Tensorflow版本2.3.1I,我尝试在以下位置遵循基本文本分类示例:。我使用csv文件,而不是像示例中那样从目录创建数据集: SELECT_COLUMNS = ['SentimentText','Sentiment'] LABEL_COLUMN = 'Sentiment' LABELS = [0, 1] def get_dataset(file_path, **kwargs): dataset = tf
SELECT_COLUMNS = ['SentimentText','Sentiment']
LABEL_COLUMN = 'Sentiment'
LABELS = [0, 1]
def get_dataset(file_path, **kwargs):
dataset = tf.data.experimental.make_csv_dataset(
file_path,
batch_size=3, # Artificially small to make examples easier to show.
label_name=LABEL_COLUMN,
na_value="?",
num_epochs=1,
ignore_errors=True,
**kwargs)
return dataset
all_data = get_dataset(data_path, select_columns=SELECT_COLUMNS)
因此,我得到:
type(all_data)
tensorflow.python.data.ops.dataset_ops.PrefetchDataset
示例使用以下命令从目录加载数据:
batch_size = 32
seed = 42
raw_train_ds = tf.keras.preprocessing.text_dataset_from_directory(
'aclImdb/train',
batch_size=batch_size,
validation_split=0.2,
subset='training',
seed=seed)
并获取另一种类型的数据集:
type(raw_train_ds)
tensorflow.python.data.ops.dataset_ops.BatchDataset
现在,当我尝试使用示例中的函数对数据进行标准化和矢量化时:
def custom_standardization(input_data):
lowercase = tf.strings.lower(input_data)
stripped_html = tf.strings.regex_replace(lowercase, '<br />', ' ')
return tf.strings.regex_replace(stripped_html,
'[%s]' % re.escape(string.punctuation),
'')
max_features = 10000
sequence_length = 250
vectorize_layer = TextVectorization(
standardize=custom_standardization,
max_tokens=max_features,
output_mode='int',
output_sequence_length=sequence_length)
def自定义_标准化(输入_数据):
小写=tf.strings.lower(输入_数据)
stripped_html=tf.strings.regex_replace(小写,
,“”)
返回tf.strings.regex\u replace(stripped\u html,
'[%s]'%re.escape(字符串标点符号),
'')
最大功能=10000
序列长度=250
矢量化\图层=文本矢量化(
标准化=自定义标准化,
最大令牌=最大特征,
输出模式为int,
输出(序列长度=序列长度)
并将它们应用到我的数据集我得到错误:
# Make a text-only dataset (without labels), then call adapt
train_text = all_data.map(lambda x, y: x)
vectorize_layer.adapt(train_text)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-20-1f1fc445912d> in <module>
1 # Make a text-only dataset (without labels), then call adapt
2 train_text = all_data.map(lambda x, y: x)
----> 3 vectorize_layer.adapt(train_text)
/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/layers/preprocessing/text_vectorization.py in adapt(self, data, reset_state)
378 shape = dataset_ops.get_legacy_output_shapes(data)
379 if not isinstance(shape, tensor_shape.TensorShape):
--> 380 raise ValueError("The dataset passed to 'adapt' must contain a single "
381 "tensor value.")
382 if shape.rank == 0:
ValueError: The dataset passed to 'adapt' must contain a single tensor value.
#创建一个纯文本数据集(无标签),然后调用adapt
列车文本=所有数据映射(λx,y:x)
矢量化图层。自适应(训练文本)
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在里面
1#制作一个纯文本数据集(无标签),然后调用adapt
2列文本=所有数据映射(λx,y:x)
---->3矢量化图层。自适应(训练文本)
/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/layers/preprocessing/text_vectorization.py in adapt(self、data、reset_state)
378 shape=数据集\操作。获取\遗留\输出\形状(数据)
379如果不存在(形状、张量_形状、张量形状):
-->380 raise VALUERROR(“传递给“自适应”的数据集必须包含一个”
381“张量值”。)
382如果shape.rank==0:
ValueError:传递给“adapt”的数据集必须包含单个张量值。
如何将预取数据集转换为BatchDataset