Tensorflow线性回归预测返回[nan]

时间:2019-08-27 09:11:04

标签: python tensorflow regression linear-regression

我正在尝试使用Tensor Flow(在没有估计器帮助的情况下)创建我的第一个线性回归器,并且在每次迭代中,我仅看到cost的{​​{1}}值。我认为我做的不正确,但是无法对此问题归零。有人可以帮我解决问题吗?

我正在使用CA住房数据集

NaN

我预计# Common imports import math import numpy as np import tensorflow as tf import pandas as pd from sklearn import metrics california_housing_dataframe = pd.read_csv("https://download.mlcc.google.com/mledu-datasets/california_housing_train.csv", sep=",")

median_house_value

创建培训和验证集

data_X = california_housing_dataframe.iloc[:, :8]
data_y = california_housing_dataframe.iloc[:, 8]
print('Features (X):\n', data_X.head(), '\n')
print('Target (y):\n', data_y.head(), '\n')

设置超空间参数和TensorFlow变量

from sklearn.model_selection import train_test_split

data_X_train, data_X_validate = train_test_split(data_X, test_size=0.2, random_state=42)
data_y_train, data_y_validate = train_test_split(data_y, test_size=0.2, random_state=42)

现在,训练模型仅返回# Hyperspace Params learning_rate = 0.01 training_epochs = 1 #40 batch_size = 500 #50 totalBatches = len(data_X_train)/batch_size n, m = data_X_train.shape # 17,000 Rows + 9 Features print('n=', n, ', m=', m) W = tf.Variable(tf.random_uniform([m, 1], -1.0, 1.0, dtype = tf.float64), name="theta") # Random initialization b = tf.Variable(np.random.randn(), name = "b", dtype = tf.float64) X = tf.placeholder(tf.float64, shape=(None, m), name="X") y = tf.placeholder(tf.float64, shape=(None, 1), name="y") print('X.shape :\n', X.shape, '\n') print('y.shape :\n', y.shape, '\n') print('b.shape :\n', b.shape, '\n') print('Thetha.shape (W):\n', W.shape, '\n') y_pred = tf.add(tf.matmul(X, W), b, name="predictions") error = y_pred - y cost = tf.reduce_mean(tf.square(error), name="mse") optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) # Global Variables Initializer init = tf.global_variables_initializer() 个值

NaN

结果看起来像

def get_batch(X, y, batch_size):
  rnd_idx = np.random.permutation(len(X))
  n_batches = len(X) // batch_size
  for batch_idx in np.array_split(rnd_idx, n_batches):
    X_batch, y_batch = X.iloc[batch_idx, :], y[batch_idx]
    yield X_batch, y_batch

# Global Variables Initializer
init = tf.global_variables_initializer()

with tf.Session() as sess:
  sess.run(init)
  for epoch in range(training_epochs):
    for X_batch, y_batch in get_batch(data_X_train, data_y_train, batch_size):
      y_batch = np.array(y_batch).reshape(-1, 1)
      sess.run(optimizer, feed_dict={X: X_batch, y: y_batch})
      curr_y_pred, curr_error, curr_cost = sess.run([y_pred, error, cost], {X: X_batch, y: y_batch})
      print('Training... batch.shape: ', X_batch.shape,'curr_error:', curr_error)

1 个答案:

答案 0 :(得分:1)

您的问题来自@mock.patch('utils.datetime.datetime.utcnow') def test_get_utc_timestamp(self, mock_dt): mock_dt.return_value = datetime.datetime(2019, 8, 27, 8, 52, 12, 703618) result = get_utc_timestamp() self.assertEqual(result, 1566895932.703618) 函数。我将其交换为pd.read_csv(...)版本(我不熟悉NumPy),它的工作原理很吸引人。这是整个代码段:

Pandas