zjf*_*fdu 5 linear-regression tensorflow
我正在使用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"))
Run Code Online (Sandbox Code Playgroud)
小智 1
有时数据按某些列排序,当您将数据拆分为 75% 与 25% 的比例时,您对最后 25% 拆分中存在的某些值是盲目的。所以你会学到除了测试中存在的值(最后 25% 行)之外的所有内容。这就是为什么最好的方法是进行洗牌,以确保打破数据中的某些顺序,并了解存在的所有可能值
| 归档时间: |
|
| 查看次数: |
1020 次 |
| 最近记录: |