首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tensorflow-for- cost cost分类,成本始终为0

tensorflow-for- cost cost分类,成本始终为0
EN

Stack Overflow用户
提问于 2017-07-19 15:36:07
回答 2查看 251关注 0票数 0

这是从这篇文章(不是我的)开始的:TensorFlow for binary classification

我遇到了类似的问题,并将我的数据转换为使用一种热编码。然而,我得到的成本仍然是0。有趣的是,当我将训练数据反馈给它时,准确率是正确的(90%)。

代码如下:

代码语言:javascript
复制
# 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}))
EN

回答 2

Stack Overflow用户

发布于 2017-07-19 16:39:05

您对成本的输出是使用:

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

因此,也许你可以用更大的数字来代替9。

票数 0
EN

Stack Overflow用户

发布于 2017-07-19 17:26:40

好了,这是我在最后发现的。

替换:

代码语言:javascript
复制
b = tf.Variable(tf.random_normal(shape=[1,1]))

通过以下方式:

代码语言:javascript
复制
b = tf.Variable(tf.zeros([1]))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45183879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档