我有一个简单的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)”,但是也没有用。
我该如何解决?
问候,

最佳答案

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

从以下代码行开始:

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}))

但是由于您的数据是随机的,所以不要期望有好的输出。

关于python-3.x - TensorFlow训练模型预测始终为零,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47622680/

10-12 21:38