如何使前馈NN更准确?

时间:2018-04-15 17:37:31

标签: python tensorflow neural-network classification

我刚刚写完了我的第一个神经网络,它终于有效了,但它确实很糟糕。我的准确度大约为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}))   

1 个答案:

答案 0 :(得分:0)

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. 使用train_test_split中的sklearn.model_selection,它会在改组后拆分您的火车和测试数据。如果您的数据具有某种模式,那么不改变您的数据可能会产生问题,例如。你有99个数据点,前33个包含它的狗,另外33个包含它的猫,而最后33个它是一个鼠标,你的神经网络只训练66只狗和猫的图像,并且不会学会识别鼠标。

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

  3. 我猜张量流部分是正确的。