为什么Tensorflow网络预测每次运行的结果不同?

时间:2017-04-07 17:10:07

标签: python python-2.7 tensorflow neural-network classification

我有一个张量流网络,目前正在从14个数字的输入中预测值1-5。现在它设置为输入test_X = np.array([1,1,1,1,1,1,1,1,1,1,1,1,1,1])点播它,网络每次都返回不同的分类。我也不确定如何预测预测的置信度(我假设这应该是0到1之间的浮点值)。我的代码可以在下面找到:

    import tensorflow as tf
    import pandas as pd
    import numpy as np

    df = pd.read_csv('/Users/zach/desktop/export.csv')
    data_ = df.drop(['ID','Species'], axis=1)


    n_classes = data_["Phylum"].nunique()

    dim = 14
    learning_rate = 0.0001
    display_step = 10
    n_hidden_1 = 2000
    n_hidden_2 = 1500
    n_hidden_3 = 1000
    n_hidden_4 = 500

    X = tf.placeholder(tf.float32, [None, dim])

    train_set = data_.sample(frac=0.7)
    test_set = data_.loc[~data_.index.isin(train_set.index)]

    train_size = train_set.size

    inputY_test = pd.get_dummies(test_set['Phylum'])
    inputY_train = pd.get_dummies(train_set['Phylum'])

    train_X = train_set.iloc[:train_size, 5:].as_matrix()
    train_X = pd.DataFrame(data=train_X)
    train_X = train_X.fillna(value=0).as_matrix()

    train_Y = inputY_train.as_matrix()
    train_Y = pd.DataFrame(data=train_Y)
    train_Y = train_Y.fillna(value=0).as_matrix()

    #test_X = test_set.iloc[:, 5:].as_matrix()
    #test_X = pd.DataFrame(data=test_X)
    #test_X = test_X.fillna(value=0).as_matrix()
    test_X = np.array([1,1,1,1,1,1,1,1,1,1,1,1,1,1])
    test_X = pd.DataFrame(data=test_X)
    test_X = test_X.fillna(value=0).as_matrix()
    test_X.resize(1,14)


    test_Y = inputY_test.as_matrix()
    test_Y = pd.DataFrame(data=test_Y)
    test_Y = test_Y.fillna(value=0).as_matrix()

    n_samples = train_Y.size
    total_len = train_X.shape[0]
    n_input = train_X.shape[1]
    batch_size = 5


    W = tf.Variable(tf.zeros([dim, n_classes]))
    b = tf.Variable(tf.zeros([n_classes]))


    def multilayer_perceptron(x, weights, biases):
        # Hidden layer with RELU activation
        layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
        layer_1 = tf.nn.relu(layer_1)

        # Hidden layer with RELU activation
        layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
        layer_2 = tf.nn.relu(layer_2)

        # Hidden layer with RELU activation
        layer_3 = tf.add(tf.matmul(layer_2, weights['h3']), biases['b3'])
        layer_3 = tf.nn.relu(layer_3)

        # Hidden layer with RELU activation
        layer_4 = tf.add(tf.matmul(layer_3, weights['h4']), biases['b4'])
        layer_4 = tf.nn.relu(layer_4)

        # Output layer with linear activation
        out_layer = tf.matmul(layer_4, weights['out']) + biases['out']
        return out_layer

    # Store layers weight & bias
    weights = {
        'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1], 0, 0.1)),
        'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], 0, 0.1)),
        'h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_3], 0, 0.1)),
        'h4': tf.Variable(tf.random_normal([n_hidden_3, n_hidden_4], 0, 0.1)),
        'out': tf.Variable(tf.random_normal([n_hidden_4, n_classes], 0, 0.1))
    }
    biases = {
        'b1': tf.Variable(tf.random_normal([n_hidden_1], 0, 0.1)),
        'b2': tf.Variable(tf.random_normal([n_hidden_2], 0, 0.1)),
        'b3': tf.Variable(tf.random_normal([n_hidden_3], 0, 0.1)),
        'b4': tf.Variable(tf.random_normal([n_hidden_4], 0, 0.1)),
        'out': tf.Variable(tf.random_normal([n_classes], 0, 0.1))
    }

    # Construct model
    pred = multilayer_perceptron(X, weights, biases)
    pred1 = tf.argmax(pred,1)

    y = tf.placeholder(tf.float32, [None, n_classes])
    #cost = -tf.reduce_sum(y*tf.log(tf.clip_by_value(pred,1e-10,1.0)))
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))

    optimizer =  tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
    hm_epochs = 20

    tf.set_random_seed(1234)
    init = tf.initialize_all_variables()
    with tf.Session() as sess:
        sess.run(init) 

        for epoch in range(hm_epochs):
            avg_cost = 0
            total_batch = int(total_len/batch_size)
            for i in range(total_batch-1):
                batch_x = train_X[i*batch_size:(i+1)*batch_size]
                batch_y = train_Y[i*batch_size:(i+1)*batch_size]

                _, c, p = sess.run([optimizer, cost, pred], feed_dict={X: batch_x,
                                                                               y: batch_y})
                avg_cost += c / total_batch

            label_value = batch_y
            estimate = p
            err = label_value-estimate

            if epoch % display_step == 0:
                print "Epoch:", '%04d' % (epoch+1), "cost=", \
                    "{:.9f}".format(avg_cost)
                print "[*]----------------------------------------------------"
                for i in xrange(3):
                    print "label value:", label_value[i], \
                            "estimated value:", estimate[i]
                print "======================================================="

        print "Optimization Finished!"

        #Test model
        #correct_prediction = tf.equal(tf.argmax(pred, 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}))


        feed_dict = {X: test_X}
        classification = pred.eval(feed_dict)
        print "Network Prediction:", classification
        print "Classification: ", np.argmax(classification) + 1

网络返回类似这样的内容(使用"网络预测""分类"值与每次运行显着不同)

Epoch: 0001 cost= 18.784451194
[*]----------------------------------------------------
label value: [1 0 0 0 0] estimated value: [  7.22390413  14.61596966  -0.08044712  41.44412231  -2.90992975]
label value: [1 0 0 0 0] estimated value: [-18.6686306   51.33540726  65.4961853   67.72460938  -3.51442194]
label value: [0 0 0 1 0] estimated value: [-32.95540619  22.96526909 -18.13385201  70.66561127 -28.7767086 ]
=======================================================
Epoch: 0011 cost= 0.798838628
[*]----------------------------------------------------
label value: [1 0 0 0 0] estimated value: [ 11.22842598  -3.67433786 -14.22806835   1.45541549  -7.12817001]
label value: [1 0 0 0 0] estimated value: [ 24.90147591  16.51822853  23.89280701  24.66276169  21.37914276]
label value: [0 0 0 1 0] estimated value: [  0.13968639   7.37386036 -21.62825203  21.48152542 -23.18427277]
=======================================================
Optimization Finished!
Network Prediction: [[ 3.85959864  0.32653514  0.26948914 -0.70163095 -2.03481865]]
Classification:  1

如何稳定网络的预测,以便每次输入保持不变时预测相同的结果?另外,我如何显示这种预测的信心?

0 个答案:

没有答案