【发布时间】:2021-01-03 16:03:53
【问题描述】:
我正在尝试对 TensorFlow 数据集中的单个列进行标记。如果只有一个特征列,我一直使用的方法效果很好,例如:
text = ["I played it a while but it was alright. The steam was a bit of trouble."
" The more they move these game to steam the more of a hard time I have"
" activating and playing a game. But in spite of that it was fun, I "
"liked it. Now I am looking forward to anno 2205 I really want to "
"play my way to the moon.",
"This game is a bit hard to get the hang of, but when you do it's great."]
target = [0, 1]
df = pd.DataFrame({"text": text,
"target": target})
training_dataset = (
tf.data.Dataset.from_tensor_slices((
tf.cast(df.text.values, tf.string),
tf.cast(df.target, tf.int32))))
tokenizer = tfds.features.text.Tokenizer()
lowercase = True
vocabulary = Counter()
for text, _ in training_dataset:
if lowercase:
text = tf.strings.lower(text)
tokens = tokenizer.tokenize(text.numpy())
vocabulary.update(tokens)
vocab_size = 5000
vocabulary, _ = zip(*vocabulary.most_common(vocab_size))
encoder = tfds.features.text.TokenTextEncoder(vocabulary,
lowercase=True,
tokenizer=tokenizer)
但是,当我尝试在有一组特征列的情况下执行此操作时,例如来自make_csv_dataset(每个特征列都被命名),上述方法失败了。 (ValueError: Attempt to convert a value (OrderedDict([]) to a Tensor.)。
我尝试使用以下方法在 for 循环中引用特定功能列:
text = ["I played it a while but it was alright. The steam was a bit of trouble."
" The more they move these game to steam the more of a hard time I have"
" activating and playing a game. But in spite of that it was fun, I "
"liked it. Now I am looking forward to anno 2205 I really want to "
"play my way to the moon.",
"This game is a bit hard to get the hang of, but when you do it's great."]
target = [0, 1]
gender = [1, 0]
age = [45, 35]
df = pd.DataFrame({"text": text,
"target": target,
"gender": gender,
"age": age})
df.to_csv('test.csv', index=False)
dataset = tf.data.experimental.make_csv_dataset(
'test.csv',
batch_size=2,
label_name='target')
tokenizer = tfds.features.text.Tokenizer()
lowercase = True
vocabulary = Counter()
for features, _ in dataset:
text = features['text']
if lowercase:
text = tf.strings.lower(text)
tokens = tokenizer.tokenize(text.numpy())
vocabulary.update(tokens)
vocab_size = 5000
vocabulary, _ = zip(*vocabulary.most_common(vocab_size))
encoder = tfds.features.text.TokenTextEncoder(vocabulary,
lowercase=True,
tokenizer=tokenizer)
我收到错误:Expected binary or unicode string, got array([])。引用单个特征列以便我可以标记化的正确方法是什么?通常,您可以在 .map 函数中使用 feature['column_name'] 方法引用特征列,例如:
def new_age_func(features, target):
age = features['age']
features['age'] = age/2
return features, targets
dataset = dataset.map(new_age_func)
for features, target in dataset.take(2):
print('Features: {}, Target {}'.format(features, target))
我尝试组合方法并通过映射函数生成词汇表。
tokenizer = tfds.features.text.Tokenizer()
lowercase = True
vocabulary = Counter()
def vocab_generator(features, target):
text = features['text']
if lowercase:
text = tf.strings.lower(text)
tokens = tokenizer.tokenize(text.numpy())
vocabulary.update(tokens)
dataset = dataset.map(vocab_generator)
但这会导致错误:
AttributeError: in user code:
<ipython-input-61-374e4c375b58>:10 vocab_generator *
tokens = tokenizer.tokenize(text.numpy())
AttributeError: 'Tensor' object has no attribute 'numpy'
将tokenizer.tokenize(text.numpy()) 更改为tokenizer.tokenize(text) 会引发另一个错误TypeError: Expected binary or unicode string, got <tf.Tensor 'StringLower:0' shape=(2,) dtype=string>
【问题讨论】:
-
您能否提供一个重现您的错误的示例?您可以将数据框转换为 CSV 并使用 make_csv_dataset 取回。
-
@NicolasGervais - 将第二个代码块更新为完全可重现的示例。
标签: python tensorflow nlp tensorflow2.0 tensorflow-datasets