我最近开始学习Tensorflow,并设法编写了一些简单的代码来预测房屋价格。我是机器学习的新手,因此我仍然必须学习很多东西,这就是为什么我需要您的帮助。该程序的预测是不准确的,而且,当我尝试最小化MSE的损失而不是交叉熵时,我会得到NaN和无穷大的值。你能告诉我我哪里做错了吗?
这是我的代码:
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}))
答案 0 :(得分:1)
标准化您的数据,然后将其传递到您的网络将解决此问题。为此,StandardScaler或MinMaxScaler可能会为您提供帮助。
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