Machine learning 张量流中的线性回归-修改入门代码时出错

Machine learning 张量流中的线性回归-修改入门代码时出错,machine-learning,tensorflow,linear-regression,Machine Learning,Tensorflow,Linear Regression,我对TensorFlow非常陌生,我正在平行学习传统的机器学习技术。在此之前,我能够使用scikit在matlab和Python中成功地实现线性回归建模 当我试图用同一个数据集使用Tensorflow复制它时,我得到了无效的输出。有人能告诉我我在哪里犯了错误或者我错过了什么吗 事实上,我正在使用tensor flow入门教程中的代码,我刚刚将x\u列和y\u列更改为不同的数据集。 # Loading the ML coursera course ex1 (Wk 2) data to try it

我对TensorFlow非常陌生,我正在平行学习传统的机器学习技术。在此之前,我能够使用scikit在matlab和Python中成功地实现线性回归建模

当我试图用同一个数据集使用Tensorflow复制它时,我得到了无效的输出。有人能告诉我我在哪里犯了错误或者我错过了什么吗

事实上,我正在使用tensor flow入门教程中的代码,我刚刚将x\u列和y\u列更改为不同的数据集。

# Loading the ML coursera course ex1 (Wk 2) data to try it out
'''
path = r'C:\Users\Prasanth\Dropbox\Python Folder\ML in Python\data\ex1data1.txt'
fh = open(path,'r')

l1 = []
l2 = []
for line in fh: 
    temp = (line.strip().split(','))
    l1.append(float(temp[0]))
    l2.append(float(temp[1]))
'''
l1 = [6.1101, 5.5277, 8.5186, 7.0032, 5.8598, 8.3829, 7.4764, 8.5781, 6.4862, 5.0546, 5.7107, 14.164, 5.734, 8.4084, 5.6407, 5.3794, 6.3654, 5.1301, 6.4296, 7.0708, 6.1891, 20.27, 5.4901, 6.3261, 5.5649, 18.945, 12.828, 10.957, 13.176, 22.203, 5.2524, 6.5894, 9.2482, 5.8918, 8.2111, 7.9334, 8.0959, 5.6063, 12.836, 6.3534, 5.4069, 6.8825, 11.708, 5.7737, 7.8247, 7.0931, 5.0702, 5.8014, 11.7, 5.5416, 7.5402, 5.3077, 7.4239, 7.6031, 6.3328, 6.3589, 6.2742, 5.6397, 9.3102, 9.4536, 8.8254, 5.1793, 21.279, 14.908, 18.959, 7.2182, 8.2951, 10.236, 5.4994, 20.341, 10.136, 7.3345, 6.0062, 7.2259, 5.0269, 6.5479, 7.5386, 5.0365, 10.274, 5.1077, 5.7292, 5.1884, 6.3557, 9.7687, 6.5159, 8.5172, 9.1802, 6.002, 5.5204, 5.0594, 5.7077, 7.6366, 5.8707, 5.3054, 8.2934, 13.394, 5.4369]
l2 = [17.592, 9.1302, 13.662, 11.854, 6.8233, 11.886, 4.3483, 12.0, 6.5987, 3.8166, 3.2522, 15.505, 3.1551, 7.2258, 0.71618, 3.5129, 5.3048, 0.56077, 3.6518, 5.3893, 3.1386, 21.767, 4.263, 5.1875, 3.0825, 22.638, 13.501, 7.0467, 14.692, 24.147, -1.22, 5.9966, 12.134, 1.8495, 6.5426, 4.5623, 4.1164, 3.3928, 10.117, 5.4974, 0.55657, 3.9115, 5.3854, 2.4406, 6.7318, 1.0463, 5.1337, 1.844, 8.0043, 1.0179, 6.7504, 1.8396, 4.2885, 4.9981, 1.4233, -1.4211, 2.4756, 4.6042, 3.9624, 5.4141, 5.1694, -0.74279, 17.929, 12.054, 17.054, 4.8852, 5.7442, 7.7754, 1.0173, 20.992, 6.6799, 4.0259, 1.2784, 3.3411, -2.6807, 0.29678, 3.8845, 5.7014, 6.7526, 2.0576, 0.47953, 0.20421, 0.67861, 7.5435, 5.3436, 4.2415, 6.7981, 0.92695, 0.152, 2.8214, 1.8451, 4.2959, 7.2029, 1.9869, 0.14454, 9.0551, 0.61705]
print ('List length and data type', len(l1), type(l1))

#------------------#

import tensorflow as tf

# Model parameters
W = tf.Variable([0], dtype=tf.float64)
b = tf.Variable([0], dtype=tf.float64)

# Model input and output
x = tf.placeholder(tf.float64)
linear_model = W * x + b
y = tf.placeholder(tf.float64)

# loss or cost function
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer (gradient descent) with learning rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

# training data (labelled input & output swt)
# Using coursera data instead of sample data
#x_train = [1.0, 2, 3, 4]
#y_train = [0, -1, -2, -3]
x_train = l1
y_train = l2

# training loop (1000 iterations)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
  sess.run(train, {x: x_train, y: y_train})

# evaluate training accuracy
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))
输出

List length and data type: 97 <class 'list'>
W: [ nan] b: [ nan] loss: nan
列表长度和数据类型:97
W:[nan]b:[nan]损失:nan

估算器的一个主要问题是损失函数。由于您使用了
tf.reduce_sum
,因此损失会随着样本数的增加而增加,您必须使用较小的学习速率来补偿样本数。更好的解决方案是使用均方误差损失

loss = tf.reduce_mean(tf.square(linear_model - y))

你不应该计算损失函数中所有例子的平均值吗?你从哪里得到这个例子?您能展示示例或幻灯片中显示的公式吗?这是我从中获得示例代码的地方-&我尝试实现的公式可以在这里找到-好的,但在公式中您可以看到总和除以代码中缺少的
2*m
。事实上,我还添加了
2*m
。结果仍然是
nan
。它应该不会有什么区别,因为它是一个常数,输入大小也很小。举我自己的例子:“sum除以
2*m
”这里的关键点是除以->或者
1/2m*sum()
或者
sum()/2m
,但是肯定不会
2m*sum()
,谢谢。将代码更改为loss=
tf.reduce\u mean(tf.square(linear\u model-y))
后,它现在可以工作了。事实上,当我使用tf.reduce\u sum时,我非常怀疑,但我认为这不会有太大的区别。