Tensorflow - I get inaccurate predictions, NaN and

2019-07-29 11:54发布

问题:

I've started learning Tensorflow recently and I managed to write some simple code which predicts price of house. I am new to machine learning, therefore I still have to learn a lot and that's why I need Your help. The prediction of this program is inaccurate, moreover when I try to minimize loss on MSE instead of Cross entropy I get NaN and infinite values. Could You tell me where I made a mistake?

Here is my code:

import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error

LEARNING_RATE = 0.003

home_input = pd.read_csv("kc_house_data.csv")

features = ["bedrooms", "bathrooms", "sqft_living", "sqft_lot", "floors", "waterfront", "view", "condition", "grade", "sqft_above", "sqft_basement", "yr_built", "yr_renovated", "zipcode", "lat", "long", "sqft_living15", "sqft_lot15"]
label = ["price"]

X = tf.placeholder(tf.float32, [None, 18])
Y = tf.placeholder(tf.float32, [None, 1])

W = tf.Variable(tf.ones([18, 1]))
b = tf.Variable(1.)

Y_ = tf.matmul(X, W) + b

cross_entropy = -tf.reduce_sum(Y*tf.log(Y_))

loss = tf.reduce_sum(tf.square(Y_ - Y))

optimizer = tf.train.GradientDescentOptimizer(LEARNING_RATE)
train = optimizer.minimize(loss)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    j = 1
    for i in range(1000):
        j = i * 10
        x_data = np.array(home_input[features][j:(j+10)])
        y_data = np.array(home_input[label][j:(j+10)])
        sess.run(train, feed_dict={X: x_data, Y: y_data})
        print(sess.run(Y_, feed_dict={X: x_data, Y: y_data}))

回答1:

Normalize your data and then pass it to your network will fix the problem. To this aim, StandardScaler or MinMaxScaler may help you.

from sklearn.preprocessing import StandardScaler
data = [[0, 0], [0, 0], [1, 1], [1, 1]]
scaler = StandardScaler()
new_data = scaler.fit_transform(data)
...
# feed new_data to the neural network