首页 > 解决方案 > 为什么我的网络需要这么多 epoch 才能提供合理的准确性?

问题描述

我有一个包含几层的简单网络,我试图减少层(和 num_outputs)以加快模型速度,但准确性也降低了,我的问题是如何加快收敛速度​​并同时提高准确性。实现中是否有问题导致网络太慢

learning_rate=0.99
n_x=50
n_y=10
lossList=[]
g_tb = tf.Graph()
#
with g_tb.as_default():
    x = tf.placeholder(tf.float32, [None, n_x], name="x")#Glove Vectors
    y = tf.placeholder(tf.float32, [None, n_y], name="y")#scores
    with tf.name_scope('Neural_Nt'):
        fully_connected1 = tf.contrib.layers.fully_connected(inputs=x, num_outputs=500, 
                                                         activation_fn=tf.nn.relu,scope="Fully_Conn1")
        fully_connected2 = tf.contrib.layers.fully_connected(inputs=fully_connected1, num_outputs=400, 
                                                             activation_fn=tf.nn.relu,scope="Fully_Conn2")
        fully_connected3 = tf.contrib.layers.fully_connected(inputs=fully_connected2, num_outputs=300, 
                                                             activation_fn=tf.nn.relu,scope="Fully_Conn3")
        fully_connected4 = tf.contrib.layers.fully_connected(inputs=fully_connected2, num_outputs=50, 
                                                             activation_fn=tf.nn.relu,scope="Fully_Conn4")
        fully_connected5 = tf.contrib.layers.fully_connected(inputs=fully_connected2, num_outputs=10, 
                                                             activation_fn=tf.nn.relu,scope="Fully_Conn5")
        prediction = tf.contrib.layers.fully_connected(inputs=fully_connected3, num_outputs=10, 
                                               activation_fn=tf.nn.softmax,scope="Out")
    with tf.name_scope('Cost'):
        cost = tf.losses.softmax_cross_entropy(onehot_labels=y, logits=prediction,scope="Cost_Function")
    with tf.name_scope('Accuracy'):
        correct_prediction = tf.equal(tf.argmax(prediction, 1, name="Argmax_Pred"), tf.argmax(y, 1, name="Y_Pred"), 
                                      name="Correct_Pred")
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32, name="Cast_Corr_Pred"), name="Accuracy")
 #       
    with tf.name_scope('Optimization'):
        optimizer = tf.train.AdagradOptimizer(learning_rate, name="Optimizer").minimize(cost)

    init = tf.global_variables_initializer()
  #  


# Start session
with tf.Session(graph=g_tb) as sess:
#
    lossList=[]

    sess.run(init)
    #Save the graph in the summary
    #summary_writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph())
    training_epochs = 2000#4000
    for epoch in range(training_epochs):
        _, c ,prediction1,acc= sess.run([optimizer, cost,prediction,accuracy], feed_dict={x: np.array(gloveVectors),
                                                     y: np.array(scores)})
    # For every epoch save cost and accuracy
        print(epoch, c)
        lossList.append(c)
    print(acc)

标签: tensorflowneural-network

解决方案


在这里写下我的 5 美分,因为我无法发表评论。所以对于初学者来说,你有一个非常高的学习率learning_rate=0.99。如果你选择一个高学习率,你的模型可能会学得更快,但代价是权重集不理想或准确性差。您可以在此处阅读有关学习率的更多信息

提高学习速度的另一种可能性是增加批量大小。但是请注意,如果您选择非常高的批量大小,权重更新的数量会减少,因此您可能还会得到一个很差的准确度分数。

最后,您可以在正常图层之间尝试Dropout Layer 。这可以降低学习速度,提高准确性并防止过度拟合。您可以在此处阅读有关 Dropout的更多信息

据我所知,您的实现本身没有任何问题,但要确保您可以共享用于再次检查的数据。

我希望这有帮助:)


推荐阅读