【问题标题】:How to make Feed Forward NN more accurate?如何让前馈神经网络更准确?
【发布时间】:2018-09-25 11:03:54
【问题描述】:

我刚刚完成了我的第一个神经网络,它终于可以工作了,但效果真的很糟糕。我得到大约 0.37 的准确度。有关如何使其更准确的任何提示?我已经尝试过不同的学习率和不同数量的隐藏层单元,但我的准确率从未超过 0.37。我正在尝试将数据分类为 3 类 0、1 或 2 之一。我使用 1 个热矩阵作为我的 Y。如何改进我的代码?

X = data[1:, 2:]
m, n = X.shape
labels = data[1:, 1]

Y = np.zeros((m,3))
i = 0
for label in labels:
    if label == 0:
        Y[i,0] = 1
    elif label == 1:
        Y[i,1] = 1
    elif label == 2:
        Y[i,2] = 1

    i += 1


 slice_size = math.floor(m/5)

 X_test = X[-slice_size:, :]
 Y_test = Y[-slice_size:]
 X_train = X[:slice_size, :]
 Y_train = Y[:slice_size]

learning_rate = 0.00001
num_steps = 200
batch_size = 100
display_step = 2

n_nodes_hl1 = 5
n_nodes_hl2 = 5
n_nodes_hl3 = 5

n_classes = 3
n_inputs = 16

training_epochs = 500

x = tf.placeholder('float32', [None,n])
y = tf.placeholder('float32', [None, n_classes])

weights = {
    'h1': tf.Variable(tf.random_normal([n_inputs, n_nodes_hl1])),
    'h2': tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
    'h3': tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
    'out': tf.Variable(tf.random_normal([n_nodes_hl1, n_classes]))
}
biases = {
    'b1': tf.Variable(tf.random_normal([n_nodes_hl1])),
    'b2': tf.Variable(tf.random_normal([n_nodes_hl2])),
    'b3': tf.Variable(tf.random_normal([n_nodes_hl3])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}


def neural_network(data):

    layer_1 = tf.add(tf.matmul(data, weights['h1']), biases['b1'])

    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])

    layer_3 = tf.add(tf.matmul(layer_2, weights['h3']), biases['b3'])

    output = tf.matmul(layer_3, weights['out']) + biases['out']

    return output



 logits = neural_network(x)
 prediction = tf.nn.softmax(logits)


 loss_op = 
 tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, 
 labels=y))

 optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
 train_op = optimizer.minimize(loss_op)

 correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y_train, 1))
 accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

 # Initialize the variables (i.e. assign their default value)
 init = tf.global_variables_initializer()

 with tf.Session() as sess:

    sess.run(init)
    for step in range(1, num_steps+1):
        x_step = np.asarray(X_train[step,:])
        y_step = np.asarray(Y_train[step])
        x_step = np.reshape(x_step, (1, n))
        y_step = np.reshape(y_step, (1,n_classes))

        sess.run(train_op, feed_dict={x:x_step , y:y_step})
        if step % display_step == 0 or step == 1:
             #Calculate batch loss and accuracy
             loss, acc = sess.run([loss_op, accuracy], feed_dict={x: x_step,
                                                             y: y_step})
             print("Step " + str(step) + ", Minibatch Loss= " +
                  "{:.4f}".format(loss) + ", Training Accuracy= " +
                  "{:.3f}".format(acc))
     x_step_test = np.asarray(X_test)
     y_step_test = np.asarray(Y_test)
     x_step_test = np.reshape(x_step, (1, n))
     y_step_test = np.reshape(y_step, (1,n_classes))
     print("Optimization Finished!")
     print("Testing Accuracy:",
          sess.run(accuracy, feed_dict={x: x_step_test,
                                    y: y_step_test}))   

【问题讨论】:

  • 神经网络并不神奇。也许 .37 是可以对您的数据进行的最佳处理。
  • @TerryJanReedy 那么您在我的代码中没有看到根本问题吗?因为这就是我最初的想法
  • 这是一个不同的问题。从slice_size...Y_train 的行有一个空格缩进,这是一个语法错误。它应该是 0(可能)或 4 个空格(不太可能)。我无法评论 TensorFlow 的使用。我曾使用过传统的分类方法,并且知道成功(必然)变化很大。

标签: python tensorflow neural-network classification


【解决方案1】:

1.

 x_step_test = np.asarray(X_test)
 y_step_test = np.asarray(Y_test)
 x_step_test = np.reshape(x_step, (1, n))
 y_step_test = np.reshape(y_step, (1,n_classes)) 

这不应该是:

 x_step_test = np.asarray(X_test)
 y_step_test = np.asarray(Y_test)
 x_step_test = np.reshape(x_step_test, (1, n))
 y_step_test = np.reshape(y_step_test, (1,n_classes)) 

还要检查你的批次,可能会有一些问题。

  1. 使用来自sklearn.model_selectiontrain_test_split,它会在洗牌后拆分您的训练和测试数据。如果您的数据具有某种模式,例如,不改组您的数据可能会产生问题。你有 99 个数据点,前 33 个包含它的狗,另外 33 个包含它的猫,最后 33 个包含一个老鼠,你的神经网络将只训练 66 个狗和猫的图像,并且不会学习识别老鼠。

  2. 提高学习率,AdamOptimizer 已经衰减 lr,使用 0.1 或 0.01 之类的值。

我猜 tensorflow 部分是正确的。

【讨论】:

    猜你喜欢
    • 2020-12-09
    • 2020-01-25
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 2016-11-29
    • 2020-02-07
    • 2019-04-12
    • 2021-02-05
    相关资源
    最近更新 更多