python-3.x - TensorFlow 训练模型预测始终为零

标签 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)”但也没有用。 我该如何解决? 问候,

最佳答案

您的代码中有多个错误。

从这行代码开始:

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

您正在执行线性回归,但您正在使用逻辑回归 方法检查准确性。如果您想查看线性回归网络的性能,请打印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/

相关文章:

python - 从运算符中分离出来,同时保留运算符

tensorflow - 如何使用 TF2 在最新的 Tensorflow Object Detection API V2 中保存最佳模型?

Tensorflow,恢复特定设备中的变量

tensorflow - 我转换后的 tensorflow 迁移学习模型总是在 Tensorflow JS 中返回相同的结果

python-3.x - Scikit learn/python 中自然文本的有效分类

python - 为什么多重处理没有使用我所有的核心

python - 为什么我的正向先行断言会消耗字符串并且无法正确匹配?

python - 值错误 : at least one array or dtype is required

ruby - 简单神经网络无法学习 XOR