第一章 最小二乘法

tensorflow是机器学习中一个优秀的框架,这一系列的教程是通过实例来介绍tensorflow的使用。慢慢的对tensorflow的一些重要的概念进行介绍,把tensorflow的使用方法融入到各个算法中,而不是一味的去专门学习tensorflow的使用。

张量

tensorflow 可以分成两个词, 一个是tensor ,另外一个是flowtensor说的是数据模型, 把它叫做 张量flow 是一种流式的计算流行以后会慢慢的明白这种方式。

几种变量形式:

  • placeholder:占位符,通常是在训练的时候把数据喂给 他们。

  • Variable:求变量,我们在训练的过程中,就是为了求出这些值来。

  • constant:常变量,可以进行赋值计算等。

    >>> import tensorflow as tf
    >>> a = tf.constant([3],name="a")
    >>> b = tf.constant([4],name="b")
    >>> c = a + b
    >>> sess = tf.Session()
    >>> sess.run(c)
    array([7], dtype=int32)
    >>> print c
    Tensor("add_1:0", shape=(1,), dtype=int32)
    >>>
    

constant 的计算过程,特别注意的是要得到变量具体的值,需要创建一个上下文sess = tf.Session() 才能访问到变量的资源。

评价函数和梯度上升下降

评价函数是我们需要求解的方程,如在最小二乘法中评价函数为方差最小, 梯度下降:是我们求解问题的方式,简单的认为是,我们沿着坡就能达到山的波峰或者波谷,波峰或者波谷代表了评价函数的最大值或最小值。

最小二乘法评价函数用数学公式表示为:

$$\sum_{n=0}^N(y_i-a-bx_i)^2$$

tensorflow 表示为:

X = tf.placeholder("float")
Y = tf.placeholder("float")

W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")

pred = tf.add(tf.multiply(X, W), b)

cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)

使用梯度,求得最值:

optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

其中 learning_rate 为学习因子,代表梯度的步长。

代码如下所示:

import matplotlib
matplotlib.use('Agg')


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

rng = numpy.random

learning_rate = 0.01
training_epochs = 13
display_step = 1

train_X = numpy.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
                         7.042,10.791,5.313,7.997,5.654,9.27,3.1])
train_Y = numpy.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
                         2.827,3.465,1.65,2.904,2.42,2.94,1.3])
n_samples = train_X.shape[0]

X = tf.placeholder("float")
Y = tf.placeholder("float")

W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")

pred = tf.add(tf.multiply(X, W), b)

cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

init = tf.global_variables_initializer()

with tf.Session() as sess:

    sess.run(init)

    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X: x, Y: y})

        if (epoch+1) % display_step == 0 or epoch == 0:
            c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
                "W=", sess.run(W), "b=", sess.run(b))

        plt.figure()
            plt.title('step:' + str(epoch))
        plt.plot(train_X, train_Y, 'ro', label='Original data')
        plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
        plt.legend()
        out_png = '%d.png'%(epoch)
        plt.savefig(out_png, dpi=150)

    print("Optimization Finished!")
    training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
    print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')

求解的过程如下:

转载请标明来之:http://www.bugingcode.com/

更多教程:阿猫学编程


来自您的鼓励

如果您感觉这一系列的教程,读后有点收获,并对知识有点了解,不妨小额捐助我一下,让我有动力继续写出更多好文章;或者在评论区留下你的意见。

所有评论

写了这么多年博客,收到的优秀评论少之又少。在这个属于 SNS 的时代也并不缺少向作者反馈的渠道。

Isidro    2018-02-21 03:00:05 pm    回复   
I'd like to cancel a cheque what is an edex search
Vivus's Qsymia and Arena's Belviq have serious side effects,ranging from depression and anxiety to heart risks and potentialharm to fetuses. And while both involve signaling the brain, theexact way they work is unknown - a big concern to those worriedabout safety.
http //kraftbude.com erfahrungen The remarks suggest that Hollandeâs government may haveovercome opposition from one key player in the talks, which areaimed at eliminating the 20 billion-euro ($27 billion) deficitfacing the pension system in 2020 if no action is taken. Theyalso underline how the Socialist president is seeking to steerthe French economy toward improved competitiveness and growth.
when should you take vitalikor “Wh

Valentin    2018-02-21 02:59:56 pm    回复   
No, I'm not particularly sporty triple renal scan with lasix "Thieves have many ways to disguise the funds they move around the world. These include bulk cash smuggling, delayed deposits, heavy use of middlemen, shell companies and tax havens, bribery of bank officials, cycling cash through legitimate businesses and cash purchases of luxury goods," the report said.
beta sitosterol mega strength In 2011, 4.5 million households were living in "fuelpoverty", needing to spend more than 10 percent of their incomeon fuel to maintain an adequate level of warmth, according toBritain's Department of Energy and Climate Change (DECC). It isalmost certain that fuel poverty has increased since then asbills continue to rise more rapidly than incomes.
vigapro Asked what Boehner would do i

Abram    2018-02-21 02:59:49 pm    回复   
I've got a very weak signal he most commonly abused prescription drugs include Clinton's work with the family foundation has kept her ties to major donors active, as her family has hosted and planned fundraisers for the group in Washington, San Francisco, London, and New York's tony Bridgehampton. She also has helped raise money for Terry McAuliffe, a close friend running in a tight race for governor of Virginia.
using zyprexa and seroquel together House Minority Leader Rep. Nancy Pelosi echoed the sentiment, writing, âPrayers are w/ victims, their families, & all affected by #NavyYardShooting. Grateful for first responders who are working to secure area,â on the micro-blogging site.
vitaros europe
French sid

Kaden    2018-02-21 02:59:42 pm    回复   
Will I have to work shifts? order viagra 100mg online Aid agencies have estimated that up to two million people have died since the mid-1990s because of acute food shortages caused by natural disasters and economic mismanagement. The country relies on foreign aid to feed millions of its people.
ventolin hfa retail price His description of himself as an "overachiever" was illuminating, as well. Itâs what has propelled most of the talk of his beating Petersonâs still-shocking pace. Having surpassed every expectation for what he would be capable of as a rookie, Griffin makes a compelling case for exceeding medical standards, too.
singapore prescription drugs Sister Manal, the 47-year-old principal of the Franciscan school i

Giovanni    2018-02-21 02:59:36 pm    回复   
Withdraw cash how long does levitra last 20 mg The Tigersâ quintet would have joined Don Larsen and Roy Halladay as the only pitchers to throw a no-hitter in a postseason game. History might have eluded the Tigers, but they didnât mind as the victory gave them a 1-0 lead in the best-of-seven series, swiping home-field advantage away from the Red Sox. It was just the second time the Red Sox had been shut out at home in the postseason; the first was Game 5 of the 1918 World Series.
where can i buy bazooka pills in canada Imagine that; the NCAA had this information, got greedy and tried to get more information illegally, then had to backtrack and essentially kneecap itself after the public outcry when it was revealed the NCAA hired the attorney of the man they were investigating (Shapiro) to help investigate the very

Harris    2018-02-21 02:59:30 pm    回复   
Could you please repeat that? deu-apotheke.com In order to focus on this mission, Halsey will be winding down his commitments to Berlin, where his tremendous impact has been recognized with many polysyllabic awards. He makes fascinating comparisons between the choral traditions in Germany and Britain, both rooted in Protestant worship, but in other respects profoundly different. “The German musical scene is highly professionalized,” he explains. “There is much less amateur music-making than in Britain and much less music teaching in schools. Germans don’t have the equivalent of our network of cathedral and college choirs or our great choral societies, which are very much a phenomenon of the English-speaking world. British conservatoires make choral singing a compulsory element of their courses, but that’s not the case in Germany.’’

Mackenzie    2018-02-21 02:59:19 pm    回复   
I'll put him on deu-apotheke.com “That is the right thing to do. And the idea that you can — oh, I don’t know — declare yourself to have been more committed than anyone to improve our immigration system and then have nothing to show for it is a little laughable,” he said.
how long will 10mg of levitra last Since Xi became president in March, police have detained around 30 of those campaigning for officials to reveal their wealth, say rights groups. Among them is well-known legal activist Xu Zhiyong, whose case Zeya said she also raised.
priceline pharmacy griffith BEIJING/HONG KONG - China reiterated its opposition on Thursday to a European Union plan to limit airline carbon dioxide emi

Goodboy    2018-02-21 02:59:12 pm    回复   
I've been made redundant buy tretinoin cream singapore On this week's Daily News Fifth Yankees Podcast, Mark Feinsand sits down with Yankees first baseman Lyle Overbay to discuss the Yankees' offense, the lack of movement at the trade deadline and - of course - the ongoing A-Rod saga.
prescription drugs that can cause you to fail a drug test This comes from our ad serving technology and is used to track how many times you have seen a particular ad on our sites, so that you don't just see one advert but an even spread. This information is not used by us for any other type of audience recording or monitoring.
generic rosuvastatin uk
The Cardinal were in this position last year at Oregon. They had r

Lamont    2018-02-21 02:59:09 pm    回复   
What's the last date I can post this to to arrive in time for Christmas? handbook of pharmaceutical generic development vol 1 "What keeps me up at night is 'are we missing out oninvestments?' because if this is what I think it can be, we willregret not being even more active," said Micky Malka, founder ofPalo Alto, California-based Ribbit Capital, which has investedin three bitcoin firms, including San Francisco-based Coinbase.
telmisartan 40 mg tablets This is no cause for panic, as this was part of a routine surveillance program in the national park system.  According to the CDC, an average of 7 cases of plague are reported annually in the US. The disease is typically not lethal if it is treated in the early stages with antibiotics.
daivonex kaina Re-count

Melanie    2018-02-21 02:59:05 pm    回复   
I've been cut off tri levlen 28 He called the Yankeesâ 1996 championship his greatest moment. The 2009 title at the new Stadium was âvery gratifying.â He hoped heâd be remembered as âa great teammateâ and someone always willing to put in the necessary work. He praised Rivera, who has saved 72 of Pettitteâs wins.
how to trust online pharmacy "The security council must be prepared to act next week for the international community to stand up and speak out in the strongest possible terms about the importance of enforceable action to rid the world of Syria's chemical weapons," Kerry said. "The world is watching to see whether we can avert military action and achieve through peaceful means even more than what those military strikes promised."

撰写评论