问题描述
我最近开始学习 Tensorflow,并设法编写了一些简单的代码来预测房价.我是机器学习的新手,因此我仍然需要学习很多东西,这就是为什么我需要你的帮助.这个程序的预测是不准确的,而且当我尝试最小化 MSE 上的损失而不是交叉熵时,我得到 NaN 和无限值.你能告诉我我哪里出错了吗?
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?
这是我的代码:
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}))
推荐答案
规范化您的数据,然后将其传递到您的网络将解决问题.为此,StandardScaler 或 MinMaxScaler 可以帮助你.
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
这篇关于Tensorflow - 在尝试预测价格时,我得到了不准确的预测、NaN 和无限值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!