扑克手数据集在Tensor流量准确性非常差

问题描述 投票:0回答:1

我正在尝试为Poker Hand Dataset(10班)训练神经网络。我试图改变mnist exampe以适应这个。但是,对于我的程序,准确率总是大约50%,这太麻烦了。如何提高准确度?

def init_weights(shape):
    """ Weight initialization """
    weights = tf.random_normal(shape, stddev=0.1)
    return tf.Variable(weights)

def forwardprop(X, weights, biases):
    """
    Forward-propagation.
    IMPORTANT: yhat is not softmax since TensorFlow's softmax_cross_entropy_with_logits() does that internally.
    """
    h    = tf.nn.sigmoid(tf.add(tf.matmul(X, weights['w_1']),biases['b_1']))  # The \sigma function
    yhat = tf.add(tf.matmul(h, weights['w_2']),biases['b_2'])  # The \varphi function
    return yhat

def get_data(filename, targetname="target", idname="", test_size=0.10, random_state=200):
   #read data from csv
    df = pd.read_csv(filename)
    data = pd.DataFrame(df.ix[:, df.columns != targetname])

    if(idname != str("")):
        df = df.drop(idname, 1)

    data = pd.DataFrame(df.ix[:, df.columns != targetname])
    data = pd.get_dummies(data)
    all_X = data.as_matrix()

    target = df[targetname]
    target = pd.factorize(target)[0]
    # Convert target into one-hot vectors
    num_labels = len(np.unique(target))
    all_Y = np.eye(num_labels)[target]  # One liner trick!

    return train_test_split(all_X, all_Y, test_size=test_size, random_state=random_state)

def main():
    start_time = time.time()
    train_X, test_X, train_y, test_y = get_data(filename = './data/poker-train.csv', targetname = "class")

    #customized for this dataset (or any large dataset), must be chosen as per the data, need to find some generic way
    #for small datasets: batch size can be 1 (for more accuracy), 
    #for large ones: somewhr around 50-80, if taken 1 very slow,50-80 is a trade off of accuracy for time
    learning_rate = 0.01
    training_epochs = 100
    batch_size = 1

    # Layer's sizes
    x_size = train_X.shape[1]   # Number of input nodes
    h_size = train_X.shape[1]   # Number of hidden nodes
    y_size = train_y.shape[1]   # Number of outcomes 

    # Symbols
    X = tf.placeholder("float", shape=[None, x_size])
    y = tf.placeholder("float", shape=[None, y_size])

    # Weight initializations
    weights = {
        'w_1' : init_weights((x_size, h_size)),
        'w_2' : init_weights((h_size, y_size))
    }

    # Bias initializations
    biases = {
        'b_1': init_weights([h_size]),
        'b_2': init_weights([y_size])
    }

    # Forward propagation
    yhat    = forwardprop(X, weights, biases)
    predict = tf.argmax(yhat, axis=1)

    # Backward propagation
    cost    = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=yhat))
    updates = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

    # Run SGD
    sess = tf.Session()
    init = tf.global_variables_initializer()
    sess.run(init)

    total_batch = int(train_X.shape[0]/batch_size)
     # Launch the graph
    with tf.Session() as sess:
        sess.run(init)

        for epoch in range(training_epochs):
            beg_i=0
                # Loop over all batches
            for i in range(total_batch):
                end_i = beg_i + batch_size
                if(end_i > train_X.shape[0]):
                    end_i = train_X.shape[0]

                batch_x, batch_y = train_X[beg_i:end_i,:],train_y[beg_i:end_i,:]
                beg_i = beg_i + batch_size

                sess.run(updates, feed_dict={X: batch_x, y: batch_y})

            train_accuracy = np.mean(np.argmax(train_y, axis=1) == sess.run(predict, feed_dict={X: train_X, y: train_y}))
            test_accuracy  = np.mean(np.argmax(test_y, axis=1) == sess.run(predict, feed_dict={X: test_X, y: test_y}))

            print("Epoch = %d, train accuracy = %.2f%%, test accuracy = %.2f%%"
                  % (epoch + 1, 100. * train_accuracy, 100. * test_accuracy))

        # # Test model
        # correct_prediction = tf.equal(tf.argmax(predict, 1), tf.argmax(y, 1))
        # # Calculate accuracy
        # accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        # print( "Accuracy:", accuracy.eval({X: test_X, y: test_y}))

    print("Total time of execution: ",time.time()-start_time)

if __name__ == '__main__':
    main()

输出是

Epoch = 100,列车精度= 55.77%,测试精度= 55.30%

Epoch = 1,列车精度= 50.13%,测试精度= 50.20%

tensorflow deep-learning
1个回答
2
投票
batch_size = 50#1
training_epochs = int(train_X.shape[0]/batch_size)

# Layer's sizes
x_size = train_X.shape[1]   # Number of input nodes
h_size = 100#train_X.shape[1]   # Number of hidden nodes
y_size = train_y.shape[1]   # Number of outcomes 

我修改上面。 Epoch = 1,列车精度= 49.98%,测试精度= 50.11% Epoch = 500,列车精度= 90.90%,测试精度= 90.78%

© www.soinside.com 2019 - 2024. All rights reserved.