我正在使用 Keras 实现多层感知器来预测句子中正确的单词顺序。我正在使用train_on_batch()
,因为我转换了树中的每个句子,然后对每个局部子树进行排序:当每个子树都被排序时,整个树甚至都被排序。
我在训练过程中注意到一个奇怪的事情:从第一个 epoch 开始,损失值为 0.0。我的数据集形状是(453732, 300)
(最初的特征数量是 838,但我使用 PCA 来减少它们),这是代码:
mlp = keras.models.Sequential()
# add input layer
mlp.add(
keras.layers.Dense(
units=training_dataset.shape[1],
input_shape = (training_dataset.shape[1], ),
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
activation='tanh')
)
# add hidden layer
mlp.add(
keras.layers.Dense(
units=training_dataset.shape[1] + 10,
input_shape = (training_dataset.shape[1] + 10,),
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
activation='relu')
)
mlp.add(
keras.layers.Dropout(
0.2,
input_shape=(training_dataset.shape[1] + 10,))
)
# add output layer
mlp.add(
keras.layers.Dense(
units=1,
input_shape = (1, ),
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
activation='softmax')
)
# define SGD optimizer
sgd_optimizer = keras.optimizers.SGD(
lr=0.01, decay=0.01, momentum=0.9, nesterov=True
)
# compile model
mlp.compile(
optimizer=sgd_optimizer,
loss=listnet_loss
)
mlp.summary() # print model settings
losses = np.array([])
# Training
with tf.device('/GPU:0'):
for epoch in range(0, 10):
print('Epoch {0} started!'.format(epoch))
start_range = 0
for group in groups_id_count:
end_range = (start_range + group[1]) # Batch is a group of words with same group id
batch_dataset = training_dataset[start_range:end_range, :]
batch_labels = training_dataset_labels[start_range:end_range]
batch_train_result = mlp.train_on_batch(batch_dataset, batch_labels)
losses = np.append(losses, batch_train_result)
start_range = end_range
print('Epoch {0} loss: {1}'.format(epoch, np.mean(losses)))
listnet_loss
如下:
def get_top_one_probability(vector):
return (K.exp(vector) / K.sum(K.exp(vector)))
def listnet_loss(real_labels, predicted_labels):
return -K.sum(get_top_one_probability(real_labels)) * tf.math.log(get_top_one_probability(predicted_labels))
groups_id_count
是形式为 的元组列表(#subtree_number,#number_of_words_in_subtree)
,其中#subtree_number
是子树的标识符,是子树#number_of_words_in_subtree
中的单词数。然后,我的批次是动态的:批次由子树中的单词数组成。这就是我过去train_on_batch()
训练模型的原因。
有什么建议么?提前致谢。