Tensor Flow softmax回归始终预测1

时间:2015-12-16 17:56:31

标签: python numpy tensorflow softmax

我有以下基于MNIST示例的代码。它有两种修改方式:

1)我没有使用单热矢量,所以我只使用tf.equal(y, y_)

2)我的结果是二进制:0或1

import tensorflow as tf
import numpy as np

# get the data
train_data, train_results = get_data(2000, 2014)
test_data, test_results = get_data(2014, 2015)

# setup a session
sess = tf.Session()

x_len = len(train_data[0])
y_len = len(train_results[0])

# make placeholders for inputs and outputs
x = tf.placeholder(tf.float32, shape=[None, x_len])
y_ = tf.placeholder(tf.float32, shape=[None, y_len])

# create the weights and bias
W = tf.Variable(tf.zeros([x_len, 1]))
b = tf.Variable(tf.zeros([1]))

# initialize everything
sess.run(tf.initialize_all_variables())

# create the "equation" for y in terms of x
y_prime = tf.matmul(x, W) + b
y = tf.nn.softmax(y_prime)

# construct the error function
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(y_prime, y_)

# setup the training algorithm
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

# train the thing
for i in range(1000):
    rand_rows = np.random.choice(train_data.shape[0], 100, replace=False)
    _, w_out, b_out, ce_out = sess.run([train_step, W, b, cross_entropy], feed_dict={x: train_data[rand_rows, :], y_: train_results[rand_rows, :]})

    print("%d: %s %s %s" % (i, str(w_out), str(b_out), str(ce_out)))

# compute how many times it was correct
correct_prediction = tf.equal(y, y_)

# find the accuracy of the predictions
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

print(sess.run(accuracy, feed_dict={x: test_data, y_: test_results}))

for i in range(0, len(test_data)):
    res = sess.run(y, {x: [test_data[i]]})

    print("RES: " + str(res) + " ACT: " + str(test_results[i]))

准确度始终为0.5(因为我的测试数据与0一样多1)。 Wb的值似乎总是增加,可能是因为cross_entropy的值始终是全零的向量。

当我尝试使用此模型进行预测时,预测始终为1:

RES: [[ 1.]] ACT: [ 0.]
RES: [[ 1.]] ACT: [ 1.]
RES: [[ 1.]] ACT: [ 0.]
RES: [[ 1.]] ACT: [ 1.]
RES: [[ 1.]] ACT: [ 0.]
RES: [[ 1.]] ACT: [ 1.]
RES: [[ 1.]] ACT: [ 0.]
RES: [[ 1.]] ACT: [ 0.]
RES: [[ 1.]] ACT: [ 1.]
RES: [[ 1.]] ACT: [ 0.]
RES: [[ 1.]] ACT: [ 1.]

我在这里做错了什么?

1 个答案:

答案 0 :(得分:4)

您似乎在预测单个标量,而不是向量。 softmax op为每个示例生成矢量值预测。此向量必须总和为1.当向量只包含一个元素时,该元素必须始终为1.如果要对此问题使用softmax,则可以使用[1,0]作为当前的输出目标使用[0]并使用[0,1],当前正在使用[1]。另一种选择是你可以只使用一个数字,但是将输出层更改为sigmoid而不是softmax,并将成本函数更改为基于sigmoid的成本函数。