我正在尝试为Keras中的二进制分类任务训练一个简单的lstm模型,该模型带有一个我创建的句子(1000对)的小数据集。问题是我获得了恒定的精度(0.3),val_accuracy和损失(0)。我是否以错误的方式处理数据?还是模型设置中有问题?
# Model variables
n_hidden = 10
gradient_clipping_norm = 1.25
batch_size = 32
n_epoch = 25
# The visible layer
left_input1 = Input(shape=(max_seq_length1,), dtype='int32')
right_input1 = Input(shape=(max_seq_length1,), dtype='int32')
embedding_layer = Embedding(len(embeddings1), embedding_dim, weights=[embeddings1], input_length=max_seq_length1, trainable=False)
# Embedded version of the inputs
encoded_left1 = embedding_layer(left_input1)
encoded_right1 = embedding_layer(right_input1)
shared_lstm = CuDNNLSTM(n_hidden)
left_output1 = shared_lstm(encoded_left1)
right_output1 = shared_lstm(encoded_right1)
# Calculates the distance as defined by the MaLSTM model
malstm_distance1 = Lambda(function=lambda x: exponent_neg_manhattan_distance(x[0], x[1]),output_shape=lambda x: (x[0][0], 1))([left_output1, right_output1])
# Pack it all up into a model
malstm1 = Model([left_input1, right_input1], [malstm_distance1])
malstm1.compile(loss='binary_crossentropy', optimizer=Adam(), metrics=['categorical_accuracy'])
malstm = Model( inputs=malstm1.input, outputs=malstm1.output)
malstm.compile(loss='categorical_hinge',optimizer=Adam(lr=0.0001), metrics=['sparse_categorical_accuracy'])
# Start training
training_start_time = time()
malstm_trained = malstm.fit([ X_train1['left'], X_train1['right'] ], Y_train, batch_size=128, nb_epoch=50,
validation_data=([X_validation1['left'], X_validation1['right'] ], Y_validation))