Tensorflow输出准确度1,但所有结果都相同(和错误)

时间:2018-04-27 02:44:52

标签: python tensorflow

我正在尝试构建Tensorflow神经网络,但无法使其正常工作。它总是为每次观察输出相同的值。试图改变激活功能,改变学习速度,重塑张量和阵列,我在这里遗漏了一些东西。

数据集完全是数字化的。

import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.preprocessing import Normalizer
from sklearn.model_selection import train_test_split

df = pd.read_csv("data.csv")
X = np.array(df[["area","bathrooms", "sq_price"]])
y = df[["price"]]
y = np.array(y).reshape([47, 1])

normalizer = Normalizer()
X = normalizer.fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
X_test = np.array(X_test, dtype='float32')
y_test = np.array(y_test, dtype="float32")
X_test.shape

n_inputs = len(X_train[0])
n_hidden = 5
n_outputs = 1
lr = 0.001
epochs = 5000
batch_size = 1

xph = tf.placeholder(tf.float32, [None, n_inputs])
yph = tf.placeholder(tf.float32, [None, n_outputs])

W1 = tf.Variable(tf.truncated_normal([n_inputs, n_hidden], stddev=0.1))
W2 = tf.Variable(tf.truncated_normal([n_hidden, n_outputs], stddev=0.1))
b1 = tf.Variable(tf.ones([n_hidden]))
b2 = tf.Variable(tf.ones([n_outputs]))

def feed_forward(X, W1, W2, b1, b2):
    l1 = tf.tanh(tf.add(tf.matmul(X, W1), b1))
    l2 = tf.add(tf.matmul(l1, W2),b2)
    return l2

output = feed_forward(xph, W1, W2, b1, b2)

error = tf.reduce_mean((output - yph)**2)
optimizer = tf.train.GradientDescentOptimizer(lr).minimize(error)

init = tf.global_variables_initializer()
correct_prediction = tf.equal(tf.argmax(output,1), tf.argmax(yph,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

with tf.Session() as sess:
    sess.run(init)
    print("Weights 1 before training:\n ", sess.run([W1]), "\n")
    for i in range(epochs):
        rand_ind = np.random.randint(len(X_train), size = batch_size)
        loss = sess.run([optimizer], feed_dict={xph: X_train[rand_ind], yph: y_train[rand_ind]})
        if i == (epochs-1):
            print("Done!\n")

    print("Weights 1 after training: \n", sess.run([W1]), "\n")
    print("Accuracy: ", sess.run(accuracy, feed_dict={xph:X_test, yph:y_test}), "\n")
    print("Results for testing:\n ", sess.run(feed_forward(X_test, W1, W2, b1, b2), feed_dict={xph: X_test}), "\n")
    print("Expected values:\n ", y_test)

代码的输出:

Weights 1 before training:
[array([[ 0.02620826, -0.11837681,  0.01349821,  0.04195584,  0.14087772],
       [-0.10512593,  0.1383599 , -0.0632275 ,  0.07759375, -0.09907298],
       [-0.14932911,  0.13720822,  0.15072195, -0.09748196,  0.08388615]],
      dtype=float32)]

Done!

Weights 1 after training:
 [array([[ 1.0387952e+01,  1.7232073e+01,  2.9152514e+01, -1.8471668e+01,
         2.2215986e+01],
       [-8.4001064e-02,  1.7373289e-01, -3.8207369e-03,  3.9849356e-02,
        -5.4067656e-02],
       [ 5.5025685e-01,  1.3086454e+00,  2.1180787e+00, -1.3474523e+00,
         1.5743145e+00]], dtype=float32)]

Accuracy:  1.0

Results for testing:
  [[340043.88]
 [340043.88]
 [340043.88]
 [340043.88]
 [340043.88]
 [340043.88]
 [340043.88]
 [340043.88]
 [340043.88]
 [340043.88]]

Expected values:
  [[349900.]
 [369000.]
 [573900.]
 [252900.]
 [299900.]
 [329900.]
 [449900.]
 [285900.]
 [212000.]
 [229900.]]

0 个答案:

没有答案