【问题标题】:Tensorflow LinearRegressor not convergingTensorflow LinearRegressor 不收敛
【发布时间】:2017-12-22 16:15:14
【问题描述】:

我正在尝试使用 TensorFlow 在 Python 中进行玩具线性回归,使用预先构建的估计器 tf.contrib.learn.LinearRegressor 而不是构建我自己的估计器。 我使用的输入是 0 到 1 之间的实数值,输出只是 3*inputs。 TensorFlow 似乎适合数据(没有引发错误),但输出与它们应该是什么无关。

我不确定我的预测是否正确 - predict() 函数的文档非常少。

关于如何改进拟合的任何想法?

import numpy as np
import pandas as pd
import tensorflow as tf
import itertools
import matplotlib.pyplot as plt

#Defining data set
x = np.random.rand(200)
y = 3.0*x
data = pd.DataFrame({'X':x, 'Y':y})
training_data = data[50:]
test_data= data[:50]

COLUMNS = ['Y','X']
FEATURES = ['X']
LABELS = 'Y'

#Wrapper function for the inputs of LinearRegressor
def get_input_fn(data_set, num_epochs=None, shuffle=True):
  return tf.estimator.inputs.pandas_input_fn(
      x=pd.DataFrame(data_set[FEATURES]),
      y=pd.Series(data_set[LABELS]),
      num_epochs=num_epochs,
      shuffle=shuffle)


feature_cols = [tf.feature_column.numeric_column(k) for k in FEATURES]
regressor = tf.contrib.learn.LinearRegressor(feature_columns=feature_cols)
regressor.fit(input_fn=get_input_fn(test_data), steps=100)

results = regressor.predict(input_fn=get_input_fn(test_data, 
num_epochs=1))
predictions = list(itertools.islice(results, 50))

#Visualizing the results
fig = plt.figure(figsize=[8,8])
ax = fig.add_subplot(111)
ax.scatter(test_data[LABELS], predictions)

ax.set_xlabel('Actual')
ax.set_ylabel('Predicted')
plt.show()

Scatter plot of results

【问题讨论】:

    标签: python machine-learning tensorflow regression


    【解决方案1】:

    想出了答案,在这里为后人回答- 我对 LinearRegressor 的输入函数将 shuffle=True 设置为参数,而我的 predict() 调用没有设置 shuffle=False。所以输出被打乱了,让它们看起来好像没有收敛!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多