【发布时间】:2019-11-20 08:44:42
【问题描述】:
问题
我从一个流行的网站中抓取了大量的 cmets,我正在尝试使用 TensorFlow 制作一个文本生成器来构建 LSTM 架构。一般来说,这很简单,但考虑到数据集的大小,我遇到了在一次性编码标签时出现内存问题,即使使用 GCP 的 AI 平台也是如此——而且我不太喜欢用大量内存来完成一项昂贵的工作。
我可以通过限制来解决:
- 语料库长度
- 词汇量
但我正在努力让它变得非常好,而不是满足于更少。否则我可以只使用马尔可夫链。
可能的解决方案
我不应该将完整的数据集加载到内存中,这是常识。 我正在寻找与 ImageDataGenerator 等效的工具,但用于文本。我可以将 One-Hot 编码器安装在完整的数据集上,但要逐批转换数据。我对 TensorFlow 的了解太有限,无法正确完成这项工作。
有没有办法让我以稀疏的方式在内存中撕掉 OHEd 标签?
我遇到了一些遇到相同问题的人,但找不到解决方案。有人可以帮帮我吗?
提前谢谢你!
这是我的代码。
代码
这是我的 task.py 中的相关部分,我在其中进行标记化和 one-hot 编码。
# tokenize corpus
if hparams.trim_corpus != 0:
corpus = corpus[:hparams.trim_corpus]
tokenizer = Tokenizer(num_words = hparams.n_words_to_keep, oov_token = '<OOV>')
tokenizer.fit_on_texts(corpus)
# create input sequences using list of tokens
input_sequences = []
for line in corpus:
token_list = tokenizer.texts_to_sequences([line])[0]
for i in range(1, len(token_list)):
n_gram_sequence = token_list[:i+1]
input_sequences.append(n_gram_sequence)
# pad sequences
input_sequences = np.array(pad_sequences(input_sequences, maxlen=model.MAX_SEQUENCE_LENGTH, padding='pre'))
# Create predictors and label
predictors, labels = input_sequences[:,:-1],input_sequences[:,-1]
# Filter most common words
most_common_items = [item[0] for item in Counter(labels).most_common(hparams.vocab_size)]
indices_to_keep = [True if b in most_common_items else False for b in labels]
predictors = predictors[indices_to_keep]
labels = labels[indices_to_keep]
这是事情变糟的部分。当我将 OHE 转换应用于训练数据集时,我在本地和 GCP 中都会出现内存错误。
# Train-test split + encoding
X_train, X_test, y_train, y_test = train_test_split(predictors, labels, test_size = 0.2)
enc = OneHotEncoder(handle_unknown='ignore')
enc.fit(y_train.reshape(-1,1))
y_train = enc.transform(y_train.reshape(-1,1)).toarray()
y_test = enc.transform(y_test.reshape(-1,1)).toarray()
exporter = tf.estimator.LatestExporter('exporter', model.serving_input_fn) # What is this?
train_input_fn = lambda: model.input_fn(X_train, y_train, hparams.batch_size, mode = tf.estimator.ModeKeys.TRAIN)
eval_input_fn = lambda: model.input_fn(X_test, y_test, hparams.batch_size, mode = tf.estimator.ModeKeys.EVAL)
train_steps = hparams.num_epochs * len(y_train) / hparams.batch_size
train_spec = tf.estimator.TrainSpec(train_input_fn, max_steps = train_steps)
eval_spec = tf.estimator.EvalSpec(eval_input_fn, steps = None, start_delay_secs=10, throttle_secs=10, exporters=exporter)
# Generate Configuration.
run_config = tf.estimator.RunConfig(save_checkpoints_steps=500)
# Create estimator
estimator = model.keras_estimator(model_dir=hparams.job_dir, config=run_config, learning_rate=hparams.learning_rate, vocab_size=hparams.vocab_size, n_words_to_keep = hparams.n_words_to_keep)
# Start training
tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
最后,这是我的model.py。
def input_fn(features, labels, batch_size, mode):
inputs = (features, labels)
# Convert the inputs to a Dataset.
dataset = tf.data.Dataset.from_tensor_slices(inputs)
if mode == tf.estimator.ModeKeys.TRAIN:
dataset = dataset.shuffle(1000).repeat().batch(batch_size)
if mode in (tf.estimator.ModeKeys.EVAL, tf.estimator.ModeKeys.PREDICT):
dataset = dataset.batch(batch_size)
return dataset.make_one_shot_iterator().get_next()
【问题讨论】:
标签: tensorflow keras nlp lstm one-hot-encoding