python - Why does Tensorflow network predict different results each run? -
i have tensorflow network predicting value 1-5 input of 14 numbers. right set input test_x = np.array([1,1,1,1,1,1,1,1,1,1,1,1,1,1]) , dispite seeding it, network returns different classifications every time. unsure of how predict confidence of prediction (i'm assuming should float value in between 0 , 1). code can found below:
import tensorflow tf import pandas pd import numpy 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 relu activation layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1']) layer_1 = tf.nn.relu(layer_1) # hidden layer relu activation layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']) layer_2 = tf.nn.relu(layer_2) # hidden layer relu activation layer_3 = tf.add(tf.matmul(layer_2, weights['h3']), biases['b3']) layer_3 = tf.nn.relu(layer_3) # hidden layer relu activation layer_4 = tf.add(tf.matmul(layer_3, weights['h4']), biases['b4']) layer_4 = tf.nn.relu(layer_4) # output layer 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() tf.session() sess: sess.run(init) epoch in range(hm_epochs): avg_cost = 0 total_batch = int(total_len/batch_size) 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 "[*]----------------------------------------------------" 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 the network returns (with "network prediction" , "classification" values differing each run)
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 how can stabilized predictions of network predicts same outcome each time when input remains constant? also, how display confidence of prediction?
Comments
Post a Comment