tensorflow实现最简单的线性回归

下面我们需要构建整个模型的损失函数,即各数据点到该直线的距离,这里我们构建的损失函数为均方误差函数:

                                                   f(W,b)=\frac{1}{m} \sum_{i=1}^{m}(H(x^{(i)} - y^{(i)})))^2

代码:

import tensorflow as tf
x_train = [1, 2, 3, 4]
y_train = [3, 5, 7, 9]

W = tf.Variable(tf.random_normal([1]), name='weight') #权重
b = tf.Variable(tf.random_normal([1]), name='bias') #偏值

hypothesis = x_train*W + b

#其中 tf.square() 为取某个数的平方,而 tf.reduce_mean() 为取均值
cost = tf.reduce_mean(tf.square(hypothesis - y_train)) 

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) #梯度下降算法
# optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)

sess = tf.Session() #创建会话
sess.run(tf.global_variables_initializer()) #初始化全局变量

for step in range(5001):
    sess.run(train)
    if step%20 == 0:
        print('step:', step, ' cost:', sess.run(cost), ' W:', sess.run(W), ' b:', sess.run(b), ' hypothesis:', sess.run(hypothesis))

运行结果:

step: 0  cost: 14.497494  W: [0.79320323]  b: [0.456508]  hypothesis: [1.2497113 2.0429144 2.8361175 3.6293209]
step: 20  cost: 0.011808948  W: [2.0059235]  b: [0.8767241]  hypothesis: [2.8826475 4.8885713 6.894495  8.900418 ]
step: 40  cost: 0.0018746607  W: [2.0351374]  b: [0.89395285]  hypothesis: [2.9290903 4.9642277 6.9993653 9.034502 ]
step: 60  cost: 0.0016570443  W: [2.033857]  b: [0.90038544]  hypothesis: [2.9342425 4.9680996 7.0019565 9.035814 ]
step: 80  cost: 0.001469735  W: [2.0319061]  b: [0.90619034]  hypothesis: [2.9380965 4.9700027 7.001909  9.033814 ]
step: 100  cost: 0.0013036326  W: [2.0300496]  b: [0.91165066]  hypothesis: [2.9417002 4.97175   7.0017996 9.031849 ]
step: 120  cost: 0.0011562991  W: [2.0283008]  b: [0.9167929]  hypothesis: [2.9450936 4.9733944 7.001695  9.029996 ]
step: 140  cost: 0.0010256033  W: [2.026653]  b: [0.921636]  hypothesis: [2.948289 4.974942 7.001595 9.028248]
step: 160  cost: 0.0009096757  W: [2.0251017]  b: [0.92619747]  hypothesis: [2.9512992 4.976401  7.0015025 9.026604 ]
step: 180  cost: 0.0008068702  W: [2.0236406]  b: [0.9304932]  hypothesis: [2.9541337 4.9777746 7.0014153 9.025056 ]
step: 200  cost: 0.0007156696  W: [2.0222647]  b: [0.93453896]  hypothesis: [2.9568038 4.9790683 7.001333  9.023598 ]
step: 220  cost: 0.0006347785  W: [2.020969]  b: [0.9383491]  hypothesis: [2.9593182 4.980287  7.001256  9.022224 ]
step: 240  cost: 0.0005630411  W: [2.0197482]  b: [0.9419374]  hypothesis: [2.9616857 4.981434  7.001182  9.02093  ]
step: 260  cost: 0.00049941416  W: [2.018599]  b: [0.94531685]  hypothesis: [2.9639158 4.982515  7.001114  9.019713 ]
step: 280  cost: 0.00044296394  W: [2.0175164]  b: [0.9484997]  hypothesis: [2.966016  4.9835324 7.001049  9.018565 ]
step: 300  cost: 0.00039289828  W: [2.016497]  b: [0.9514972]  hypothesis: [2.9679942 4.984491  7.000988  9.017485 ]
step: 320  cost: 0.0003484954  W: [2.0155368]  b: [0.95432013]  hypothesis: [2.969857  4.9853935 7.0009303 9.016467 ]
step: 340  cost: 0.00030910384  W: [2.0146325]  b: [0.95697904]  hypothesis: [2.9716115 4.986244  7.0008764 9.015509 ]
step: 360  cost: 0.00027416967  W: [2.0137806]  b: [0.959483]  hypothesis: [2.9732637 4.9870443 7.000825  9.0146055]
step: 380  cost: 0.0002431866  W: [2.0129786]  b: [0.9618413]  hypothesis: [2.97482  4.987798 7.000777 9.013756]
step: 400  cost: 0.00021570362  W: [2.0122232]  b: [0.9640623]  hypothesis: [2.9762855 4.9885087 7.000732  9.012956 ]
step: 420  cost: 0.00019132036  W: [2.0115118]  b: [0.96615404]  hypothesis: [2.977666  4.9891777 7.0006895 9.012201 ]
step: 440  cost: 0.00016969867  W: [2.0108416]  b: [0.96812415]  hypothesis: [2.9789658 4.989807  7.0006495 9.011491 ]
step: 460  cost: 0.00015051577  W: [2.0102105]  b: [0.96997947]  hypothesis: [2.98019   4.9904003 7.000611  9.010821 ]
step: 480  cost: 0.00013350215  W: [2.0096164]  b: [0.9717268]  hypothesis: [2.9813433 4.9909596 7.000576  9.010192 ]
step: 500  cost: 0.00011841652  W: [2.0090568]  b: [0.9733724]  hypothesis: [2.9824293 4.991486  7.0005426 9.0096   ]
step: 520  cost: 0.00010503392  W: [2.0085297]  b: [0.9749223]  hypothesis: [2.9834518 4.9919815 7.000511  9.009041 ]
step: 540  cost: 9.3163006e-05  W: [2.008033]  b: [0.9763819]  hypothesis: [2.984415 4.992448 7.000481 9.008514]
step: 560  cost: 8.263322e-05  W: [2.0075655]  b: [0.9777566]  hypothesis: [2.985322  4.9928875 7.000453  9.0080185]
step: 580  cost: 7.329445e-05  W: [2.0071251]  b: [0.97905123]  hypothesis: [2.9861765 4.9933014 7.000427  9.007552 ]
step: 600  cost: 6.500938e-05  W: [2.0067105]  b: [0.98027056]  hypothesis: [2.9869812 4.9936914 7.000402  9.0071125]
step: 620  cost: 5.7660847e-05  W: [2.0063198]  b: [0.9814189]  hypothesis: [2.9877386 4.9940586 7.000378  9.006698 ]
step: 640  cost: 5.114451e-05  W: [2.0059521]  b: [0.98250043]  hypothesis: [2.9884524 4.994405  7.000357  9.006309 ]
step: 660  cost: 4.5362896e-05  W: [2.0056057]  b: [0.98351896]  hypothesis: [2.9891248 4.9947305 7.000336  9.005941 ]
step: 680  cost: 4.023615e-05  W: [2.0052793]  b: [0.98447824]  hypothesis: [2.9897575 4.995037  7.0003157 9.005595 ]
step: 700  cost: 3.5691286e-05  W: [2.004972]  b: [0.98538166]  hypothesis: [2.9903536 4.9953256 7.0002975 9.00527  ]
step: 720  cost: 3.1656087e-05  W: [2.0046825]  b: [0.9862326]  hypothesis: [2.990915  4.995598  7.0002804 9.004963 ]
step: 740  cost: 2.807828e-05  W: [2.00441]  b: [0.98703384]  hypothesis: [2.9914439 4.995854  7.000264  9.004674 ]
step: 760  cost: 2.4904344e-05  W: [2.0041535]  b: [0.98778844]  hypothesis: [2.991942  4.9960957 7.000249  9.004402 ]
step: 780  cost: 2.2091603e-05  W: [2.0039117]  b: [0.98849916]  hypothesis: [2.992411  4.9963226 7.000234  9.004147 ]
step: 800  cost: 1.9592733e-05  W: [2.003684]  b: [0.9891685]  hypothesis: [2.9928527 4.9965367 7.000221  9.003904 ]
step: 820  cost: 1.7379327e-05  W: [2.0034697]  b: [0.9897988]  hypothesis: [2.9932685 4.9967384 7.000208  9.003677 ]
step: 840  cost: 1.5417123e-05  W: [2.003268]  b: [0.99039245]  hypothesis: [2.9936604 4.996928  7.0001965 9.003465 ]
step: 860  cost: 1.3674962e-05  W: [2.0030777]  b: [0.9909514]  hypothesis: [2.994029  4.997107  7.000185  9.0032625]
step: 880  cost: 1.2129233e-05  W: [2.0028985]  b: [0.99147797]  hypothesis: [2.9943764 4.997275  7.0001736 9.003072 ]
step: 900  cost: 1.0758734e-05  W: [2.00273]  b: [0.99197394]  hypothesis: [2.9947038 4.9974337 7.0001636 9.002893 ]
step: 920  cost: 9.543119e-06  W: [2.002571]  b: [0.992441]  hypothesis: [2.995012  4.9975834 7.0001545 9.002726 ]
step: 940  cost: 8.464629e-06  W: [2.0024214]  b: [0.99288094]  hypothesis: [2.9953022 4.9977236 7.000145  9.002566 ]
step: 960  cost: 7.508552e-06  W: [2.0022805]  b: [0.99329525]  hypothesis: [2.9955757 4.997856  7.000137  9.002418 ]
step: 980  cost: 6.658999e-06  W: [2.002148]  b: [0.99368566]  hypothesis: [2.9958336 4.9979815 7.0001297 9.002277 ]
step: 1000  cost: 5.9064487e-06  W: [2.0020227]  b: [0.9940531]  hypothesis: [2.9960759 4.9980984 7.000121  9.002144 ]
step: 1020  cost: 5.238907e-06  W: [2.0019052]  b: [0.9943992]  hypothesis: [2.9963045 4.9982095 7.0001144 9.00202  ]
step: 1040  cost: 4.647267e-06  W: [2.001794]  b: [0.99472505]  hypothesis: [2.996519  4.9983134 7.000108  9.001902 ]
step: 1060  cost: 4.122803e-06  W: [2.00169]  b: [0.99503195]  hypothesis: [2.9967217 4.9984117 7.0001016 9.001792 ]
step: 1080  cost: 3.6565127e-06  W: [2.0015917]  b: [0.99532104]  hypothesis: [2.9969127 4.9985046 7.0000963 9.001688 ]
step: 1100  cost: 3.2436774e-06  W: [2.0014992]  b: [0.9955932]  hypothesis: [2.9970922 4.9985914 7.0000906 9.00159  ]
step: 1120  cost: 2.877028e-06  W: [2.001412]  b: [0.9958495]  hypothesis: [2.9972615 4.9986734 7.0000854 9.001497 ]
step: 1140  cost: 2.5521838e-06  W: [2.0013297]  b: [0.9960909]  hypothesis: [2.9974205 4.99875   7.00008   9.00141  ]
step: 1160  cost: 2.264207e-06  W: [2.0012527]  b: [0.9963181]  hypothesis: [2.9975708 4.998823  7.0000763 9.001328 ]
step: 1180  cost: 2.0085313e-06  W: [2.0011797]  b: [0.9965322]  hypothesis: [2.997712  4.998892  7.0000715 9.001251 ]
step: 1200  cost: 1.7817017e-06  W: [2.0011113]  b: [0.99673384]  hypothesis: [2.9978452 4.998956  7.0000677 9.001179 ]
step: 1220  cost: 1.5801786e-06  W: [2.0010467]  b: [0.9969238]  hypothesis: [2.9979706 4.9990172 7.000064  9.00111  ]
step: 1240  cost: 1.4019823e-06  W: [2.0009856]  b: [0.9971026]  hypothesis: [2.9980884 4.999074  7.0000596 9.001045 ]
step: 1260  cost: 1.2439975e-06  W: [2.0009284]  b: [0.99727076]  hypothesis: [2.9981992 4.9991274 7.000056  9.000984 ]
step: 1280  cost: 1.1039815e-06  W: [2.0008748]  b: [0.99742943]  hypothesis: [2.9983041 4.999179  7.000054  9.000929 ]
step: 1300  cost: 9.788264e-07  W: [2.0008235]  b: [0.9975789]  hypothesis: [2.9984024 4.999226  7.0000496 9.000873 ]
step: 1320  cost: 8.6861525e-07  W: [2.0007758]  b: [0.9977196]  hypothesis: [2.9984953 4.9992714 7.000047  9.000823 ]
step: 1340  cost: 7.704128e-07  W: [2.0007308]  b: [0.99785227]  hypothesis: [2.998583 4.999314 7.000045 9.000775]
step: 1360  cost: 6.834642e-07  W: [2.0006878]  b: [0.9979771]  hypothesis: [2.9986649 4.999353  7.000041  9.000729 ]
step: 1380  cost: 6.0630487e-07  W: [2.0006485]  b: [0.9980948]  hypothesis: [2.9987433 4.9993916 7.00004   9.000689 ]
step: 1400  cost: 5.3798794e-07  W: [2.0006104]  b: [0.9982053]  hypothesis: [2.9988155 4.999426  7.0000362 9.000647 ]
step: 1420  cost: 4.774367e-07  W: [2.0005753]  b: [0.9983097]  hypothesis: [2.998885 4.99946  7.000036 9.000611]
step: 1440  cost: 4.2326036e-07  W: [2.0005417]  b: [0.9984077]  hypothesis: [2.9989495 4.999491  7.000033  9.000574 ]
step: 1460  cost: 3.757561e-07  W: [2.0005102]  b: [0.99850035]  hypothesis: [2.9990106 4.999521  7.000031  9.000542 ]
step: 1480  cost: 3.3348664e-07  W: [2.0004811]  b: [0.9985873]  hypothesis: [2.9990685 4.9995494 7.0000305 9.000512 ]
step: 1500  cost: 2.9567474e-07  W: [2.0004525]  b: [0.99866915]  hypothesis: [2.9991217 4.999574  7.0000267 9.000479 ]
step: 1520  cost: 2.6227417e-07  W: [2.0004263]  b: [0.9987468]  hypothesis: [2.9991732 4.9995995 7.0000257 9.000452 ]
step: 1540  cost: 2.3285418e-07  W: [2.000402]  b: [0.9988194]  hypothesis: [2.9992213 4.9996233 7.0000253 9.000427 ]
step: 1560  cost: 2.0659088e-07  W: [2.0003781]  b: [0.99888784]  hypothesis: [2.999266  4.9996443 7.0000224 9.000401 ]
step: 1580  cost: 1.831321e-07  W: [2.0003562]  b: [0.99895275]  hypothesis: [2.999309  4.9996653 7.0000215 9.000378 ]
step: 1600  cost: 1.6265739e-07  W: [2.0003357]  b: [0.9990135]  hypothesis: [2.999349  4.999685  7.0000205 9.000357 ]
step: 1620  cost: 1.442934e-07  W: [2.0003166]  b: [0.99907047]  hypothesis: [2.999387  4.999704  7.0000205 9.000337 ]
step: 1640  cost: 1.2805664e-07  W: [2.0002975]  b: [0.9991243]  hypothesis: [2.9994218 4.9997196 7.000017  9.000315 ]
step: 1660  cost: 1.1344804e-07  W: [2.0002804]  b: [0.9991754]  hypothesis: [2.999456  4.9997363 7.0000167 9.000297 ]
step: 1680  cost: 1.0072195e-07  W: [2.0002644]  b: [0.99922323]  hypothesis: [2.9994876 4.999752  7.0000167 9.00028  ]
step: 1700  cost: 8.957301e-08  W: [2.0002494]  b: [0.9992681]  hypothesis: [2.9995174 4.999767  7.000016  9.000266 ]
step: 1720  cost: 7.954573e-08  W: [2.000235]  b: [0.9993102]  hypothesis: [2.9995453 4.99978   7.0000153 9.000251 ]
step: 1740  cost: 7.058503e-08  W: [2.0002208]  b: [0.99935013]  hypothesis: [2.9995708 4.9997916 7.0000124 9.000234 ]
step: 1760  cost: 6.244562e-08  W: [2.000208]  b: [0.9993882]  hypothesis: [2.999596 4.999804 7.000012 9.000219]
step: 1780  cost: 5.5463445e-08  W: [2.000196]  b: [0.9994238]  hypothesis: [2.9996197 4.999816  7.000012  9.000208 ]
step: 1800  cost: 4.9231517e-08  W: [2.0001848]  b: [0.99945724]  hypothesis: [2.999642  4.999827  7.0000114 9.000196 ]
step: 1820  cost: 4.379325e-08  W: [2.0001745]  b: [0.9994885]  hypothesis: [2.9996629 4.9998374 7.000012  9.000187 ]
step: 1840  cost: 3.8801147e-08  W: [2.0001645]  b: [0.9995178]  hypothesis: [2.9996824 4.999847  7.0000114 9.000175 ]
step: 1860  cost: 3.446945e-08  W: [2.000155]  b: [0.99954545]  hypothesis: [2.9997005 4.9998555 7.0000105 9.000165 ]
step: 1880  cost: 3.0613933e-08  W: [2.0001454]  b: [0.99957174]  hypothesis: [2.9997172 4.9998627 7.000008  9.000154 ]
step: 1900  cost: 2.7141425e-08  W: [2.0001369]  b: [0.99959713]  hypothesis: [2.999734  4.999871  7.0000076 9.000145 ]
step: 1920  cost: 2.399112e-08  W: [2.0001287]  b: [0.99962074]  hypothesis: [2.9997494 4.9998784 7.000007  9.000135 ]
step: 1940  cost: 2.1338849e-08  W: [2.0001216]  b: [0.9996429]  hypothesis: [2.9997644 4.999886  7.0000076 9.00013  ]
step: 1960  cost: 1.8875213e-08  W: [2.0001144]  b: [0.99966365]  hypothesis: [2.999778  4.9998927 7.000007  9.000121 ]
step: 1980  cost: 1.680337e-08  W: [2.000108]  b: [0.99968314]  hypothesis: [2.9997911 4.999899  7.0000076 9.000115 ]
step: 2000  cost: 1.4889409e-08  W: [2.0001018]  b: [0.99970144]  hypothesis: [2.9998033 4.999905  7.0000067 9.000109 ]
step: 2020  cost: 1.3252588e-08  W: [2.000096]  b: [0.99971855]  hypothesis: [2.9998145 4.999911  7.0000067 9.000103 ]
step: 2040  cost: 1.1791386e-08  W: [2.0000906]  b: [0.9997346]  hypothesis: [2.9998252 4.9999156 7.000006  9.000097 ]
step: 2060  cost: 1.0456873e-08  W: [2.0000856]  b: [0.9997496]  hypothesis: [2.9998353 4.999921  7.000006  9.000092 ]
step: 2080  cost: 9.316921e-09  W: [2.0000808]  b: [0.9997638]  hypothesis: [2.9998446 4.9999256 7.000006  9.000087 ]
step: 2100  cost: 8.320356e-09  W: [2.000076]  b: [0.9997769]  hypothesis: [2.999853 4.999929 7.000005 9.000081]
step: 2120  cost: 7.3663387e-09  W: [2.0000713]  b: [0.99979]  hypothesis: [2.9998612 4.999933  7.000004  9.000075 ]
step: 2140  cost: 6.5019776e-09  W: [2.0000668]  b: [0.9998026]  hypothesis: [2.9998693 4.999936  7.000003  9.00007  ]
step: 2160  cost: 5.724317e-09  W: [2.0000627]  b: [0.9998145]  hypothesis: [2.9998772 4.99994   7.0000024 9.000065 ]
step: 2180  cost: 5.083166e-09  W: [2.0000591]  b: [0.99982536]  hypothesis: [2.9998846 4.9999437 7.000003  9.000062 ]
step: 2200  cost: 4.4904738e-09  W: [2.0000556]  b: [0.9998359]  hypothesis: [2.9998915 4.999947  7.000003  9.000058 ]
step: 2220  cost: 3.981725e-09  W: [2.0000522]  b: [0.9998455]  hypothesis: [2.9998977 4.99995   7.000002  9.000054 ]
step: 2240  cost: 3.532307e-09  W: [2.0000494]  b: [0.9998548]  hypothesis: [2.9999042 4.9999533 7.000003  9.000052 ]
step: 2260  cost: 3.1386236e-09  W: [2.0000465]  b: [0.9998632]  hypothesis: [2.9999096 4.999956  7.0000024 9.00005  ]
step: 2280  cost: 2.7560532e-09  W: [2.0000436]  b: [0.99987143]  hypothesis: [2.9999151 4.9999585 7.000002  9.000046 ]
step: 2300  cost: 2.4604674e-09  W: [2.0000412]  b: [0.9998787]  hypothesis: [2.99992   4.9999614 7.000003  9.000044 ]
step: 2320  cost: 2.1692017e-09  W: [2.0000389]  b: [0.99988586]  hypothesis: [2.9999247 4.9999638 7.0000024 9.000041 ]
step: 2340  cost: 1.9243345e-09  W: [2.0000367]  b: [0.99989253]  hypothesis: [2.9999292 4.999966  7.000003  9.000039 ]
step: 2360  cost: 1.7322037e-09  W: [2.0000346]  b: [0.9998985]  hypothesis: [2.999933  4.9999676 7.0000024 9.000037 ]
step: 2380  cost: 1.5293722e-09  W: [2.0000327]  b: [0.99990445]  hypothesis: [2.999937 4.99997  7.000003 9.000035]
step: 2400  cost: 1.3656773e-09  W: [2.0000308]  b: [0.99990994]  hypothesis: [2.9999406 4.9999714 7.0000024 9.000033 ]
step: 2420  cost: 1.2206556e-09  W: [2.0000293]  b: [0.99991477]  hypothesis: [2.9999442 4.9999733 7.0000024 9.000032 ]
step: 2440  cost: 1.0942927e-09  W: [2.0000277]  b: [0.99991953]  hypothesis: [2.999947  4.9999747 7.0000024 9.0000305]
step: 2460  cost: 9.689103e-10  W: [2.000026]  b: [0.99992424]  hypothesis: [2.9999502 4.999976  7.0000024 9.000029 ]
step: 2480  cost: 8.5624663e-10  W: [2.0000246]  b: [0.9999284]  hypothesis: [2.999953  4.9999776 7.000002  9.000027 ]
step: 2500  cost: 7.787122e-10  W: [2.0000234]  b: [0.999932]  hypothesis: [2.9999554 4.9999785 7.000002  9.000026 ]
step: 2520  cost: 6.890133e-10  W: [2.0000222]  b: [0.99993557]  hypothesis: [2.9999578 4.99998   7.0000024 9.000024 ]
step: 2540  cost: 6.1915273e-10  W: [2.000021]  b: [0.99993914]  hypothesis: [2.9999602 4.999981  7.000002  9.000023 ]
step: 2560  cost: 5.4929217e-10  W: [2.0000198]  b: [0.9999427]  hypothesis: [2.9999626 4.9999824 7.000002  9.000022 ]
step: 2580  cost: 4.918661e-10  W: [2.0000188]  b: [0.99994576]  hypothesis: [2.9999647 4.9999833 7.000002  9.000021 ]
step: 2600  cost: 4.5304205e-10  W: [2.000018]  b: [0.9999482]  hypothesis: [2.9999661 4.999984  7.0000014 9.00002  ]
step: 2620  cost: 4.0905945e-10  W: [2.0000172]  b: [0.9999506]  hypothesis: [2.9999678 4.9999847 7.000002  9.000019 ]
step: 2640  cost: 3.7370285e-10  W: [2.0000162]  b: [0.999953]  hypothesis: [2.9999692 4.999985  7.0000014 9.000018 ]
step: 2660  cost: 3.3612935e-10  W: [2.0000157]  b: [0.99995536]  hypothesis: [2.9999712 4.9999866 7.0000024 9.000018 ]
step: 2680  cost: 3.0395597e-10  W: [2.0000148]  b: [0.99995774]  hypothesis: [2.9999726 4.999987  7.000002  9.000017 ]
step: 2700  cost: 2.6790303e-10  W: [2.000014]  b: [0.9999601]  hypothesis: [2.9999743 4.999988  7.000002  9.000016 ]
step: 2720  cost: 2.3754865e-10  W: [2.0000134]  b: [0.9999623]  hypothesis: [2.9999757 4.999989  7.0000024 9.000015 ]
step: 2740  cost: 2.2362201e-10  W: [2.0000129]  b: [0.9999637]  hypothesis: [2.9999766 4.9999895 7.0000024 9.000015 ]
step: 2760  cost: 2.0590107e-10  W: [2.0000124]  b: [0.999965]  hypothesis: [2.9999774 4.99999   7.0000024 9.000014 ]
step: 2780  cost: 1.974172e-10  W: [2.000012]  b: [0.9999662]  hypothesis: [2.999978 4.99999  7.000002 9.000014]
step: 2800  cost: 1.8077628e-10  W: [2.0000114]  b: [0.9999674]  hypothesis: [2.9999788 4.9999905 7.000002  9.000013 ]
step: 2820  cost: 1.6866863e-10  W: [2.0000112]  b: [0.9999686]  hypothesis: [2.9999797 4.999991  7.000002  9.000013 ]
step: 2840  cost: 1.5381829e-10  W: [2.0000107]  b: [0.9999698]  hypothesis: [2.9999804 4.9999914 7.0000024 9.000012 ]
step: 2860  cost: 1.4604495e-10  W: [2.0000103]  b: [0.999971]  hypothesis: [2.9999812 4.9999914 7.0000014 9.000012 ]
step: 2880  cost: 1.3433521e-10  W: [2.00001]  b: [0.99997216]  hypothesis: [2.999982  4.9999924 7.0000024 9.000012 ]
step: 2900  cost: 1.2187229e-10  W: [2.0000095]  b: [0.99997336]  hypothesis: [2.9999828 4.9999924 7.000002  9.000011 ]
step: 2920  cost: 1.110152e-10  W: [2.0000093]  b: [0.99997455]  hypothesis: [2.9999838 4.9999933 7.0000024 9.000011 ]
step: 2940  cost: 1.0271606e-10  W: [2.0000086]  b: [0.99997574]  hypothesis: [2.9999843 4.999993  7.0000014 9.0000105]
step: 2960  cost: 9.265477e-11  W: [2.0000083]  b: [0.99997693]  hypothesis: [2.9999852 4.999994  7.000002  9.0000105]
step: 2980  cost: 8.492407e-11  W: [2.000008]  b: [0.99997807]  hypothesis: [2.9999862 4.9999943 7.0000024 9.0000105]
step: 3000  cost: 7.482015e-11  W: [2.0000076]  b: [0.99997926]  hypothesis: [2.999987  4.9999943 7.000002  9.00001  ]
step: 3020  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3040  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3060  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3080  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3100  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3120  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3140  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3160  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3180  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3200  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3220  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3240  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3260  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3280  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3300  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3320  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3340  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3360  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3380  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3400  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3420  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3440  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3460  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3480  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3500  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3520  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3540  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3560  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3580  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3600  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3620  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3640  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3660  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3680  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3700  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3720  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3740  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3760  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3780  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3800  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3820  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3840  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3860  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3880  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3900  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3920  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3940  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3960  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 3980  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4000  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4020  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4040  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4060  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4080  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4100  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4120  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4140  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4160  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4180  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4200  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4220  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4240  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4260  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4280  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4300  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4320  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4340  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4360  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4380  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4400  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4420  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4440  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4460  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4480  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4500  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4520  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4540  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4560  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4580  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4600  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4620  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4640  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4660  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4680  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4700  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4720  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4740  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4760  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4780  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4800  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4820  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4840  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4860  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4880  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4900  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4920  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4940  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4960  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 4980  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]
step: 5000  cost: 6.7757355e-11  W: [2.0000074]  b: [0.9999802]  hypothesis: [2.9999876 4.999995  7.000002  9.00001  ]

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单变量线性回归是机器学习中最基础的模型之一,用于预测一个变量与另一个变量之间的线性关系。在本教程中,我们将使用TensorFlow编写实现单变量线性回归的程序。 1. 导入必要的库 首先,我们需要导入TensorFlow和其他必要的库。 ```python import tensorflow as tf import numpy as np import matplotlib.pyplot as plt ``` 2. 准备数据 我们将使用一个简单的数据集来演示单变量线性回归。该数据集包含两列数据,第一列是房屋的面积,第二列是房屋的价格。 ```python data = np.array([[1400, 245000], [1600, 312000], [1700, 279000], [1875, 308000], [1100, 199000], [1550, 219000], [2350, 405000], [2450, 324000]]) ``` 我们可以将数据集分成两个数组,一个用于输入(房屋面积),另一个用于输出(房屋价格)。 ```python x_data = data[:,0] y_data = data[:,1] ``` 接下来,我们将数据可视化,以便更好地理解数据集。 ```python plt.scatter(x_data, y_data, color='blue') plt.xlabel('House Area') plt.ylabel('House Price') plt.show() ``` 3. 创建模型 使用TensorFlow创建单变量线性回归模型的第一步是定义变量。 ```python X = tf.placeholder(tf.float32, name='X') Y = tf.placeholder(tf.float32, name='Y') W = tf.Variable(0.0, name='weights') B = tf.Variable(0.0, name='bias') ``` 我们定义了两个占位符变量X和Y,这些变量将在训练模型时用于输入和输出。我们还定义了两个变量W和B,这些变量将在训练过程中被优化。 接下来,我们定义了线性模型。 ```python Y_pred = tf.add(tf.multiply(X, W), B) ``` 这个简单的线性模型将输入X乘以权重W并加上偏置B。 4. 定义损失函数 接下来,我们需要定义一个损失函数来评估模型的性能。 ```python cost = tf.reduce_mean(tf.square(Y_pred - Y)) ``` 我们使用平方误差作为损失函数。 5. 定义优化器 为了最小化损失函数,我们需要定义一个优化器。 ```python optimizer = tf.train.GradientDescentOptimizer(0.0001).minimize(cost) ``` 我们使用梯度下降优化器来最小化损失函数。 6. 训练模型 我们现在可以开始训练我们的模型了。 ```python init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for i in range(10000): total_loss = 0 for x, y in zip(x_data, y_data): _, loss = sess.run([optimizer, cost], feed_dict={X: x, Y: y}) total_loss += loss if i % 1000 == 0: print("Epoch {0}: {1}".format(i, total_loss/len(x_data))) W, B = sess.run([W, B]) ``` 我们使用10000个epoch训练模型,并打印出每1000个epoch的平均损失。在训练完成后,我们获得了最终的权重W和偏置B。 7. 可视化结果 最后,我们可以可视化结果,以便更好地理解模型。 ```python plt.scatter(x_data, y_data, color='blue') plt.plot(x_data, W * x_data + B, color='red') plt.xlabel('House Area') plt.ylabel('House Price') plt.show() ``` 这个图形显示了原始数据点以及模型的线性拟合。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值