如何提高LSTM和GRU循环神经网络的分类准确率?

5

Tensorflow中的二元分类问题:

我已经学习了在线教程,并试图使用门控循环单元(GRU)将其应用于实时问题。我已经尝试了我所知道的所有可能性来改进分类。

1)开始添加堆叠RNN(GRU)层 2)增加每个RNN层的隐藏单元 3)为隐藏层添加“sigmoid”和“RelU”激活函数 4)规范化输入数据 5)更改超参数

请查找数据集链接: https://github.com/madhurilalitha/Myfirstproject/blob/master/ApplicationLayerTrainingData1.txt

如果您可以浏览数据集,它具有标签“normal”和“other than normal”。我已将“normal”编码为'1 0',将异常编码为'0 1'。我还将数据集更改为以下形状的3D:

新训练X的形状(7995,5,40) 新训练Y的形状(7995,2) 新测试X的形状(1994,5,40) 新测试Y的形状(1994,2)

我不确定我在哪里缺少逻辑,有人能帮我找出代码中的错误吗?

测试数据的分类准确率为52.3%。它在训练数据上的准确性也相同。请查看以下代码:

#Hyper Parameters for the model
learning_rate = 0.001   
n_classes = 2    
display_step = 100    
input_features = train_X.shape[1] #No of selected features(columns)    
training_cycles = 1000    
time_steps = 5 # No of time-steps to backpropogate    
hidden_units = 50 #No of GRU units in a GRU Hidden Layer   

#Input Placeholders
with tf.name_scope('input'):
    x = tf.placeholder(tf.float64,shape = [None,time_steps,input_features], name 
= "x-input")    
    y = tf.placeholder(tf.float64, shape = [None,n_classes],name = "y-input")
#Weights and Biases    
with tf.name_scope("weights"):
    W = tf.Variable(tf.random_normal([hidden_units,n_classes]),name = "layer-
weights")    

with tf.name_scope("biases"):
    b = tf.Variable(tf.random_normal([n_classes]),name = "unit-biases")     


# Unstack to get a list of 'time_steps' tensors of shape (batch_size, 
input_features)
x_ = tf.unstack(x,time_steps,axis =1)    

#Defining a single GRU cell
gru_cell = tf.contrib.rnn.GRUCell(hidden_units)    

#GRU Output
with tf.variable_scope('MyGRUCel36'):   
    gruoutputs,grustates = 
tf.contrib.rnn.static_rnn(gru_cell,x_,dtype=tf.float64)    

#Linear Activation , using gru inner loop last output
output = tf.add(tf.matmul(gruoutputs[-1],tf.cast(W,tf.float64)),tf.cast(b,tf.float64))

#Defining the loss function
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits = output))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

#Training the Model
sess = tf.InteractiveSession()    
sess.run(tf.global_variables_initializer())    
for i in range (training_cycles):   
    _,c = sess.run([optimizer,cost], feed_dict = {x:newtrain_X, y:newtrain_Y})

    if (i) % display_step == 0:
        print ("Cost for the training cycle : ",i," : is : ",sess.run(cost, feed_dict ={x :newtrain_X,y:newtrain_Y}))
correct = tf.equal(tf.argmax(output, 1), tf.argmax(y,1))    
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))    
print('Accuracy on the overall test set is :',accuracy.eval({x:newtest_X, y:newtest_Y}))    
1个回答

6

听起来你朝着正确的方向在努力。我建议你尝试可视化你的训练数据,以确保它按预期逐渐减少。

你认为应该获得更高的准确率有什么原因吗?这可能是你使用这些数据能够达到的最佳效果。提高模型性能的最佳途径之一就是获取更多数据;是否有可能获取更多数据?

超参数优化是一个不错的方法;我建议你尝试使用不同的学习速率、不同数量和大小的隐藏层。


1
非常感谢fbt先生,您是正确的,准确率对于我使用的数据集是有效的。我在训练第一个模型(情况1)时,更进一步将样本洗牌。当我使用原始数据集(未洗牌的原始大小)时,该模型表现更好,具有高的有效准确率(情况2)。我确保不仅考虑“准确率”来衡量这两种情况下模型的性能。 - OJJ
如果你正在寻找其他指标,除了准确率之外,F1、召回率和精确度也是不错的选择;Kappa 是另一个强大的指标。 - finbarr
1
这是我的新账户。当我尝试给你的答案点赞时,它显示了消息:“投票已记录,但由于我的声望较低,不会公开显示”。 - OJJ
那是合法的。我认为你可以点击复选标记来接受答案,而不需要声望。 - finbarr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接