tensorflow + GradientDescentOptimizer 拟合直线

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

def LineModel(SrcX, SrcK, SrcB):
    return tf.matmul(SrcX, SrcK) + SrcB

DataNum = 100
SrcX = np.linspace(-1, 1, num=DataNum)
SrcX = np.reshape(SrcX, [DataNum, 1])
SrcY = SrcX * 0.33 + 10  + (np.random.rand(DataNum, 1) - 0.5) * 0.2

tfSrcX = tf.constant(SrcX, shape=[DataNum, 1], dtype=tf.float32)
tfSrcW = tf.Variable(tf.random_uniform([1, 1], minval=-1, maxval=1, dtype=tf.float32))
tfSrcB = tf.Variable(tf.zeros([1]))
tfSrcY = LineModel(tfSrcX, tfSrcW, tfSrcB)
tfCost = tf.reduce_sum(tf.pow(SrcY - tfSrcY, 2), 0) / DataNum
TrainOP = tf.train.GradientDescentOptimizer(0.1).minimize(tfCost)


with tf.Session() as Sess:
    tf.initialize_all_variables().run()
    print("w should be something around [0.33]: ", Sess.run(tfSrcW))
    print("b should be something around [10]:", Sess.run(tfSrcB))

    for i in range(100):
        # TrainOP.run()
        Sess.run(TrainOP)

        print(i)
        print("w should be something around [0.33]: ", Sess.run(tfSrcW))
        print("b should be something around [10]:", Sess.run(tfSrcB))
        print("")

    plt.plot(SrcX, SrcY, "ro", label="Orinal data")
    plt.plot(SrcX, tfSrcW.eval() * SrcX + tfSrcB.eval(), label="Fitted line")
    plt.legend()
    plt.show()
0
w should be something around [0.33]:  [[0.52602535]]
b should be something around [10]: [2.000546]

1
w should be something around [0.33]:  [[0.5125659]]
b should be something around [10]: [3.6009827]

2
w should be something around [0.33]:  [[0.5000219]]
b should be something around [10]: [4.8813324]

3
w should be something around [0.33]:  [[0.48833093]]
b should be something around [10]: [5.905612]

4
w should be something around [0.33]:  [[0.47743517]]
b should be something around [10]: [6.7250357]

5
w should be something around [0.33]:  [[0.46728054]]
b should be something around [10]: [7.3805747]

6
w should be something around [0.33]:  [[0.45781657]]
b should be something around [10]: [7.905006]

7
w should be something around [0.33]:  [[0.44899622]]
b should be something around [10]: [8.324551]

8
w should be something around [0.33]:  [[0.44077578]]
b should be something around [10]: [8.660187]

9
w should be something around [0.33]:  [[0.43311444]]
b should be something around [10]: [8.928696]

10
w should be something around [0.33]:  [[0.42597416]]
b should be something around [10]: [9.143502]

11
w should be something around [0.33]:  [[0.41931954]]
b should be something around [10]: [9.315348]

12
w should be something around [0.33]:  [[0.41311753]]
b should be something around [10]: [9.452824]

13
w should be something around [0.33]:  [[0.40733734]]
b should be something around [10]: [9.562805]

14
w should be something around [0.33]:  [[0.40195027]]
b should be something around [10]: [9.65079]

15
w should be something around [0.33]:  [[0.3969296]]
b should be something around [10]: [9.721178]

16
w should be something around [0.33]:  [[0.3922504]]
b should be something around [10]: [9.777489]

17
w should be something around [0.33]:  [[0.38788944]]
b should be something around [10]: [9.822536]

18
w should be something around [0.33]:  [[0.3838251]]
b should be something around [10]: [9.858575]

19
w should be something around [0.33]:  [[0.3800372]]
b should be something around [10]: [9.887405]

20
w should be something around [0.33]:  [[0.3765069]]
b should be something around [10]: [9.91047]

21
w should be something around [0.33]:  [[0.37321672]]
b should be something around [10]: [9.928922]

22
w should be something around [0.33]:  [[0.37015033]]
b should be something around [10]: [9.943684]

23
w should be something around [0.33]:  [[0.3672925]]
b should be something around [10]: [9.955493]

24
w should be something around [0.33]:  [[0.36462903]]
b should be something around [10]: [9.96494]

25
w should be something around [0.33]:  [[0.3621467]]
b should be something around [10]: [9.972498]

26
w should be something around [0.33]:  [[0.3598332]]
b should be something around [10]: [9.978544]

27
w should be something around [0.33]:  [[0.35767707]]
b should be something around [10]: [9.983381]

28
w should be something around [0.33]:  [[0.3556676]]
b should be something around [10]: [9.987251]

29
w should be something around [0.33]:  [[0.35379478]]
b should be something around [10]: [9.990347]

30
w should be something around [0.33]:  [[0.35204935]]
b should be something around [10]: [9.992824]

31
w should be something around [0.33]:  [[0.35042262]]
b should be something around [10]: [9.994804]

32
w should be something around [0.33]:  [[0.34890652]]
b should be something around [10]: [9.996389]

33
w should be something around [0.33]:  [[0.34749353]]
b should be something around [10]: [9.997658]

34
w should be something around [0.33]:  [[0.34617665]]
b should be something around [10]: [9.9986725]

35
w should be something around [0.33]:  [[0.34494933]]
b should be something around [10]: [9.999484]

36
w should be something around [0.33]:  [[0.3438055]]
b should be something around [10]: [10.0001335]

37
w should be something around [0.33]:  [[0.34273946]]
b should be something around [10]: [10.000652]

38
w should be something around [0.33]:  [[0.3417459]]
b should be something around [10]: [10.001068]

39
w should be something around [0.33]:  [[0.34081995]]
b should be something around [10]: [10.0014]

40
w should be something around [0.33]:  [[0.33995697]]
b should be something around [10]: [10.001666]

41
w should be something around [0.33]:  [[0.3391527]]
b should be something around [10]: [10.001879]

42
w should be something around [0.33]:  [[0.3384031]]
b should be something around [10]: [10.0020485]

43
w should be something around [0.33]:  [[0.3377045]]
b should be something around [10]: [10.002185]

44
w should be something around [0.33]:  [[0.33705342]]
b should be something around [10]: [10.002294]

45
w should be something around [0.33]:  [[0.3364466]]
b should be something around [10]: [10.00238]

46
w should be something around [0.33]:  [[0.33588108]]
b should be something around [10]: [10.00245]

47
w should be something around [0.33]:  [[0.335354]]
b should be something around [10]: [10.002506]

48
w should be something around [0.33]:  [[0.33486277]]
b should be something around [10]: [10.002551]

49
w should be something around [0.33]:  [[0.33440495]]
b should be something around [10]: [10.002586]

50
w should be something around [0.33]:  [[0.33397827]]
b should be something around [10]: [10.002615]

51
w should be something around [0.33]:  [[0.3335806]]
b should be something around [10]: [10.002638]

52
w should be something around [0.33]:  [[0.33321]]
b should be something around [10]: [10.002656]

53
w should be something around [0.33]:  [[0.33286458]]
b should be something around [10]: [10.00267]

54
w should be something around [0.33]:  [[0.33254266]]
b should be something around [10]: [10.002682]

55
w should be something around [0.33]:  [[0.33224264]]
b should be something around [10]: [10.002691]

56
w should be something around [0.33]:  [[0.33196303]]
b should be something around [10]: [10.002699]

57
w should be something around [0.33]:  [[0.33170244]]
b should be something around [10]: [10.002705]

58
w should be something around [0.33]:  [[0.33145958]]
b should be something around [10]: [10.002709]

59
w should be something around [0.33]:  [[0.33123323]]
b should be something around [10]: [10.002713]

60
w should be something around [0.33]:  [[0.33102226]]
b should be something around [10]: [10.002716]

61
w should be something around [0.33]:  [[0.33082566]]
b should be something around [10]: [10.002719]

62
w should be something around [0.33]:  [[0.33064243]]
b should be something around [10]: [10.002721]

63
w should be something around [0.33]:  [[0.33047166]]
b should be something around [10]: [10.002723]

64
w should be something around [0.33]:  [[0.3303125]]
b should be something around [10]: [10.002724]

65
w should be something around [0.33]:  [[0.33016416]]
b should be something around [10]: [10.002725]

66
w should be something around [0.33]:  [[0.3300259]]
b should be something around [10]: [10.002726]

67
w should be something around [0.33]:  [[0.32989708]]
b should be something around [10]: [10.002727]

68
w should be something around [0.33]:  [[0.329777]]
b should be something around [10]: [10.0027275]

69
w should be something around [0.33]:  [[0.3296651]]
b should be something around [10]: [10.0027275]

70
w should be something around [0.33]:  [[0.3295608]]
b should be something around [10]: [10.0027275]

71
w should be something around [0.33]:  [[0.32946357]]
b should be something around [10]: [10.0027275]

72
w should be something around [0.33]:  [[0.32937297]]
b should be something around [10]: [10.0027275]

73
w should be something around [0.33]:  [[0.32928854]]
b should be something around [10]: [10.0027275]

74
w should be something around [0.33]:  [[0.32920986]]
b should be something around [10]: [10.0027275]

75
w should be something around [0.33]:  [[0.32913652]]
b should be something around [10]: [10.0027275]

76
w should be something around [0.33]:  [[0.32906815]]
b should be something around [10]: [10.0027275]

77
w should be something around [0.33]:  [[0.32900444]]
b should be something around [10]: [10.0027275]

78
w should be something around [0.33]:  [[0.32894507]]
b should be something around [10]: [10.0027275]

79
w should be something around [0.33]:  [[0.32888973]]
b should be something around [10]: [10.0027275]

80
w should be something around [0.33]:  [[0.32883817]]
b should be something around [10]: [10.0027275]

81
w should be something around [0.33]:  [[0.3287901]]
b should be something around [10]: [10.0027275]

82
w should be something around [0.33]:  [[0.3287453]]
b should be something around [10]: [10.0027275]

83
w should be something around [0.33]:  [[0.32870355]]
b should be something around [10]: [10.0027275]

84
w should be something around [0.33]:  [[0.32866466]]
b should be something around [10]: [10.0027275]

85
w should be something around [0.33]:  [[0.3286284]]
b should be something around [10]: [10.0027275]

86
w should be something around [0.33]:  [[0.3285946]]
b should be something around [10]: [10.0027275]

87
w should be something around [0.33]:  [[0.3285631]]
b should be something around [10]: [10.0027275]

88
w should be something around [0.33]:  [[0.32853374]]
b should be something around [10]: [10.0027275]

89
w should be something around [0.33]:  [[0.32850638]]
b should be something around [10]: [10.0027275]

90
w should be something around [0.33]:  [[0.3284809]]
b should be something around [10]: [10.0027275]

91
w should be something around [0.33]:  [[0.32845715]]
b should be something around [10]: [10.0027275]

92
w should be something around [0.33]:  [[0.328435]]
b should be something around [10]: [10.0027275]

93
w should be something around [0.33]:  [[0.32841435]]
b should be something around [10]: [10.0027275]

94
w should be something around [0.33]:  [[0.32839513]]
b should be something around [10]: [10.0027275]

95
w should be something around [0.33]:  [[0.3283772]]
b should be something around [10]: [10.0027275]

96
w should be something around [0.33]:  [[0.32836047]]
b should be something around [10]: [10.0027275]

97
w should be something around [0.33]:  [[0.3283449]]
b should be something around [10]: [10.0027275]

98
w should be something around [0.33]:  [[0.3283304]]
b should be something around [10]: [10.0027275]

99
w should be something around [0.33]:  [[0.32831687]]
b should be something around [10]: [10.0027275]

tensorflow + GradientDescentOptimizer 拟合直线

 

上一篇:2.3.3 `do while`循环


下一篇:Java内存访问重排序的研究