【发布时间】:2020-08-18 13:08:21
【问题描述】:
如何创建一个 DataSet 对象,其中包含一组用于使用 tensorflow 进行文本处理的单词?
假设我有一个这样的单词列表
words = [ ['This', 'is', 'the', 'first'],
[ 'and', 'another']
]
所以每个训练/测试样本的项目数量是可变的。 (实际上我是从数据库中获取文本,并使用 Spacy 提取相关单词)
我正在处理来自 tensorflow.org 的 word embeddings tutorial,它使用具有这些属性的 IMDB 数据集,但想切换到使用我拥有的数据。
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import tensorflow_datasets as tfds
tfds.disable_progress_bar()
(train_data, test_data), info = tfds.load(
'imdb_reviews/subwords8k',
split = (tfds.Split.TRAIN, tfds.Split.TEST),
with_info=True, as_supervised=True)
#train_data = ??? How do I make it from my own set of words/sentences
encoder = info.features['text'].encoder
train_batches = train_data.shuffle(1000).padded_batch(10)
test_batches = test_data.shuffle(1000).padded_batch(10)
embedding_dim=16
model = keras.Sequential([
layers.Embedding(encoder.vocab_size, embedding_dim),
layers.GlobalAveragePooling1D(),
layers.Dense(16, activation='relu'),
layers.Dense(1)
])
model.summary()
model.compile(optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(
train_batches,
epochs=10,
validation_data=test_batches, validation_steps=20)
【问题讨论】:
-
您打算对我的回答提供反馈吗?让我知道我是否可以改进它
-
@NicolasGervais 在回到这个话题之前,我一直在忙其他事情——请稍等。
标签: python tensorflow keras nlp tensorflow2.0