如何为tensorflow创建正确的文本文件?

问题描述 投票:0回答:2

Tensorflow 无法找到从数据帧创建的文本文件。下面的代码给了我错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-d5b632e1e805> in <module>
      2 seed = 123
      3 
----> 4 train_ds = tf.keras.preprocessing.text_dataset_from_directory(directory='data_tf/train', batch_size=batch_size, validation_split=0.2,subset='training', seed=seed,label_mode='binary',labels=[0,1])
      5 
      6 val_ds = tf.keras.preprocessing.text_dataset_from_directory(directory='data_tf/train', batch_size=batch_size, validation_split=0.2,subset='validation', seed=seed,labels=[0,1],label_mode='binary')

~/my_repo//venv/lib64/python3.7/site-packages/tensorflow/python/keras/preprocessing/text_dataset.py in text_dataset_from_directory(directory, labels, label_mode, class_names, batch_size, max_length, shuffle, seed, validation_split, subset, follow_links)
    155       file_paths, labels, validation_split, subset)
    156   if not file_paths:
--> 157     raise ValueError('No text files found.')
    158 
    159   dataset = paths_and_labels_to_dataset(

ValueError: No text files found.

但也会在崩溃之前显示此内容

Found 2 files belonging to 2 classes. Using 2 files for training.

train_ds = tf.keras.preprocessing.text_dataset_from_directory(directory='data_tf/train', batch_size=batch_size, validation_split=0.2,subset='training', seed=seed,label_mode='binary',labels=[0,1])`

文件夹结构正确,如下:
data_tf > 训练 > 0 > neg.txt / data_tf > 训练 > 1 > pos.txt

我使用

to_csv()
函数创建了文本文件:
pos_df.to_csv(r'data_tf/train/1/pos.txt', header=None, index=None, sep=' ', mode='a')

我应该使用另一种方法创建文本文件以便张量流接受它吗?谢谢。

python tensorflow text-processing
2个回答
0
投票

每个目录中至少需要两个文本文件,否则无法工作。

enter image description here


0
投票

对于tensorflow-rocm 2.15,当有两个文件夹:train 和 test,每个文件夹至少有 3 个文件时,它对我有用。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.