TensorFlow训练模型预测始终为零

时间:2017-12-03 20:00:04

标签: python-3.x tensorflow neural-network

我有一个简单的TensorFlow模型,其准确度为1.但是当我尝试预测一些新输入时,它总是返回零(0)。

import numpy as np
import tensorflow as tf

sess = tf.InteractiveSession()

# generate data

np.random.seed(10)

#inputs = np.random.uniform(low=1.2, high=1.5, size=[5000, 150]).astype('float32')

inputs = np.random.randint(low=50, high=500, size=[5000, 150])


label = np.random.uniform(low=1.3, high=1.4, size=[5000, 1])
# reverse_label = 1 - label
reverse_label = np.random.uniform(
    low=1.3, high=1.4, size=[5000, 1])
reverse_label1 = np.random.randint(
    low=80, high=140, size=[5000, 1])
#labels = np.append(label, reverse_label, 1)
#labels = np.append(labels, reverse_label1, 1)
labels = reverse_label1
print(inputs)
print(labels)
# parameters

learn_rate = 0.001
epochs = 100
n_input = 150
n_hidden = 15
n_output = 1

# set weights/biases

x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_output])


b0 = tf.Variable(tf.truncated_normal([n_hidden], stddev=0.2, seed=0))
b1 = tf.Variable(tf.truncated_normal([n_output], stddev=0.2, seed=0))

w0 = tf.Variable(tf.truncated_normal([n_input, n_hidden], stddev=0.2, seed=0))
w1 = tf.Variable(tf.truncated_normal([n_hidden, n_output], stddev=0.2, seed=0))


# step function


def returnPred(x, w0, w1, b0, b1):

    z1 = tf.add(tf.matmul(x, w0), b0)
    a2 = tf.nn.relu(z1)

    z2 = tf.add(tf.matmul(a2, w1), b1)
    h = tf.nn.relu(z2)

    return h  # return the first response vector from the


y_ = returnPred(x, w0, w1, b0, b1)  # predict operation

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
    logits=y_, labels=y))  # calculate loss between prediction and actual
model = tf.train.AdamOptimizer(learning_rate=learn_rate).minimize(
    loss)  # apply gradient descent based on loss


init = tf.global_variables_initializer()
tf.Session = sess
sess.run(init)  # initialize graph

for step in range(0, epochs):
    sess.run([model, loss], feed_dict={x: inputs, y: labels})  # train model



correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: inputs, y: labels}))  # print accuracy


inp = np.random.randint(low=50, high=500, size=[5, 150])


print(sess.run(tf.argmax(y_, 1), feed_dict={x: inp})) # predict some new inputs

所有功能都正常工作,我的问题是最新的代码行。我只试过" y _"相反" tf.argmax(y_,1)"但也没有奏效。 我该如何解决这个问题? 的问候,

1 个答案:

答案 0 :(得分:1)

您的代码中存在多个错误。

从这行代码开始:

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: inputs, y: labels}))  # print accuracy

您正在执行linear regression,但您正在检查logistic regression方法的准确性。如果要查看线性回归网络的执行情况,请打印loss。确保在每个培训期后您的损失都在减少。

如果您查看该准确度代码,请运行以下代码:

print(y_.get_shape())    # Outputs (?, 1)

只有一个输入,您的函数tf.argmax(y,1)tf.argmax(y_,1)都将始终返回[0,0,..]。因此,您的准确度将始终为1.0。删除这三行代码。

接下来,要获得输出,只需运行以下代码:

print(sess.run(y_, feed_dict={x: inp}))

但由于您的数据是随机的,因此不要指望良好的输出集。

相关问题