为什么我必须在tensorflow中为线性回归洗牌输入数据

为什么我必须在tensorflow中为线性回归洗牌输入数据,tensorflow,linear-regression,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)

我正在使用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%行)之外,您了解了所有内容。 这就是为什么最好的方法是洗牌,以确保打破数据中的某些顺序,并了解所有可能存在的各种值