【问题标题】:tensorflow-for-onehot-classification , cost is always 0tensorflow-for-onehot-classification , cost 总是 0
【发布时间】:2017-07-19 07:36:07
【问题描述】:

这来自这篇文章(不是我的):TensorFlow for binary classification

我遇到了类似的问题,并将我的数据转换为使用一种热编码。但是,我的成本仍然为 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}))

【问题讨论】:

  • 你的 cost_function、batch_size 和 total_batch 是多少?
  • 成本函数如上所示。 Batch_size 为 3123。总批次为 300。

标签: python-3.x machine-learning tensorflow


【解决方案1】:

您的成本输出正在使用:

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

因此,也许您可​​以将 9 替换为更大的数字。

【讨论】:

    【解决方案2】:

    好的,这就是我最后找到的。

    替换:

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

    与:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-27
      • 2019-02-02
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      • 1970-01-01
      相关资源
      最近更新 更多