2017-07-19 25 views
0

這從這篇文章(不是我)如下操作:TensorFlow for binary classificationtensorflow換onehot分類,成本始終是0

我也有類似問題,將我的數據使用一個熱碼。然而,我仍然得到0的成本。有趣的是,當我將訓練數據反饋給它時,準確度是正確的(90%)。下面

代碼:

# Set parameters 
learning_rate = 0.02 
training_iteration = 2 
batch_size = int(np.size(y_vals)/300) 
display_step = 1 
numOfFeatures = 20 # 784 if MNIST 
numOfClasses = 2 #10 if MNIST dataset 

# TF graph input 
x = tf.placeholder("float", [None, numOfFeatures]) 
y = tf.placeholder("float", [None, numOfClasses]) 

# Create a model 

# Set model weights to random numbers: https://www.tensorflow.org/api_docs/python/tf/random_normal 
W = tf.Variable(tf.random_normal(shape=[numOfFeatures,1])) # Weight vector 
b = tf.Variable(tf.random_normal(shape=[1,1]))    # Constant 

# Construct a linear model 
model = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax 

# Minimize error using cross entropy 
# Cross entropy 
cost_function = -tf.reduce_sum(y*tf.log(model)) 
# Gradient Descent 
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function) 

# Initializing the variables 
init = tf.global_variables_initializer() 

# Launch the graph 
with tf.Session() as sess: 
    sess.run(init) 

    # Training cycle 
    for iteration in range(training_iteration): 
     avg_cost = 0. 

     total_batch = int(len(x_vals)/batch_size) 
     # Loop over all batches 
     for i in range(total_batch): 

      batch_xs = x_vals[i*batch_size:(i*batch_size)+batch_size] 
      batch_ys = y_vals_onehot[i*batch_size:(i*batch_size)+batch_size] 

      # Fit training using batch data 
      sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys}) 

      # Compute average loss 
      avg_cost += sess.run(cost_function, feed_dict={x: batch_xs, y: batch_ys})/total_batch 

     # Display logs per eiteration step 
     if iteration % display_step == 0: 
      print ("Iteration:", '%04d' % (iteration + 1), "cost=", "{:.9f}".format(avg_cost)) 

    print ("Tuning completed!") 

    # Evaluation function 
    correct_prediction = tf.equal(tf.argmax(model, 1), tf.argmax(y, 1)) 
    #correct_prediction = tf.equal(model, y) 
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) 

    # Test the model 
    print ("Accuracy:", accuracy.eval({x: x_vals_test, y: y_vals_test_onehot})) 
+0

你是什麼cost_function,並且batch_size時total_batch? – Himaprasoon

+0

成本函數如上所示。 Batch_size是3123.總批次是300。 – user1761806

回答

0

您的成本輸出使用:

"{:.9f}".format(avg_cost)

因此,也許你可以用較大的編號替換9。

0

好的,這是我最終發現的。

替換:

b = tf.Variable(tf.random_normal(shape=[1,1])) 

有:

b = tf.Variable(tf.zeros([1]))