TensorFlow™ 是Google开源的一个采用数据流图用于数值计算的开源库。截止到目前为止在github上已经获得超过6万个Star,已经成为深度学习和机器学习方面最为受欢迎的项目,炙手可热。这篇文章介绍一下如何安装tensorflow并使用其写下第一个程序。
版本信息
时间 | 版本信息 |
---|---|
2015年底 | Google开源了tensorflow |
2017年2月 | 发布了tensorflow的1.0版本 |
目前 | 稳定版本1.2,prerelease版本1.3 |
github项目信息
项目 | 详细信息 |
---|---|
URL | https://github.com/tensorflow/tensorflow |
安装方式
安装平台 | 安装步骤 |
---|---|
Ubuntu | https://www.tensorflow.org/install/install_linux |
Mac OS X | https://www.tensorflow.org/install/install_mac |
Windows | https://www.tensorflow.org/install/install_windows |
源码安装方式
方式 | 安装步骤 |
---|---|
源码方式 | https://www.tensorflow.org/install/install_sources |
通用Linux方式
方式 | 安装步骤 |
---|---|
linux | https://www.tensorflow.org/install/install_linux#determine_which_tensorflow_to_install |
简单来说,使用python-pip进行安装,一步即可。
安装日志
本文使用python-pip方式进行直接安装,执行日志如下:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
第一个例子程序
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
执行确认
- 1
- 2
- 3
- 4
- 5
- 6
- 7
我们可以看到,简单的信息输出和计算都是正常的运行,提示的信息就是告诉你如果你使用源码的方式进行编译的话可能会快一些,如果不想看到这些输出信息,可以在代码中加入如下信息设定LOG的级别即可
- 1
- 2
因为我们在Linux上,设定环境变量非常方便,可以如下即可:
- 1
- 2
- 3
- 4
- 5
- 6
42是什么
例子程序为什么是42,仔细查了一下发现一些很无聊又很有趣的事情。Douglas Adams写过一本著名的科幻小说”银河系漫游指南(The Hitchhiker’s Guide to the Galaxy)”,里面提到了一个终极问题,“the Answer to Life, the Universe and Everything is?”生命,宇宙和一切的答案是什么?那个非常厉害的计算机算了半天,回答就是42, 这已经是一个梗,被放到了无数的彩蛋之中,比如你google一下,你会发先google算的快的多了
关于为什么是42,很多人给出了不同的解释,比如程序员是这样说的:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
通配符*代表一切,它的ASCII是42.
更有不知道是认真还是无聊的人,整理了42个为什么是42的原因
项番 | 详细 |
---|---|
1 | Queen Victoria’s husband Prince Albert died aged 42; they had 42 grandchildren and their great-grandson, Edward VIII, abdicated at the age of 42. |
2 | The world’s first book printed with movable type is the Gutenberg Bible which has 42 lines per page. |
3 | On page 42 of Harry Potter and the Philosopher’s Stone, Harry discovers he’s a wizard. |
4 | The first time Douglas Adams essayed the number 42 was in a sketch called “The Hole in the Wall Club”. In it, comedian Griff Rhys Jones mentions the 42nd meeting of the Crawley and District Paranoid Society. |
5 | Lord Lucan’s last known location was outside 42 Norman Road, Newhaven, East Sussex. |
6 | The Doctor Who episode entitled “42” lasts for 42 minutes. |
7 | Titanic was travelling at a speed equivalent to 42km/hour when it collided with an iceberg. |
8 | The marine battalion 42 Commando insists that it be known as “Four two, Sir!” |
9 | In east Asia, including parts of China, tall buildings often avoid having a 42nd floor because of tetraphobia – fear of the number four because the words “four” and “death” sound the same (si or sei). Likewise, four 14, 24, etc. |
10 | Elvis Presley died at the age of 42. |
11 | BBC Radio 4’s Desert Island Discs was created in 1942. There are 42 guests per year. |
12 | Toy Story character Buzz Lightyear’s spaceship is named 42. |
13 | Fox Mulder’s apartment in the US TV series The X Files was number 42. |
14 | The youngest president of the United States,Theodore Roosevelt, was 42 when he was elected. |
15 | The office of Google’s chief executive Eric Schmidt is called Building 42 of the firm’s San Francisco complex. |
16 | The Bell-X1 rocket plane Glamorous Glennis piloted by Chuck Yeager, first broke the sound barrier at 42,000 feet. |
17 | The atomic bomb that devastated Nagasaki, Japan, contained the destructive power of 42 million sticks of dynamite. |
18 | A single Big Mac contains 42 per cent of the recommended daily intake of salt. |
19 | Cricket has 42 laws. |
20 | On page 42 of Bram Stoker’s Dracula, Jonathan Harker discovers he is a prisoner of the vampire. And on the same page of Frankenstein, Victor Frankenstein reveals he is able to create life. |
21 | In Shakespeare’s Romeo and Juliet, Friar Laurence gives Juliet a potion that allows for her to be in a death-like coma for “two and forty hours”. |
22 | The three best-selling music albums – Michael Jackson’s Thriller, AC/DC’s Back in Black and Pink Floyd’s The Dark Side of the Moon – last 42 minutes. |
23 | The result of the most famous game in English football – the world cup final of 1966 – was 4-2. |
24 | The type 42 vacuum tube was one of the most popular audio output amplifiers of the 1930s. |
25 | A marathon course is 42km and 195m. |
26 | Samuel Johnson compiled the Dictionary of the English Language, regarded as one of the greatest works of scholarship. In a nine-year period he defined a total of 42,777 words. |
27 | 42,000 balls were used at Wimbledon last year. |
28 | The wonder horse Nijinsky was 42 months old in 1970 when he became the last horse to win the English Triple Crown: the Derby; the 2000 Guineas and the St Leger. |
29 | The element molybdenum has the atomic number 42 and is also the 42nd most common element in the universe. |
30 | Dodi Fayed was 42 when he was killed alongside Princess Diana. |
31 | Cell 42 on Alcatraz Island was once home to Robert Stroud who was transferred to The Rock in 1942. After murdering a guard he spent 42 years in solitary confinement in different prisons. |
32 | In the Book of Revelation, it is prophesised that the beast will hold dominion over the earth for 42 months. |
33 | The Moorgate Tube disaster of 1975 killed 42 passengers. |
34 | When the growing numbers of Large Hadron Collider scientists acquired more office space recently, they named their new complex Building 42. |
35 | Lewis Carroll’s Alice’s Adventures in Wonderland has 42 illustrations. |
36 | 42 is the favourite number of Dr House, the American television doctor played by Hugh Laurie. |
37 | There are 42 US gallons in a barrel of oil. |
38 | In an episode of The Simpsons, police chief Wiggum wakes up to a question aimed at him and replies “42”. |
39 | Best Western is the world’s largest hotel chain with more than 4,200 hotels in 80 countries. |
40 | There are 42 principles of Ma’at, the ancient Egyptian goddess – and concept – of physical and moral law, order and truth. |
41 | Mungo Jerry’s 1970 hit “In the Summertime”, written by Ray Dorset, has a tempo of 42 beats per minute. |
42 | The band Level 42 chose their name in recognition of The Hitchhiker’s Guide to the Galaxy and not – as is often repeated – after the world’s tallest car park. |
这样的例子无穷无尽,直到Douglas Adams在1993年正式回答之后,依然后很多人认为Douglas Adams其实不是那样想的,这个42有深层的含义。Douglas Adams这样说:
- 1
vim的彩蛋
突然想起来很久以前使用vim时发现的一个彩蛋,调出来之后一看,果然是这个梗(:help 42),只是当时读书太少,没有能理解埋彩蛋的人的心情,真是太不应该了。
为什么生命,宇宙和一切的答案是42。很不幸的是,唯一真正知道这个答案的人Douglas Adams已经死了。现在你可能开始要考虑死亡的意义是什么了…
我们程序员看来还真是前赴后继比较有趣的一个群体。
为什么要死,当然是让别人无法理解你埋得彩蛋是什么意思啦。你每天都能看到,每天又不知道为什么,闹不闹心,想不想知道,就不告诉你。
总结
我们花了一分钟学习和安装了一下tensorflow,一分钟做了一个简单的例子,10分钟去理解了一下42到底是什么这样一个关于生命和宇宙答案的哲学问题。
参考文献
https://github.com/tensorflow/tensorflow/issues/7778
https://www.tensorflow.org/
https://www.tensorflow.org/get_started/
https://www.zhihu.com/question/19846530
- 本文已收录于以下专栏:
- TensorFlow入门教程