TensorFlow學(xué)習(xí):線性回歸算法

The linear regression algorithm

we begin our exploration of machine learning techniques with the linear regression algorithm. Our goal is to build a model by which to predict the values of a dependent variable from the values of one or more independent variables.

The relationship between these two variables is linear; that is, if y is the dependent variable and x the independent, then the linear relationship between the two variables will look like this: y = Ax + b.

The linear regression algorithm adapts to a great variety of situations; for its versatility, it is used extensively in the field of applied sciences, for example, biology and economics.

Furthermore, the implementation of this algorithm allows us to introduce in a totally clear and understandable way the two important concepts of machine learning: the cost function and the gradient descent algorithms.

Data model

The first crucial step is to build our data model. We mentioned earlier that the relationship between our variables is linear, that is: y = Ax + b, where A and b are constants. To test our algorithm, we need data points in a two-dimensional space.

We start by importing the Python library NumPy:

import numpy as np

Then we define the number of points we want to draw:

number_of_points = 500

We initialize the following two lists:

x_point = []
y_point = []

These points will contain the generated points.

We then set the two constants that will appear in the linear relation of y with x:

a = 0.22
b = 0.78

Via NumPy's random.normal function, we generate 300 random points around the regression equation y = 0.22x + 0.78:

for i in range(number_of_points):
    x = np.random.normal(0.0,0.5)
    y = a*x + b +np.random.normal(0.0,0.1)
    x_point.append([x])
    y_point.append([y])

Finally, view the generated points by matplotlib:

import matplotlib.pyplot as plt
plt.plot(x_point,y_point, 'o', label='Input Data')
plt.legend()
plt.show()
Linear regression: The data model
Cost functions and gradient descent

The machine learning algorithm that we want to implement with TensorFlow must predict values of y as a function of x data according to our data model. The linear regression algorithm will determine the values of the constants A and b (fixed for our data model), which are then the true unknowns of the problem.

The first step is to import the tensorflow library:

import tensorflow as tf

Then define the A and b unknowns, using the TensorFlow tf.Variable:

A = tf.Variable(tf.random_uniform([1], -1.0, 1.0))

The unknown factor A was initialized using a random value between -1 and 1, while the variable b is initially set to zero:

b = tf.Variable(tf.zeros([1]))

So we write the linear relationship that binds y to x:

y = A * x_point + b

Now we will introduce, this cost function: that has parameters containing a pair of values A and b to be determined which returns a value that estimates how well the parameters are correct. In this example, our cost function is mean square error:

cost_function = tf.reduce_mean(tf.square(y - y_point))

It provides an estimate of the variability of the measures, or more precisely, of the dispersion of values around the average value; a small value of this function corresponds to a best estimate for the unknown parameters A and b.

To minimize cost_function, we use an optimization algorithm with the gradient descent. Given a mathematical function of several variables, gradient descent allows to find a local minimum of this function. The technique is as follows:

Evaluate, at an arbitrary first point of the function's domain, the function itself and its gradient. The gradient indicates the direction in which the function tends to a minimum.

Select a second point in the direction indicated by the gradient. If the function for this second point has a value lower than the value calculated at the first point, the descent can continue.

You can refer to the following figure for a visual explanation of the algorithm:

The gradient descent algorithm

We also remark that the gradient descent is only a local function minimum, but it can also be used in the search for a global minimum, randomly choosing a new start point once it has found a local minimum and repeating the process many times. If the number of minima of the function is limited, and there are very high number of attempts, then there is a good chance that sooner or later the global minimum will be identified.

Using TensorFlow, the application of this algorithm is very simple. The instruction are as follows:

optimizer = tf.train.GradientDescentOptimizer(0.5)

Here 0.5is the learning rate of the algorithm.

The learning rate determines how fast or slow we move towards the optimal weights. If it is very large, we skip the optimal solution, and if it is too small, we need too many iterations to converge to the best values.

An intermediate value (0.5) is provided, but it must be tuned in order to improve the performance of the entire procedure.

We define train as the result of the application of the cost_function
(optimizer), through its minimizefunction:

train = optimizer.minimize(cost_function)

Testing the model

Now we can test the algorithm of gradient descent on the data model you created earlier. As usual, we have to initialize all the variables:

model = tf.initialize_all_variables()

So we build our iteration (20 computation steps), allowing us to determine the best values of A and b, which define the line that best fits the data model. Instantiate the evaluation graph:

with tf.Session() as session:

We perform the simulation on our model:

session.run(model) for step in range(0,21):

For each iteration, we execute the optimization step:

session.run(train)

Every five steps, we print our pattern of dots:

if (step % 5) == 0: 
          plt.plot(x_point,y_point,'o',
               label='step = {}'         
               .format(step))

And the straight lines are obtained by the following command:

                   plt.plot(x_point,
                              session.run(A) * 
                              x_point + 
                              session.run(B))
                     plt.legend()
                     plt.show()

The following figure shows the convergence of the implemented algorithm:

Linear regression : start computation (step = 0)

After just five steps, we can already see (in the next figure) a substantial improvement in the fit of the line:

Linear regression: situation after 5 computation steps
Linear regression: situation after 5 computation steps

The following (and final) figure shows the definitive result after 20 steps. We can see the efficiency of the algorithm used, with the straight line efficiency perfectly across the cloud of points.

Linear regression: final result
Linear regression: final result

Finally we report, to further our understanding, the complete code:

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
number_of_points = 200
x_point = []
y_point = []
a = 0.22
b = 0.78
for i in range(number_of_points):
    x = np.random.normal(0.0,0.5)
    y = a*x + b +np.random.normal(0.0,0.1)
    x_point.append([x])
    y_point.append([y])
plt.plot(x_point,y_point, 'o', label='Input Data')
plt.legend()
plt.show()
A = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
B = tf.Variable(tf.zeros([1]))
y = A * x_point + B
cost_function = tf.reduce_mean(tf.square(y - y_point))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(cost_function)
model = tf.initialize_all_variables()
with tf.Session() as session:
        session.run(model)
        for step in range(0,21):
                session.run(train)
                if (step % 5) == 0:
                        plt.plot(x_point,y_point,'o',
                                 label='step = {}'
                                 .format(step))
                        plt.plot(x_point,
                                 session.run(A) * 
                                 x_point + 
                                 session.run(B))
                        plt.legend()
                        plt.show()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市弦蹂,隨后出現(xiàn)的幾起案子肩碟,更是在濱河造成了極大的恐慌,老刑警劉巖凸椿,帶你破解...
    沈念sama閱讀 210,914評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件削祈,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機髓抑,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,935評論 2 383
  • 文/潘曉璐 我一進店門咙崎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人吨拍,你說我怎么就攤上這事∶苣” “怎么了握爷?”我有些...
    開封第一講書人閱讀 156,531評論 0 345
  • 文/不壞的土叔 我叫張陵燥撞,是天一觀的道長。 經(jīng)常有香客問我,道長悉盆,這世上最難降的妖魔是什么焕盟? 我笑而不...
    開封第一講書人閱讀 56,309評論 1 282
  • 正文 為了忘掉前任宏粤,我火速辦了婚禮脚翘,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘商架。我一直安慰自己堰怨,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 65,381評論 5 384
  • 文/花漫 我一把揭開白布蛇摸。 她就那樣靜靜地躺著备图,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上揽涮,一...
    開封第一講書人閱讀 49,730評論 1 289
  • 那天抠藕,我揣著相機與錄音,去河邊找鬼蒋困。 笑死盾似,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的雪标。 我是一名探鬼主播零院,決...
    沈念sama閱讀 38,882評論 3 404
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼村刨!你這毒婦竟也來了告抄?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,643評論 0 266
  • 序言:老撾萬榮一對情侶失蹤嵌牺,失蹤者是張志新(化名)和其女友劉穎打洼,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體逆粹,經(jīng)...
    沈念sama閱讀 44,095評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡募疮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,448評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了僻弹。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片阿浓。...
    茶點故事閱讀 38,566評論 1 339
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖奢方,靈堂內(nèi)的尸體忽然破棺而出搔扁,到底是詐尸還是另有隱情,我是刑警寧澤蟋字,帶...
    沈念sama閱讀 34,253評論 4 328
  • 正文 年R本政府宣布,位于F島的核電站扭勉,受9級特大地震影響鹊奖,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜涂炎,卻給世界環(huán)境...
    茶點故事閱讀 39,829評論 3 312
  • 文/蒙蒙 一忠聚、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧唱捣,春花似錦两蟀、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,715評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春党涕,著一層夾襖步出監(jiān)牢的瞬間烦感,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,945評論 1 264
  • 我被黑心中介騙來泰國打工膛堤, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留手趣,地道東北人。 一個月前我還...
    沈念sama閱讀 46,248評論 2 360
  • 正文 我出身青樓肥荔,卻偏偏與公主長得像绿渣,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子燕耿,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,440評論 2 348

推薦閱讀更多精彩內(nèi)容

  • 計劃已達(dá)成中符,會畫思維導(dǎo)圖了,但是都是簡單的那種缸棵。無論做什么都有個框架抓個核心舟茶,要勤練習(xí),學(xué)以致用堵第。 文字導(dǎo)圖一張吧凉,...
    果凍不耐凍閱讀 144評論 0 0
  • Tablelayout以行和列的形式對控件進行管理,每一行為一個TableRow對象踏志。當(dāng)為TableRow對象時阀捅,...
    a紫星閱讀 1,487評論 3 2
  • 2016年6月20號,我最后一天在自己的崗位上工作针余。沒有和你說感謝饲鄙,沒有和你說再見,我離開了圆雁。不必沮喪忍级,無需失落,...
    不回頭的姑涼閱讀 269評論 0 0