我有一个来自Reddit的帖子数据集以及它们来自的subreddit,我正在尝试编写一个模型,该模型试图预测给定标题的帖子的subreddit。
这是我预处理数据的方式:
title_tokenizer = Tokenizer()
title_tokenizer.fit_on_texts(df.title)
max_sentence_len = len(df.title.max())
title_voc_size = len(title_tokenizer.word_index)
subreddit_tokenizer = Tokenizer()
subreddit_tokenizer.fit_on_texts(df.subreddit)
subreddits_size = len(subreddit_tokenizer.word_index)
def preprocess(title, subreddit):
title_sequence = title_tokenizer.texts_to_sequences([title.numpy().decode("utf-8")])
title_padded = pad_sequences(title_sequence, max_sentence_len)[0]
subreddit_sequence = subreddit_tokenizer.texts_to_sequences([subreddit.numpy().decode("utf-8")])
subreddit_categorical_value = keras.utils.to_categorical(subreddit_sequence[0][0], subreddits_size + 1)
return title_padded, subreddit_categorical_value
def set_shape_func(title, subreddit):
title.set_shape(max_sentence_len)
subreddit.set_shape(subreddits_size + 1)
return title, subreddit
dataset = tf.data.Dataset.from_tensor_slices((df.title, df.subreddit))
dataset = dataset.map(lambda x, y: tf.py_function(preprocess, [x, y], [tf.int64, tf.int64]))
dataset = dataset.map(set_shape_func)
dataset = dataset.shuffle(50)
dataset = dataset.repeat(None)
dataset = dataset.batch(128).prefetch(1)
这是模型及其训练的代码:
epochs = 500
steps_per_epoch = 20
lr = 0.015
inputs = keras.Input(max_sentence_len)
x = keras.layers.Embedding(title_voc_size+1, 100)(inputs)
x = keras.layers.Bidirectional(keras.layers.LSTM(256, return_sequences=True))(x)
x = keras.layers.LSTM(128)(x)
x = keras.layers.Dropout(1/3)(x)
x = keras.layers.Dense(256, activation='relu')(x)
x = keras.layers.Dense(256, activation='relu')(x)
outputs = keras.layers.Dense(subreddits_size + 1, activation='softmax')(x)
model = keras.Model(inputs=inputs, outputs=outputs)
model.compile(keras.optimizers.SGD(lr), 'categorical_crossentropy', metrics=['acc'])
history = model.fit(dataset, epochs=epochs, steps_per_epoch=steps_per_epoch)
loss, acc = model.evaluate(dataset, steps=epochs*steps_per_epoch)
model.save('models/{}-loss-{}-acc-{}-epochs-{}-steps-{}-lr-subreddit-prediction.model'.format(loss, acc, epochs, steps_per_epoch, lr))
由于某种原因,精度和损耗一直在波动,您可以在此图中看到它: (蓝色是损耗,橙色是精度)
我该怎么做才能使我的损失和准确性更加一致并获得更好的结果?