tensorflow - 为什么我必须在 tensorflow 中为线性回归打乱输入数据

标签 tensorflow linear-regression

我正在使用 tensorflow 构建线性回归模型,以下是我的代码。但是从我的实验来看,我必须对训练数据进行洗牌,否则权重和偏差将被估计为 na。谁能向我解释为什么我必须洗牌数据?谢谢

train_X = np.linspace(1, 50, 100)
train_Y = 1.5 * train_X + 10.0 + np.random.normal(scale=10, size=1)
data = list(zip(train_X, train_Y))
random.shuffle(data) # have to shuffle data, otherwise w and b would be na

X = tf.placeholder(dtype=tf.float32, shape=[], name='X')
Y = tf.placeholder(dtype=tf.float32, shape=[], name='Y')
W = tf.Variable(0.0, name='weight')
b = tf.Variable(0.0, name='bias')
Y_pred = W * X + b

cost = tf.square(Y-Y_pred, name="cost")
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(cost)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for _ in range(30):
        for x, y in data:
            sess.run(optimizer, feed_dict={X: x, Y: y})

        w_value, b_value = sess.run([W, b])
        print("w: {}, b: {}, {}".format(w_value, b_value, "test"))

最佳答案

有时数据按某些列排序
当您将数据拆分为 75% 与 25% 的比率时
您对最近 25% 分割中存在的某些值视而不见。
所以你学习除了测试中存在的值(最后 25% 行)之外的所有内容。
这就是为什么最好的方法是洗牌,以确保打破数据中的某些顺序,并了解存在的所有可能值

关于tensorflow - 为什么我必须在 tensorflow 中为线性回归打乱输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42017411/

相关文章:

R:使用 dynlm 包进行动态线性回归,如何预测()?

c - 从二维 vector 数组进行直线拟合

scikit-learn - 同时预测

tensorflow - 停止和启动深度学习谷歌云 VM 实例导致 tensorflow 停止识别 GPU

python - Tensorflow — 使用 `tf.keras.Model.add_metric` 时无法调用 `tf.distribute.MirroredStrategy`

python - 重新训练 inception 谷歌云陷入全局步骤 0

machine-learning - 线性回归中正则化常数的范围

python - 如何正确塑造我的神经网络模型的数据?

python - 从给定范围内的张量中删除值

javascript - D3.js 线性回归