Chapter 01
Your Deep Learning Journey
#hide
! [ -e /content ] && pip install -Uqq fastbook
import fastbook
fastbook.setup_book()#hide
from fastbook import *Your Deep Learning Journey
你的深度学习之旅
Hello, and thank you for letting us join you on your deep learning journey, however far along that you may be! In this chapter, we will tell you a little bit more about what to expect in this book, introduce the key concepts behind deep learning, and train our first models on different tasks. It doesn't matter if you don't come from a technical or a mathematical background (though it's okay if you do too!); we wrote this book to make deep learning accessible to as many people as possible.
您好,感谢您让我们加入您的深度学习之旅!在本章中,我们将告诉您更多关于本书的内容,介绍深度学习背后的关键概念,并就不同的任务训练我们的第一个模型。不管你是否有技术或数学背景(如果你也有,也没关系);我们写这本书是为了让尽可能多的人都能接触到深度学习。
Deep Learning Is for Everyone
深度学习适合每一个人
A lot of people assume that you need all kinds of hard-to-find stuff to get great results with deep learning, but as you'll see in this book, those people are wrong. <> is a list of a few thing you absolutely don't need to do world-class deep learning.
.What you don't need to do deep learning
[options="header"]
|======
| Myth (don't need) | Truth
| Lots of math | Just high school math is sufficient
| Lots of data | We've seen record-breaking results with <50 items of data
| Lots of expensive computers | You can get what you need for state of the art work for free
|======Deep learning is a computer technique to extract and transform data–-with use cases ranging from human speech recognition to animal imagery classification–-by using multiple layers of neural networks. Each of these layers takes its inputs from previous layers and progressively refines them. The layers are trained by algorithms that minimize their errors and improve their accuracy. In this way, the network learns to perform a specified task. We will discuss training algorithms in detail in the next section.
很多人认为你需要各种各样的难找的东西才能通过深度学习获得好的结果,但正如你在本书中看到的,这些人错了。<>列出了一些即使是世界级的深度学习也绝对不需要做的事情。
.What you don't need to do deep learning
[options="header"]
|======
| Myth (don't need) | Truth
| Lots of math | Just high school math is sufficient
| Lots of data | We've seen record-breaking results with <50 items of data
| Lots of expensive computers | You can get what you need for state of the art work for free
|======深度学习是一种利用多层神经网络提取和转换数据的计算机技术,使用的用例从人类语音识别到动物图像分类。这些层中的每一层都从之前的层中获取其输入,并逐步细化它们。层通过算法进行训练,以最小化其误差并提高其精度。通过这种方式,网络学习执行指定的任务。我们将在下一节详细讨论训练算法。
Deep learning has power, flexibility, and simplicity. That's why we believe it should be applied across many disciplines. These include the social and physical sciences, the arts, medicine, finance, scientific research, and many more. To give a personal example, despite having no background in medicine, Jeremy started Enlitic, a company that uses deep learning algorithms to diagnose illness and disease. Within months of starting the company, it was announced that its algorithm could identify malignant tumors more accurately than radiologists.
Here's a list of some of the thousands of tasks in different areas at which deep learning, or methods heavily using deep learning, is now the best in the world:
- Natural language processing (NLP):: Answering questions; speech recognition; summarizing documents; classifying documents; finding names, dates, etc. in documents; searching for articles mentioning a concept
- Computer vision:: Satellite and drone imagery interpretation (e.g., for disaster resilience); face recognition; image captioning; reading traffic signs; locating pedestrians and vehicles in autonomous vehicles
- Medicine:: Finding anomalies in radiology images, including CT, MRI, and X-ray images; counting features in pathology slides; measuring features in ultrasounds; diagnosing diabetic retinopathy
- Biology:: Folding proteins; classifying proteins; many genomics tasks, such as tumor-normal sequencing and classifying clinically actionable genetic mutations; cell classification; analyzing protein/protein interactions
- Image generation:: Colorizing images; increasing image resolution; removing noise from images; converting images to art in the style of famous artists
- Recommendation systems:: Web search; product recommendations; home page layout
- Playing games:: Chess, Go, most Atari video games, and many real-time strategy games
- Robotics:: Handling objects that are challenging to locate (e.g., transparent, shiny, lacking texture) or hard to pick up
- Other applications:: Financial and logistical forecasting, text to speech, and much more...
深度学习具有强大的能力、灵活性和简单性。这就是为什么我们认为它应该应用于许多学科。这些学科包括社会科学和物理科学、艺术、医学、金融、科学研究等等。举个个人例子,尽管杰里米没有医学背景,但他创办了Enlitic,这是一家使用深度学习算法诊断疾病的公司。该公司成立几个月后,就宣布其算法可以比放射科医生更准确地识别恶性肿瘤。
下面列出了不同领域的数千项任务中的一些,深度学习或大量使用深度学习的方法目前是世界上最好的:
- 自然语言处理(NLP):回答问题;语音识别总结文件;对文件进行分类;在文件中查找名称、日期等;搜索提及概念的文章
- 计算机视觉:卫星和无人机图像判读(例如,抗灾能力);人脸识别;图像字幕;阅读交通标志;自动驾驶车辆中的行人和车辆定位
- 医学:在放射学图像中发现异常,包括CT、MRI和X射线图像;病理切片中的计数特征;测量超声波特征;糖尿病视网膜病变的诊断
- 生物学:折叠蛋白质;蛋白质分类;许多基因组学任务,如肿瘤正常测序和分类临床可操作的基因突变;细胞分类;分析蛋白质/蛋白质相互作用
- 图像生成:图像着色;提高图像分辨率;从图像中去除噪声;以著名艺术家的风格将图像转化为艺术
- 推荐系统:网络搜索;产品推荐;主页布局
- 玩游戏:国际象棋、围棋、大多数雅达利电子游戏和许多实时战略游戏
- 机器人技术:处理难以定位(例如,透明、有光泽、缺乏纹理)或难以拾取的对象
- 其他应用程序:金融和物流预测,文本到语音等
What is remarkable is that deep learning has such varied application yet nearly all of deep learning is based on a single type of model, the neural network.
But neural networks are not in fact completely new. In order to have a wider perspective on the field, it is worth it to start with a bit of history.
Neural Networks: A Brief History
神经网络简史
In 1943 Warren McCulloch, a neurophysiologist, and Walter Pitts, a logician, teamed up to develop a mathematical model of an artificial neuron. In their paper "A Logical Calculus of the Ideas Immanent in Nervous Activity" they declared that:
: Because of the “all-or-none” character of nervous activity, neural events and the relations among them can be treated by means of propositional logic. It is found that the behavior of every net can be described in these terms.
1943年,神经生理学专家沃伦·麦卡洛克和逻辑学家沃尔特·皮茨合作开发了人工神经元的数学模型。在他们的论文《神经活动中内在思想的逻辑演算》中,他们宣称:
: 由于神经活动的“全有或全无”特征,神经事件及其相互关系可以用命题逻辑来处理。发现每个网络的行为都可以用这些术语来描述。
McCulloch and Pitts realized that a simplified model of a real neuron could be represented using simple addition and thresholding, as shown in <>. Pitts was self-taught, and by age 12, had received an offer to study at Cambridge University with the great Bertrand Russell. He did not take up this invitation, and indeed throughout his life did not accept any offers of advanced degrees or positions of authority. Most of his famous work was done while he was homeless. Despite his lack of an officially recognized position and increasing social isolation, his work with McCulloch was influential, and was taken up by a psychologist named Frank Rosenblatt.
McCulloch和Pitts认识到,真实神经元的简化模型可以使用简单的加法和阈值来表示,如<>所示。皮特是自学成才的,到12岁时,他收到了一份邀请,和伟大的伯特兰·罗素一起在剑桥大学学习。他没有接受这一邀请,事实上,在他的一生中,他没有接受任何高级学位或权威职位的邀请。他的大部分著名作品都是在他无家可归时完成的。尽管他没有一个官方认可的职位,社会与世隔绝程度也越来越高,但他与麦卡洛克的工作颇具影响力,并被一位名叫弗兰克·罗森布拉特的心理学家所接手。

Rosenblatt further developed the artificial neuron to give it the ability to learn. Even more importantly, he worked on building the first device that actually used these principles, the Mark I Perceptron. In "The Design of an Intelligent Automaton" Rosenblatt wrote about this work: "We are now about to witness the birth of such a machine–-a machine capable of perceiving, recognizing and identifying its surroundings without any human training or control." The perceptron was built, and was able to successfully recognize simple shapes.
An MIT professor named Marvin Minsky (who was a grade behind Rosenblatt at the same high school!), along with Seymour Papert, wrote a book called Perceptrons (MIT Press), about Rosenblatt's invention. They showed that a single layer of these devices was unable to learn some simple but critical mathematical functions (such as XOR). In the same book, they also showed that using multiple layers of the devices would allow these limitations to be addressed. Unfortunately, only the first of these insights was widely recognized. As a result, the global academic community nearly entirely gave up on neural networks for the next two decades.
罗森布拉特进一步开发了人工神经元,赋予其学习能力。更重要的是,他致力于构建第一个实际使用这些原理的设备,即Mark I感知机。在《智能自动机的设计》一书中,罗森布拉特写到了这项工作:“我们现在即将见证这样一台机器的诞生——一台能够感知、识别和识别周围环境而无需任何人类训练或控制的机器。”感知机已经制造出来,并能够成功地识别简单的形状。
麻省理工学院教授马文·明斯基(Marvin Minsky,在同一所高中的成绩落后于罗森布拉特!),与西摩·帕普特(Seymour Papert)一起,他写了一本名为《感知机》(麻省理工学院出版社)的书,讲述了罗森布拉特的发明。他们表明,这些设备的单层无法学习一些简单但关键的数学函数(如XOR)。在同一本书中,他们还展示了使用多层设备可以解决这些限制。不幸的是,这些见解中只有第一个得到了广泛认可。因此,在接下来的二十年里,全球学术界几乎完全放弃了神经网络。
Perhaps the most pivotal work in neural networks in the last 50 years was the multi-volume Parallel Distributed Processing (PDP) by David Rumelhart, James McClellan, and the PDP Research Group, released in 1986 by MIT Press. Chapter 1 lays out a similar hope to that shown by Rosenblatt:
: People are smarter than today's computers because the brain employs a basic computational architecture that is more suited to deal with a central aspect of the natural information processing tasks that people are so good at. ...We will introduce a computational framework for modeling cognitive processes that seems… closer than other frameworks to the style of computation as it might be done by the brain.
The premise that PDP is using here is that traditional computer programs work very differently to brains, and that might be why computer programs had been (at that point) so bad at doing things that brains find easy (such as recognizing objects in pictures). The authors claimed that the PDP approach was "closer than other frameworks" to how the brain works, and therefore it might be better able to handle these kinds of tasks.
In fact, the approach laid out in PDP is very similar to the approach used in today's neural networks. The book defined parallel distributed processing as requiring:
- A set of processing units
- A state of activation
- An output function for each unit
- A pattern of connectivity among units
- A propagation rule for propagating patterns of activities through the network of connectivities
- An activation rule for combining the inputs impinging on a unit with the current state of that unit to produce an output for the unit
- A learning rule whereby patterns of connectivity are modified by experience
- An environment within which the system must operate
We will see in this book that modern neural networks handle each of these requirements.
In the 1980's most models were built with a second layer of neurons, thus avoiding the problem that had been identified by Minsky and Papert (this was their "pattern of connectivity among units," to use the framework above). And indeed, neural networks were widely used during the '80s and '90s for real, practical projects. However, again a misunderstanding of the theoretical issues held back the field. In theory, adding just one extra layer of neurons was enough to allow any mathematical function to be approximated with these neural networks, but in practice such networks were often too big and too slow to be useful.
Although researchers showed 30 years ago that to get practical good performance you need to use even more layers of neurons, it is only in the last decade that this principle has been more widely appreciated and applied. Neural networks are now finally living up to their potential, thanks to the use of more layers, coupled with the capacity to do so due to improvements in computer hardware, increases in data availability, and algorithmic tweaks that allow neural networks to be trained faster and more easily. We now have what Rosenblatt promised: "a machine capable of perceiving, recognizing, and identifying its surroundings without any human training or control."
This is what you will learn how to build in this book. But first, since we are going to be spending a lot of time together, let's get to know each other a bit…
在过去50年中,神经网络中最关键的工作可能是David Rumelhart、James McClellan和PDP研究小组于1986年发表的多卷并行分布式处理(PDP),由麻省理工学院出版社出版。第一章提出了与罗森布拉特类似的希望:
:人们比今天的计算机更聪明,因为大脑采用了一种基本的计算架构,更适合处理人们非常擅长的自然信息处理任务的中心方面,我们将介绍一个模拟认知过程的计算框架,它似乎比其他框架更接近于大脑可能进行的计算方式。
PDP在这里使用的前提是,传统的计算机程序与大脑的工作方式非常不同,这可能就是为什么计算机程序(当时)在做大脑认为容易做的事情(如识别图片中的对象)方面表现得如此糟糕的原因。作者声称,PDP方法“比其他框架更接近”大脑的工作方式,因此它可能更好地处理此类任务。
事实上,PDP中提出的方法与当今神经网络中使用的方法非常相似。本书将并行分布式处理定义为需要:
- 一组处理单元
- 激活状态
- 每个单元的输出函数
- 单元之间的连接模式
- 通过连接网络传播活动模式的传播规则
- 一种激活规则,用于将影响单元的输入与该单元的当前状态相结合,以产生该单元的输出
- 通过经验修改连接模式的学习规则
- 系统必须在其中运行的环境
我们将在本书中看到,现代神经网络可以处理这些需求。
在20世纪80年代,大多数模型都是用第二层神经元构建的,因此避免了明斯基和帕普特发现的问题(使用上述框架,这是他们的“单元间连接模式”)。事实上,神经网络在80年代和90年代被广泛用于实际项目。然而,对理论问题的误解再次阻碍了该领域的发展。理论上,仅仅增加一层神经元就足以让任何数学函数都可以用这些神经网络近似,但在实践中,这样的网络往往太大、太慢而没有用处。
虽然研究人员在30年前表明,要获得实际的良好性能,需要使用更多的神经元层,但直到最近十年,这一原理才得到更广泛的重视和应用。由于使用了更多的层,再加上计算机硬件的改进、数据可用性的提高和算法调整,神经网络现在终于发挥了它们的潜力,使神经网络能够更快、更容易地训练。我们现在有了罗森布拉特承诺的东西:“一台能够感知、识别和识别周围环境的机器,无需任何人类训练或控制。”
这就是你将在本书中学习如何构建的内容。但首先,因为我们要花很多时间在一起,让我们互相了解一下…
Who We Are
我们是谁
We are Sylvain and Jeremy, your guides on this journey. We hope that you will find us well suited for this position.
Jeremy has been using and teaching machine learning for around 30 years. He started using neural networks 25 years ago. During this time, he has led many companies and projects that have machine learning at their core, including founding the first company to focus on deep learning and medicine, Enlitic, and taking on the role of President and Chief Scientist of the world's largest machine learning community, Kaggle. He is the co-founder, along with Dr. Rachel Thomas, of fast.ai, the organization that built the course this book is based on.
From time to time you will hear directly from us, in sidebars like this one from Jeremy:
我们是Sylvain和Jeremy,你们这趟旅程的向导。我们希望您会发现我们非常适合这个职位。
Jeremy已经使用和教授机器学习大约30年了。他25年前开始使用神经网络。在此期间,他领导了许多以机器学习为核心的公司和项目,包括成立了第一家专注于深度学习和医学的公司Enlitic,并担任世界最大机器学习社区Kaggle的总裁兼首席科学家。他和Rachel Thomas博士是fast.ai的联合创始人,也就是这本书所基于的课程的组织。
不时你会直接从我们的边栏中听到杰里米的话:
J: Hi everybody, I'm Jeremy! You might be interested to know that I do not have any formal technical education. I completed a BA, with a major in philosophy, and didn't have great grades. I was much more interested in doing real projects, rather than theoretical studies, so I worked full time at a management consulting firm called McKinsey and Company throughout my university years. If you're somebody who would rather get their hands dirty building stuff than spend years learning abstract concepts, then you will understand where I am coming from! Look out for sidebars from me to find information most suited to people with a less mathematical or formal technical background—that is, people like me…
J:大家好,我是Jeremy!你可能对于我没有受过正规的技术教育很感兴趣。我获得了哲学专业的学士学位,但成绩并不好。我更感兴趣的是做真实的项目,而不是理论研究,所以我在大学期间一直在一家名为麦肯锡公司的管理咨询公司全职工作。如果你是一个宁愿把自己的手弄脏,也不愿花数年时间学习抽象概念的人,那么你就会明白我来自何方!注意我的侧边栏,找到最适合数学或技术背景较低的人的信息,即像我这样的人…
Sylvain, on the other hand, knows a lot about formal technical education. In fact, he has written 10 math textbooks, covering the entire advanced French maths curriculum!
另一方面,Sylvain对正规技术教育了解很多。事实上,他已经编写了10本数学教科书,涵盖了整个法国高等数学课程!
S: Unlike Jeremy, I have not spent many years coding and applying machine learning algorithms. Rather, I recently came to the machine learning world, by watching Jeremy's fast.ai course videos. So, if you are somebody who has not opened a terminal and written commands at the command line, then you will understand where I am coming from! Look out for sidebars from me to find information most suited to people with a more mathematical or formal technical background, but less real-world coding experience—that is, people like me…
S:与Jeremy不同,我没有花很多年时间编写和应用机器学习算法。相反,我是最近才来到了机器学习世界,观看了杰里米的fast.ai课程视频。因此,如果您没有打开过终端并在命令行中编写命令,那么您将理解我的来历!请注意我的侧边栏,以找到最适合具有更多数学或技术背景,但较少实际编码经验的人的信息。也就是说,像我这样的人…
The fast.ai course has been studied by hundreds of thousands of students, from all walks of life, from all parts of the world. Sylvain stood out as the most impressive student of the course that Jeremy had ever seen, which led to him joining fast.ai, and then becoming the coauthor, along with Jeremy, of the fastai software library.
All this means that between us you have the best of both worlds: the people who know more about the software than anybody else, because they wrote it; an expert on math, and an expert on coding and machine learning; and also people who understand both what it feels like to be a relative outsider in math, and a relative outsider in coding and machine learning.
Anybody who has watched sports knows that if you have a two-person commentary team then you also need a third person to do "special comments." Our special commentator is Alexis Gallagher. Alexis has a very diverse background: he has been a researcher in mathematical biology, a screenplay writer, an improv performer, a McKinsey consultant (like Jeremy!), a Swift coder, and a CTO.
来自世界各地各行各业的数十万学生学习了fast.ai课程。Sylvain是Jeremy所见过的这门课程中最令人印象深刻的学生,这使他加入了fast.ai然后与Jeremy一起成为fastai软件库的合著者。
所有这一切意味着,在我们之间,你拥有两个世界中最好的:那些比任何人都更了解软件的人,因为他们编写了软件;数学专家、编码和机器学习专家;还有那些既懂数学,又懂编码和机器学习的人。
任何看过体育比赛的人都知道,如果你有一个两人的评论团队,那么你还需要第三人来做“特别评论”。我们的特别评论员是Alexis Gallagher。Alexis的背景非常多样化:他曾是数学生物学研究员、编剧、即兴表演家、麦肯锡顾问(比如Jeremy!),一个Swift的程序员和首席技术官。
A: I've decided it's time for me to learn about this AI stuff! After all, I've tried pretty much everything else… But I don't really have a background in building machine learning models. Still… how hard can it be? I'm going to be learning throughout this book, just like you are. Look out for my sidebars for learning tips that I found helpful on my journey, and hopefully you will find helpful too.
A:我决定是时候学习这些人工智能的东西了! 毕竟,我已经尝试了几乎所有其他的东西。毕竟,我已经尝试了几乎所有其他的东西 ...... 但我并没有建立机器学习模型的真正背景。但是,...... 这能有多难?我将在本书中不断学习,就像你会一样。请留意我的侧边栏,了解我一路走来发现的有用的学习技巧,我希望你也会发现它的帮助。
How to Learn Deep Learning
如何学习深度学习
Harvard professor David Perkins, who wrote Making Learning Whole (Jossey-Bass), has much to say about teaching. The basic idea is to teach the whole game. That means that if you're teaching baseball, you first take people to a baseball game or get them to play it. You don't teach them how to wind twine to make a baseball from scratch, the physics of a parabola, or the coefficient of friction of a ball on a bat.
Paul Lockhart, a Columbia math PhD, former Brown professor, and K-12 math teacher, imagines in the influential essay "A Mathematician's Lament" a nightmare world where music and art are taught the way math is taught. Children are not allowed to listen to or play music until they have spent over a decade mastering music notation and theory, spending classes transposing sheet music into a different key. In art class, students study colors and applicators, but aren't allowed to actually paint until college. Sound absurd? This is how math is taught–-we require students to spend years doing rote memorization and learning dry, disconnected fundamentals that we claim will pay off later, long after most of them quit the subject.
Unfortunately, this is where many teaching resources on deep learning begin–-asking learners to follow along with the definition of the Hessian and theorems for the Taylor approximation of your loss functions, without ever giving examples of actual working code. We're not knocking calculus. We love calculus, and Sylvain has even taught it at the college level, but we don't think it's the best place to start when learning deep learning!
In deep learning, it really helps if you have the motivation to fix your model to get it to do better. That's when you start learning the relevant theory. But you need to have the model in the first place. We teach almost everything through real examples. As we build out those examples, we go deeper and deeper, and we'll show you how to make your projects better and better. This means that you'll be gradually learning all the theoretical foundations you need, in context, in such a way that you'll see why it matters and how it works.
So, here's our commitment to you. Throughout this book, we will follow these principles:
- Teaching the whole game. We'll start by showing how to use a complete, working, very usable, state-of-the-art deep learning network to solve real-world problems, using simple, expressive tools. And then we'll gradually dig deeper and deeper into understanding how those tools are made, and how the tools that make those tools are made, and so on…
- Always teaching through examples. We'll ensure that there is a context and a purpose that you can understand intuitively, rather than starting with algebraic symbol manipulation.
- Simplifying as much as possible. We've spent years building tools and teaching methods that make previously complex topics very simple.
- Removing barriers. Deep learning has, until now, been a very exclusive game. We're breaking it open, and ensuring that everyone can play.
哈佛大学教授大卫-帕金斯(David Perkins)写了《让学习成为整体》(Jossey-Bass)一书,他对教学有很多话要说。其基本思想是教授整个游戏。这意味着,如果你要教棒球,你首先要带人们去看棒球比赛或让他们玩棒球。你不会教他们如何用麻绳从头开始做一个棒球,也不会教他们抛物线的物理学原理,或者球在球棒上的摩擦系数。
哥伦比亚大学数学博士、前布朗大学教授、K-12数学教师保罗-洛克哈特(Paul Lockhart)在一篇颇具影响力的文章《一个数学家的悲歌》中想象了一个噩梦般的世界,在那里,音乐和艺术的教学方式与数学的教学方式相同。孩子们在花了十几年的时间掌握音乐符号和理论之前,不允许听或玩音乐,要花时间把乐谱换成不同的调。在艺术课上,学生们学习颜色和涂抹器,但直到大学才被允许真正作画。听起来很荒唐?这就是数学的教学方式--我们要求学生花数年时间死记硬背,学习枯燥无味、互不相干的基础知识,我们声称这些知识以后会有回报,而且是在他们中的大多数人退出这个学科之后很久。
不幸的是,这就是许多关于深度学习的教学资源开始的地方--要求学习者跟随Hessian的定义和损失函数的泰勒近似定理,而没有给出实际工作代码的例子。我们不是在嘲笑微积分。我们喜欢微积分,Sylvain甚至在大学里也教过它,但我们认为在学习深度学习时,它并不是最好的开始。
在深度学习中,如果你有动力去修正你的模型,让它做得更好,这真的很有帮助。这就是你开始学习相关理论的时候。但你首先需要有模型。我们几乎所有的东西都是通过真实的例子来教的。随着我们建立起这些例子,我们会越来越深入,我们会告诉你如何使你的项目越来越好。这意味着你将逐步学习你所需要的所有理论基础,在上下文中,你会看到为什么它很重要,它是如何工作的。
因此,这里是我们对你的承诺。在本书中,我们将遵循这些原则。
- 教授整个游戏。我们将从展示如何使用一个完整的、可操作的、非常可用的、最先进的深度学习网络开始,使用简单的、可表达的工具来解决现实世界的问题。然后我们会逐渐深入了解这些工具是如何制造的,以及制造这些工具的工具是如何制造的,等等...
- 总是通过实例进行教学。我们会确保有一个你能直观理解的背景和目的,而不是从代数符号的操作开始。
- 尽可能地简化。我们花了数年时间建立工具和教学方法,使以前复杂的主题变得非常简单。
- 消除障碍。到目前为止,深度学习一直是一个非常独特的游戏。我们正在打破它,并确保每个人都能玩。
The hardest part of deep learning is artisanal: how do you know if you've got enough data, whether it is in the right format, if your model is training properly, and, if it's not, what you should do about it? That is why we believe in learning by doing. As with basic data science skills, with deep learning you only get better through practical experience. Trying to spend too much time on the theory can be counterproductive. The key is to just code and try to solve problems: the theory can come later, when you have context and motivation.
There will be times when the journey will feel hard. Times where you feel stuck. Don't give up! Rewind through the book to find the last bit where you definitely weren't stuck, and then read slowly through from there to find the first thing that isn't clear. Then try some code experiments yourself, and Google around for more tutorials on whatever the issue you're stuck with is—often you'll find some different angle on the material might help it to click. Also, it's expected and normal to not understand everything (especially the code) on first reading. Trying to understand the material serially before proceeding can sometimes be hard. Sometimes things click into place after you get more context from parts down the road, from having a bigger picture. So if you do get stuck on a section, try moving on anyway and make a note to come back to it later.
Remember, you don't need any particular academic background to succeed at deep learning. Many important breakthroughs are made in research and industry by folks without a PhD, such as "Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks"—one of the most influential papers of the last decade—with over 5,000 citations, which was written by Alec Radford when he was an undergraduate. Even at Tesla, where they're trying to solve the extremely tough challenge of making a self-driving car, CEO Elon Musk says:
: A PhD is definitely not required. All that matters is a deep understanding of AI & ability to implement NNs in a way that is actually useful (latter point is what’s truly hard). Don’t care if you even graduated high school.
深度学习最难的是手工的部分:你怎么知道你是否有足够的数据,数据的格式是否正确,你的模型是否在正常训练,如果不正确,你应该怎么做?这就是为什么我们相信在实践中学习。就像基本的数据科学技能一样,对于深度学习,你只有通过实践经验才能变得更好。试图在理论上花费太多时间可能会适得其反。关键是只需编写代码并尝试解决问题:理论可以在以后,当你有背景和动机的时候再来研究。
会有一些时候,这段旅程会感觉很艰难。有的时候,你会觉得被卡住了。不要放弃! 倒回去看书,找到最后一点你肯定没有卡住的地方,然后从那里慢慢读下去,找到第一个不清楚的地方。然后自己尝试一些代码实验,并在谷歌上搜索更多关于你被卡住的问题的教程--通常你会发现对材料的一些不同的角度可能会帮助它成功。另外,在第一次阅读时,不理解所有的东西(尤其是代码)是可以预期的,也是正常的。试图在继续学习之前连续地理解材料有时是很难的。有时,当你从下一阶段的部分内容中获得更多的上下文,从更大的画面中获得更多的东西后,事情就会水到渠成。因此,如果你确实在某一节上卡住了,无论如何都要继续前进,并记下以后再来找它。
记住,你不需要任何特定的学术背景就能在深度学习方面取得成功。许多重要的突破是由没有博士学位的人在研究和行业中取得的,例如 "用深度卷积生成对抗网络进行无监督表征学习"--过去十年中最有影响力的论文之一,被引用超过5000次,这是由Alec Radford在他还是本科生时写的。即使是在特斯拉,他们也在努力解决制造自动驾驶汽车这一极其艰难的挑战,首席执行官埃隆-马斯克说:
:博士学位绝对不是必需的。重要的是对人工智能的深刻理解,以及以真正有用的方式实现NNs的能力(后一点才是真正困难的)。这与你是否高中毕业无关。
What you will need to do to succeed however is to apply what you learn in this book to a personal project, and always persevere.
然而,你需要做的是将你在本书中学到的东西应用到个人项目中,并始终坚持下去,才能取得成功。
Your Projects and Your Mindset
你的项目和你的思维方式
Whether you're excited to identify if plants are diseased from pictures of their leaves, auto-generate knitting patterns, diagnose TB from X-rays, or determine when a raccoon is using your cat door, we will get you using deep learning on your own problems (via pre-trained models from others) as quickly as possible, and then will progressively drill into more details. You'll learn how to use deep learning to solve your own problems at state-of-the-art accuracy within the first 30 minutes of the next chapter! (And feel free to skip straight there now if you're dying to get coding right away.) There is a pernicious myth out there that you need to have computing resources and datasets the size of those at Google to be able to do deep learning, but it's not true.
So, what sorts of tasks make for good test cases? You could train your model to distinguish between Picasso and Monet paintings or to pick out pictures of your daughter instead of pictures of your son. It helps to focus on your hobbies and passions–-setting yourself four or five little projects rather than striving to solve a big, grand problem tends to work better when you're getting started. Since it is easy to get stuck, trying to be too ambitious too early can often backfire. Then, once you've got the basics mastered, aim to complete something you're really proud of!
无论你是想通过植物的叶子图片来识别植物是否有病,自动生成针织图案,通过X射线诊断肺结核,还是确定浣熊何时使用你的猫门,我们都会让你尽快在自己的问题上使用深度学习(通过别人的预训练模型),然后会逐步钻研更多的细节。在下一章的前30分钟内,你将学会如何使用深度学习来解决你自己的问题,并达到最先进的精度 (如果你很想马上开始编程的话,可以直接跳过这里)。外面有一个害人的迷思,说你需要拥有像谷歌那样的计算资源和数据集才能进行深度学习,但这不是真的。
那么,什么样的任务才是好的测试案例呢?你可以训练你的模型来区分毕加索和莫奈的画,或者挑选出你女儿的照片而不是你儿子的照片。专注于你的爱好和激情是有帮助的--为自己设定四五个小项目,而不是努力解决一个大的、宏大的问题,这在你开始的时候往往效果更好。因为很容易被卡住,过早地尝试过于雄心勃勃往往会适得其反。然后,一旦你掌握了基础知识,就以完成你真正引以为豪的东西为目标!
J: Deep learning can be set to work on almost any problem. For instance, my first startup was a company called FastMail, which provided enhanced email services when it launched in 1999 (and still does to this day). In 2002 I set it up to use a primitive form of deep learning, single-layer neural networks, to help categorize emails and stop customers from receiving spam.
J:深度学习可以被设置在几乎任何问题上。例如,我的第一家创业公司是一家名为FastMail的公司,该公司在1999年推出时提供了强化的电子邮件服务(直到今天仍然如此)。2002年,我将其设定为使用深度学习的原始形式,即单层神经网络,以帮助对电子邮件进行分类并阻止客户收到垃圾邮件。
Common character traits in the people that do well at deep learning include playfulness and curiosity. The late physicist Richard Feynman is an example of someone who we'd expect to be great at deep learning: his development of an understanding of the movement of subatomic particles came from his amusement at how plates wobble when they spin in the air.
在深度学习方面表现出色的人的共同性格特征包括玩心和好奇心。已故物理学家理查德-费曼(Richard Feynman)是我们期望在深度学习方面表现出色的人的一个例子:他对亚原子粒子运动的理解的发展来自于他对盘子在空中旋转时如何摇晃的乐趣。
Let's now focus on what you will learn, starting with the software.
现在让我们把重点放在你将学到的东西上,首先是软件。
The Software: PyTorch, fastai, and Jupyter
软件:Pytorch, fastai和Jupyter
(And Why It Doesn't Matter)
We've completed hundreds of machine learning projects using dozens of different packages, and many different programming languages. At fast.ai, we have written courses using most of the main deep learning and machine learning packages used today. After PyTorch came out in 2017 we spent over a thousand hours testing it before deciding that we would use it for future courses, software development, and research. Since that time PyTorch has become the world's fastest-growing deep learning library and is already used for most research papers at top conferences. This is generally a leading indicator of usage in industry, because these are the papers that end up getting used in products and services commercially. We have found that PyTorch is the most flexible and expressive library for deep learning. It does not trade off speed for simplicity, but provides both.
PyTorch works best as a low-level foundation library, providing the basic operations for higher-level functionality. The fastai library is the most popular library for adding this higher-level functionality on top of PyTorch. It's also particularly well suited to the purposes of this book, because it is unique in providing a deeply layered software architecture (there's even a peer-reviewed academic paper about this layered API). In this book, as we go deeper and deeper into the foundations of deep learning, we will also go deeper and deeper into the layers of fastai. This book covers version 2 of the fastai library, which is a from-scratch rewrite providing many unique features.
我们已经完成了数百个机器学习项目,使用了几十个不同的软件包,以及许多不同的编程语言。在fast.ai,我们使用目前使用的大多数主要深度学习和机器学习软件包编写了课程。2017年PyTorch问世后,我们花了一千多个小时测试它,然后决定将它用于未来的课程、软件开发和研究。从那时起,PyTorch已经成为世界上增长最快的深度学习库,并且已经被用于顶级会议的大多数研究论文。这通常是行业使用情况的领先指标,因为这些论文最终会被用于产品和服务的商业化。我们发现,PyTorch是最灵活、最具表现力的深度学习库。它并没有以速度为代价换取简单性,而是同时提供了两者。
PyTorch作为一个低层次的基础库,为更高层次的功能提供基本操作,效果最好。fastai库是在PyTorch基础上添加这种高层次功能的最流行的库。它也特别适合本书的目的,因为它在提供深度分层的软件架构方面是独一无二的(甚至有一篇关于这种分层API的同行评审的学术论文)。在本书中,随着我们对深度学习的基础研究越来越深入,我们也将越来越深入地研究fastai的各个层次。本书涵盖了fastai库的第二版,它是一次从头开始的重写,提供了许多独特的功能。
However, it doesn't really matter what software you learn, because it takes only a few days to learn to switch from one library to another. What really matters is learning the deep learning foundations and techniques properly. Our focus will be on using code that clearly expresses the concepts that you need to learn. Where we are teaching high-level concepts, we will use high-level fastai code. Where we are teaching low-level concepts, we will use low-level PyTorch, or even pure Python code.
If it feels like new deep learning libraries are appearing at a rapid pace nowadays, then you need to be prepared for a much faster rate of change in the coming months and years. As more people enter the field, they will bring more skills and ideas, and try more things. You should assume that whatever specific libraries and software you learn today will be obsolete in a year or two. Just think about the number of changes in libraries and technology stacks that occur all the time in the world of web programming—a much more mature and slow-growing area than deep learning. We strongly believe that the focus in learning needs to be on understanding the underlying techniques and how to apply them in practice, and how to quickly build expertise in new tools and techniques as they are released.
然而,学习什么软件其实并不重要,因为从一个库切换到另一个库,只需要几天时间就能学会。真正重要的是正确学习深度学习的基础和技术。我们的重点将是使用明确表达你需要学习的概念的代码。在我们教授高级概念的地方,我们将使用高级的fastai代码。在教授低层次概念的地方,我们将使用低层次的PyTorch,甚至是纯Python代码。
如果现在感觉新的深度学习库正在快速出现,那么你需要做好准备,在未来几个月和几年里,变化的速度会更快。随着越来越多的人进入这个领域,他们会带来更多的技能和想法,并尝试更多的东西。你应该假定,无论你今天学习什么具体的库和软件,在一两年内就会被淘汰。试想一下,在网络编程的世界里,库和技术栈的变化数量一直在发生--这是一个比深度学习更成熟、发展更缓慢的领域。我们坚信,学习的重点需要放在理解基础技术和如何在实践中应用这些技术上,以及如何在新工具和技术发布时快速建立专业知识。
By the end of the book, you'll understand nearly all the code that's inside fastai (and much of PyTorch too), because in each chapter we'll be digging a level deeper to show you exactly what's going on as we build and train our models. This means that you'll have learned the most important best practices used in modern deep learning—not just how to use them, but how they really work and are implemented. If you want to use those approaches in another framework, you'll have the knowledge you need to do so if needed.
Since the most important thing for learning deep learning is writing code and experimenting, it's important that you have a great platform for experimenting with code. The most popular programming experimentation platform is called Jupyter. This is what we will be using throughout this book. We will show you how you can use Jupyter to train and experiment with models and introspect every stage of the data pre-processing and model development pipeline. Jupyter Notebook is the most popular tool for doing data science in Python, for good reason. It is powerful, flexible, and easy to use. We think you will love it!
在本书结束时,你将了解fastai(以及PyTorch的大部分)内的几乎所有代码,因为在每一章中,我们将深入挖掘,向你展示我们在构建和训练模型时到底发生了什么。这意味着你将学到现代深度学习中使用的最重要的最佳实践--不仅仅是如何使用它们,而是它们真正的工作和实现方式。如果你想在另一个框架中使用这些方法,你将拥有必要的知识来做到这一点。
由于学习深度学习最重要的是写代码和做实验,所以有一个很好的代码实验平台是很重要的。最流行的编程实验平台被称为Jupyter。这就是我们将在本书中使用的东西。我们将向你展示如何使用Jupyter来训练和实验模型,并对数据预处理和模型开发流的每个阶段进行反思。Jupyter笔记本是在Python中进行数据科学的最流行的工具,这是有原因的。它强大、灵活,而且易于使用。我们认为你会喜欢它!
Let's see it in practice and train our first model.
让我们在实践中看看,并训练我们的第一个模型。
Your First Model
你的第一个模型
As we said before, we will teach you how to do things before we explain why they work. Following this top-down approach, we will begin by actually training an image classifier to recognize dogs and cats with almost 100% accuracy. To train this model and run our experiments, you will need to do some initial setup. Don't worry, it's not as hard as it looks.
正如我们之前所说的,我们将在解释它们为什么有效之前教你如何做事情。按照这种自上而下的方法,我们将首先实际训练一个图像分类器,以几乎100%的准确率识别狗和猫。为了训练这个模型并运行我们的实验,你将需要做一些初始设置。别担心,这并不像看起来那么难。
s: Do not skip the setup part even if it looks intimidating at first, especially if you have little or no experience using things like a terminal or the command line. Most of that is actually not necessary and you will find that the easiest servers can be set up with just your usual web browser. It is crucial that you run your own experiments in parallel with this book in order to learn.
s: 不要跳过设置部分,即使它一开始看起来很吓人,特别是如果你很少或没有使用终端或命令行等东西的经验。实际上大部分都是没有必要的,你会发现最简单的服务器可以只用你常用的网络浏览器就能设置好。为了学习,你在阅读本书的同时进行自己的实验,这一点至关重要。
Getting a GPU Deep Learning Server
获取GPU深度学习服务器
To do nearly everything in this book, you'll need access to a computer with an NVIDIA GPU (unfortunately other brands of GPU are not fully supported by the main deep learning libraries). However, we don't recommend you buy one; in fact, even if you already have one, we don't suggest you use it just yet! Setting up a computer takes time and energy, and you want all your energy to focus on deep learning right now. Therefore, we instead suggest you rent access to a computer that already has everything you need preinstalled and ready to go. Costs can be as little as US$0.25 per hour while you're using it, and some options are even free.
要完成本书中的几乎所有内容,你需要使用一台带有NVIDIA GPU的计算机(不幸的是,其他品牌的GPU并不完全被主要的深度学习库所支持)。然而,我们并不建议你买一个;事实上,即使你已经有一个,我们也不建议你现在就使用它 设置一台电脑需要时间和精力,而你现在想把所有的精力都放在深度学习上。因此,我们建议你租用一台已经预装了所有你需要的东西并准备就绪的电脑。在你使用时,费用可以低至每小时0.25美元,有些选项甚至是免费的。
jargon: Graphics Processing Unit (GPU): Also known as a graphics card. A special kind of processor in your computer that can handle thousands of single tasks at the same time, especially designed for displaying 3D environments on a computer for playing games. These same basic tasks are very similar to what neural networks do, such that GPUs can run neural networks hundreds of times faster than regular CPUs. All modern computers contain a GPU, but few contain the right kind of GPU necessary for deep learning.
术语:图形处理单元(GPU)。也被称为显卡。计算机中一种特殊的处理器,可以同时处理成千上万的单一任务,特别是为在计算机上显示3D环境玩游戏而设计。这些相同的基本任务与神经网络的工作非常相似,GPU运行神经网络的速度比普通CPU快数百倍。所有现代计算机都包含一个GPU,但很少有人包含深度学习所需的正确类型的GPU。
The best choice of GPU servers to use with this book will change over time, as companies come and go and prices change. We maintain a list of our recommended options on the book's website, so go there now and follow the instructions to get connected to a GPU deep learning server. Don't worry, it only takes about two minutes to get set up on most platforms, and many don't even require any payment, or even a credit card, to get started.
A: My two cents: heed this advice! If you like computers you will be tempted to set up your own box. Beware! It is feasible but surprisingly involved and distracting. There is a good reason this book is not titled, Everything You Ever Wanted to Know About Ubuntu System Administration, NVIDIA Driver Installation, apt-get, conda, pip, and Jupyter Notebook Configuration. That would be a book of its own. Having designed and deployed our production machine learning infrastructure at work, I can testify it has its satisfactions, but it is as unrelated to modeling as maintaining an airplane is to flying one.
Each option shown on the website includes a tutorial; after completing the tutorial, you will end up with a screen looking like <<notebook_init>>.
随着公司的出现和价格的变化,与本书一起使用的GPU服务器的最佳选择将随着时间的推移而改变。我们在本书的网站上维护了一个我们推荐的选项列表,所以现在就去那里,按照说明连接到GPU深度学习服务器。别担心,在大多数平台上只需要两分钟就能设置好,而且许多平台甚至不需要任何付款,甚至不需要信用卡就能开始。
A:我的建议:听从这个建议! 如果你喜欢电脑,你会被吸引去配置你自己的电脑。请注意!它是可行的,但令人惊讶的是,它涉及到了很多问题,而且让人分心。这本书不叫《你想知道的关于Ubuntu系统管理、NVIDIA驱动程序安装、apt-get、conda、pip和Jupyter笔记本配置的一切》是有原因的。那将是一本属于自己的书。我在工作中设计并部署了我们的生产型机器学习基础设施,我可以证明它有其令人满意的地方,但它与建模的关系就像维护飞机与驾驶飞机一样不相关。
网站上显示的每个选项都包括一个教程;完成教程后,你最终会看到一个看起来像<<notebook_init>>的屏幕。

You are now ready to run your first Jupyter notebook!
现在你已经准备好运行你的第一个Jupyter notebook了。
jargon: Jupyter Notebook: A piece of software that allows you to include formatted text, code, images, videos, and much more, all within a single interactive document. Jupyter received the highest honor for software, the ACM Software System Award, thanks to its wide use and enormous impact in many academic fields and in industry. Jupyter Notebook is the software most widely used by data scientists for developing and interacting with deep learning models.
术语:Jupyter notebook。一款允许你在一个单一的交互式文档中包含格式化的文本、代码、图像、视频等等的软件。Jupyter获得了软件的最高荣誉--ACM软件系统奖,这得益于它在许多学术领域和工业界的广泛使用和巨大影响。Jupyter笔记本是数据科学家最广泛使用的软件,用于开发深度学习模型并与之互动。
Running Your First Notebook
运行你的第一个Notebook
The notebooks are labeled by chapter and then by notebook number, so that they are in the same order as they are presented in this book. So, the very first notebook you will see listed is the notebook that you need to use now. You will be using this notebook to train a model that can recognize dog and cat photos. To do this, you'll be downloading a dataset of dog and cat photos, and using that to train a model. A dataset is simply a bunch of data—it could be images, emails, financial indicators, sounds, or anything else. There are many datasets made freely available that are suitable for training models. Many of these datasets are created by academics to help advance research, many are made available for competitions (there are competitions where data scientists can compete to see who has the most accurate model!), and some are by-products of other processes (such as financial filings).
笔记本按章节标示,然后按笔记本编号标示,因此,它们的顺序与本书中的顺序相同。因此,你将看到列出的第一个笔记本是你现在需要使用的笔记本。你将使用这个笔记本来训练一个能够识别狗和猫照片的模型。要做到这一点,你将下载一个狗和猫照片的数据集,并使用它来训练一个模型。数据集只是一堆数据--它可以是图像、电子邮件、金融指标、声音或其他任何东西。有许多数据集是免费提供的,适合用于训练模型。其中许多数据集是由学术界创建的,以帮助推进研究,许多是为比赛提供的(有比赛,数据科学家可以竞争,看谁的模型最准确!),还有一些是其他过程的副产品(如财务申报)。
note: Full and Stripped Notebooks: There are two folders containing different versions of the notebooks. The full folder contains the exact notebooks used to create the book you're reading now, with all the prose and outputs. The stripped version has the same headings and code cells, but all outputs and prose have been removed. After reading a section of the book, we recommend working through the stripped notebooks, with the book closed, and seeing if you can figure out what each cell will show before you execute it. Also try to recall what the code is demonstrating.
注:完整的和剥离的笔记本。有两个文件夹包含不同版本的笔记本。完整的文件夹包含用于创建你现在正在阅读的这本书的笔记本,包括所有的文章和输出。剥离的版本有相同的标题和代码单元,但所有输出和文字内容都已被删除。在阅读完本书的某一章节后,我们建议在合上书本的情况下,翻阅剥离后的笔记本,看看你是否能在执行前弄清楚每个单元格会显示什么。还可以试着回忆一下代码所演示的内容。
To open a notebook, just click on it. The notebook will open, and it will look something like <> (note that there may be slight differences in details across different platforms; you can ignore those differences).
要打开一个笔记本,只需点击它。笔记本将被打开,它看起来就像<>(注意,不同的平台在细节上可能有轻微的差异,你可以忽略这些差异)。

A notebook consists of cells. There are two main types of cell:
- Cells containing formatted text, images, and so forth. These use a format called markdown, which you will learn about soon.
- Cells containing code that can be executed, and outputs will appear immediately underneath (which could be plain text, tables, images, animations, sounds, or even interactive applications).
Jupyter notebooks can be in one of two modes: edit mode or command mode. In edit mode typing on your keyboard enters the letters into the cell in the usual way. However, in command mode, you will not see any flashing cursor, and the keys on your keyboard will each have a special function.
Before continuing, press the Escape key on your keyboard to switch to command mode (if you are already in command mode, this does nothing, so press it now just in case). To see a complete list of all of the functions available, press H; press Escape to remove this help screen. Notice that in command mode, unlike most programs, commands do not require you to hold down Control, Alt, or similar—you simply press the required letter key.
You can make a copy of a cell by pressing C (the cell needs to be selected first, indicated with an outline around it; if it is not already selected, click on it once). Then press V to paste a copy of it.
一个笔记本由单元格组成。有两种主要类型的单元格。
- 包含格式化文本、图像等的单元格。这些单元格使用一种叫做markdown的格式,你很快就会了解到这一点。
- 包含可以执行的代码的单元格,输出会紧接着出现在下面(可以是纯文本、表格、图像、动画、声音,甚至是交互式应用程序)。
Jupyter笔记本可以处于两种模式中的一种:编辑模式或命令模式。在编辑模式下,在键盘上打字,以通常的方式将字母输入单元格。然而,在命令模式下,你不会看到任何闪烁的光标,键盘上的每个键都有特殊功能。
在继续之前,请按键盘上的Escape键切换到命令模式(如果你已经在命令模式中,这不会有任何作用,所以现在按它只是为了以防万一)。要查看所有可用功能的完整列表,请按H键;按Escape键可以删除这个帮助界面。注意,在命令模式下,与大多数程序不同,命令不需要你按住Control、Alt或类似的键,你只需按下所需的字母键。
你可以按C键复制一个单元格(首先需要选择该单元格,用周围的轮廓表示;如果还没有选择,点击一次)。然后按V键来粘贴它的副本。
Click on the cell that begins with the line "# CLICK ME" to select it. The first character in that line indicates that what follows is a comment in Python, so it is ignored when executing the cell. The rest of the cell is, believe it or not, a complete system for creating and training a state-of-the-art model for recognizing cats versus dogs. So, let's train it now! To do so, just press Shift-Enter on your keyboard, or press the Play button on the toolbar. Then wait a few minutes while the following things happen:
- A dataset called the Oxford-IIIT Pet Dataset that contains 7,349 images of cats and dogs from 37 different breeds will be downloaded from the fast.ai datasets collection to the GPU server you are using, and will then be extracted.
- A pretrained model that has already been trained on 1.3 million images, using a competition-winning model will be downloaded from the internet.
- The pretrained model will be fine-tuned using the latest advances in transfer learning, to create a model that is specially customized for recognizing dogs and cats.
The first two steps only need to be run once on your GPU server. If you run the cell again, it will use the dataset and model that have already been downloaded, rather than downloading them again. Let's take a look at the contents of the cell, and the results (<<first_training>>):
点击以 "# CLICK ME "一行开头的单元格来选择它。该行的第一个字符表示后面的内容是Python中的注释,所以在执行单元格时它被忽略了。该单元格的其余部分,不管你信不信,都是一个完整的系统,用于创建和训练一个最先进的识别猫和狗的模型。那么,我们现在就来训练它吧! 要做到这一点,只需按键盘上的Shift-Enter键,或按工具栏上的Play按钮。然后等待几分钟,以下事情就会发生:
- 一个名为 "Oxford-IIIT宠物数据集 "的数据集包含37个不同品种的猫和狗的7,349张图像,将从fast.ai数据集集合中下载到你正在使用的GPU服务器,然后进行提取。
- 将从网络上下载一个已经在130万张图片上训练过的预训练模型(一个竞赛获奖的模型)。
- 预训练的模型将使用迁移学习的最新进展进行微调,以创建一个为识别猫狗而特别定制的模型。
前两个步骤只需要在你的GPU服务器上运行一次。如果你再次运行该单元,它将使用已经下载的数据集和模型,而不是再次下载它们。让我们看一下单元格的内容,以及结果(<<first_training>>)。
#id first_training
#caption Results from the first training
# CLICK ME
from fastai.vision.all import *
path = untar_data(URLs.PETS)/'images'
def is_cat(x): return x[0].isupper()
dls = ImageDataLoaders.from_name_func(
path, get_image_files(path), valid_pct=0.2, seed=42,
label_func=is_cat, item_tfms=Resize(224))
learn = vision_learner(dls, resnet34, metrics=error_rate)
learn.fine_tune(1)You will probably not see exactly the same results that are in the book. There are a lot of sources of small random variation involved in training models. We generally see an error rate of well less than 0.02 in this example, however.
你可能不会看到与书中完全相同的结果。在训练模型中,有很多小的随机变化的来源。然而,在这个例子中,我们一般看到的错误率远远低于0.02。
important: Training Time: Depending on your network speed, it might take a few minutes to download the pretrained model and dataset. Running
fine_tunemight take a minute or so. Often models in this book take a few minutes to train, as will your own models, so it's a good idea to come up with good techniques to make the most of this time. For instance, keep reading the next section while your model trains, or open up another notebook and use it for some coding experiments.
重要的是:训练时间。根据你的网络速度,下载预训练的模型和数据集可能需要几分钟时间。运行fine_tune可能需要1分钟左右。本书中的模型往往需要几分钟的时间来训练,你自己的模型也是如此,所以想出好的技术来充分利用这段时间是个好主意。例如,在你的模型训练时继续阅读下一节,或者打开另一个笔记本,用它做一些编码实验。
Sidebar: This Book Was Written in Jupyter Notebooks
题外话:这本书是用Jupyter笔记本写的
We wrote this book using Jupyter notebooks, so for nearly every chart, table, and calculation in this book, we'll be showing you the exact code required to replicate it yourself. That's why very often in this book, you will see some code immediately followed by a table, a picture or just some text. If you go on the book's website you will find all the code, and you can try running and modifying every example yourself.
我们使用Jupyter笔记本来写这本书,所以对于本书中几乎每一个图表、表格和计算,我们都会向你展示自己复制它所需要的准确代码。这就是为什么在本书中,很多时候你会看到一些代码后面紧跟着一张表格、一张图片或只是一些文字。如果你进入本书的网站,你会发现所有的代码,你可以尝试自己运行和修改每个例子。
You just saw how a cell that outputs a table looks inside the book. Here is an example of a cell that outputs text:
你刚刚看到了一个输出表格的单元格在书中的样子。下面是一个输出文本的单元格的例子。
1+1Jupyter will always print or show the result of the last line (if there is one). For instance, here is an example of a cell that outputs an image:
Jupyter总是会打印或显示最后一行的结果(如果有的话)。例如,这里是一个输出图像的单元格的例子。
img = PILImage.create(image_cat())
img.to_thumb(192)End sidebar
题外话结束
So, how do we know if this model is any good? In the last column of the table you can see the error rate, which is the proportion of images that were incorrectly identified. The error rate serves as our metric—our measure of model quality, chosen to be intuitive and comprehensible. As you can see, the model is nearly perfect, even though the training time was only a few seconds (not including the one-time downloading of the dataset and the pretrained model). In fact, the accuracy you've achieved already is far better than anybody had ever achieved just 10 years ago!
Finally, let's check that this model actually works. Go and get a photo of a dog, or a cat; if you don't have one handy, just search Google Images and download an image that you find there. Now execute the cell with uploader defined. It will output a button you can click, so you can select the image you want to classify:
那么,我们如何知道这个模型是否好呢?在表格的最后一栏,你可以看到错误率,也就是被错误识别的图像比例。错误率是我们的衡量标准--我们对模型质量的衡量,被选择为直观和可理解的。正如你所看到的,这个模型几乎是完美的,尽管训练时间只有几秒钟(不包括一次性下载数据集和预训练的模型)。事实上,你所取得的准确度已经远远超过了任何人在10年前所取得的成绩。
最后,让我们检查一下这个模型是否真的有效。去找一张狗或猫的照片;如果你手头没有,就在谷歌图片上搜索并下载一张你在那里找到的图片。现在执行定义了uploader的单元格。它将输出一个你可以点击的按钮,所以你可以选择你想分类的图片。
#hide_output
uploader = widgets.FileUpload()
uploaderOutput
FileUpload(value={}, description='Upload')
Now you can pass the uploaded file to the model. Make sure that it is a clear photo of a single dog or a cat, and not a line drawing, cartoon, or similar. The notebook will tell you whether it thinks it is a dog or a cat, and how confident it is. Hopefully, you'll find that your model did a great job:
现在你可以把上传的文件传给模型。确保它是一张清晰的单身狗或猫的照片,而不是线描、卡通或类似的东西。笔记本会告诉你它是否认为这是一只狗或一只猫,而且它十分自信。希望你会发现,你的模型做得很好。
#hide
# For the book, we can't actually click an upload button, so we fake it
uploader = SimpleNamespace(data = ['images/chapter1_cat_example.jpg'])img = PILImage.create(uploader.data[0])
is_cat,_,probs = learn.predict(img)
print(f"Is this a cat?: {is_cat}.")
print(f"Probability it's a cat: {probs[1].item():.6f}")Output
<IPython.core.display.HTML object>
Is this a cat?: True. Probability it's a cat: 1.000000
Congratulations on your first classifier!
But what does this mean? What did you actually do? In order to explain this, let's zoom out again to take in the big picture.
为你的第一个分类器表示祝贺!
但这意味着什么呢?你实际上做了什么?为了解释这个问题,让我们再次放大,来看看大局。
What Is Machine Learning?
什么是机器学习
Your classifier is a deep learning model. As was already mentioned, deep learning models use neural networks, which originally date from the 1950s and have become powerful very recently thanks to recent advancements.
Another key piece of context is that deep learning is just a modern area in the more general discipline of machine learning. To understand the essence of what you did when you trained your own classification model, you don't need to understand deep learning. It is enough to see how your model and your training process are examples of the concepts that apply to machine learning in general.
So in this section, we will describe what machine learning is. We will look at the key concepts, and show how they can be traced back to the original essay that introduced them.
Machine learning is, like regular programming, a way to get computers to complete a specific task. But how would we use regular programming to do what we just did in the last section: recognize dogs versus cats in photos? We would have to write down for the computer the exact steps necessary to complete the task.
Normally, it's easy enough for us to write down the steps to complete a task when we're writing a program. We just think about the steps we'd take if we had to do the task by hand, and then we translate them into code. For instance, we can write a function that sorts a list. In general, we'd write a function that looks something like <<basic_program>> (where inputs might be an unsorted list, and results a sorted list).
你的分类器是一个深度学习模型。正如已经提到的,深度学习模型使用的是神经网络,它最初可以追溯到20世纪50年代,由于最近的进步,它已经变得非常强大。
另一个关键的背景是,深度学习只是机器学习这门更普遍的学科中的一个现代领域。要理解你在训练自己的分类模型时所做的事情的本质,你不需要了解深度学习。只要看到你的模型和你的训练过程是适用于一般机器学习的概念的例子,就足够了。
因此在本节中,我们将描述什么是机器学习。我们将看一下关键的概念,并说明它们如何可以追溯到介绍它们的原始文章中。
机器学习和常规编程一样,是一种让计算机完成特定任务的方法。但我们如何使用常规编程来完成我们在上一节中所做的事情:识别照片中的狗和猫?我们将不得不为计算机写下完成任务所需的确切步骤。
通常情况下,我们在编写程序时很容易写下完成一项任务的步骤。我们只需想一想,如果我们不得不用手去做这项任务,我们会采取哪些步骤,然后把它们翻译成代码。例如,我们可以写一个对列表进行排序的函数。一般来说,我们会写一个类似<>的函数(其中输入可能是一个未排序的列表,而结果是一个已排序的列表)。
#hide_input
#caption A traditional program
#id basic_program
#alt Pipeline inputs, program, results
gv('''program[shape=box3d width=1 height=0.7]
inputs->program->results''')But for recognizing objects in a photo that's a bit tricky; what are the steps we take when we recognize an object in a picture? We really don't know, since it all happens in our brain without us being consciously aware of it!
Right back at the dawn of computing, in 1949, an IBM researcher named Arthur Samuel started working on a different way to get computers to complete tasks, which he called machine learning. In his classic 1962 essay "Artificial Intelligence: A Frontier of Automation", he wrote:
但对于识别照片中的物体来说,这就有点棘手了;当我们识别照片中的物体时,我们会采取哪些步骤?我们真的不知道,因为这一切都发生在我们的大脑中,而我们却没有意识到这一点!
就在计算机的黎明时分,1949年,一位名叫阿瑟-塞缪尔的IBM研究员开始研究一种不同的方式来让计算机完成任务,他称之为机器学习。在他1962年的经典文章《人工智能:自动化的前沿》一文中,他写道:
: Programming a computer for such computations is, at best, a difficult task, not primarily because of any inherent complexity in the computer itself but, rather, because of the need to spell out every minute step of the process in the most exasperating detail. Computers, as any programmer will tell you, are giant morons, not giant brains.
:为这种计算编程充其量是一项困难的任务,主要不是因为计算机本身有任何固有的复杂性,而是因为需要以最令人恼火的细节来说明这一过程的每一个细小步骤。正如任何程序员会告诉你的,计算机是巨大的白痴,而不是巨大的大脑。
His basic idea was this: instead of telling the computer the exact steps required to solve a problem, show it examples of the problem to solve, and let it figure out how to solve it itself. This turned out to be very effective: by 1961 his checkers-playing program had learned so much that it beat the Connecticut state champion! Here's how he described his idea (from the same essay as above):
他的基本想法是这样的:不要告诉计算机解决一个问题所需的确切步骤,而是向它展示要解决的问题的例子,让它自己想出如何解决。这被证明是非常有效的:到1961年,他的跳棋程序已经学到了很多东西,以至于它打败了康涅狄格州的冠军!他是这样描述他的想法的(来自于《美国科学》杂志)。以下是他对自己想法的描述(来自上述同一篇文章):
: Suppose we arrange for some automatic means of testing the effectiveness of any current weight assignment in terms of actual performance and provide a mechanism for altering the weight assignment so as to maximize the performance. We need not go into the details of such a procedure to see that it could be made entirely automatic and to see that a machine so programmed would "learn" from its experience.
:假设我们安排一些自动手段来测试任何当前权重分配在实际性能方面的有效性,并提供一种机制来改变权重分配,以使性能最大化。我们不需要研究这种程序的细节,就可以看到它可以完全自动进行,并且看到这样编程的机器会从其经验中 "学习"。
There are a number of powerful concepts embedded in this short statement:
- The idea of a "weight assignment"
- The fact that every weight assignment has some "actual performance"
- The requirement that there be an "automatic means" of testing that performance,
- The need for a "mechanism" (i.e., another automatic process) for improving the performance by changing the weight assignments
Let us take these concepts one by one, in order to understand how they fit together in practice. First, we need to understand what Samuel means by a weight assignment.
Weights are just variables, and a weight assignment is a particular choice of values for those variables. The program's inputs are values that it processes in order to produce its results—for instance, taking image pixels as inputs, and returning the classification "dog" as a result. The program's weight assignments are other values that define how the program will operate.
Since they will affect the program they are in a sense another kind of input, so we will update our basic picture in <<basic_program>> and replace it with <<weight_assignment>> in order to take this into account.
这个简短的声明中蕴含着许多强有力的概念。
- "权重分配 "的概念
- 每个权重分配都有一些 "实际性能 "的事实
- 要求有一个测试该性能的 "自动手段"。
- 需要一个 "机制"(即另一个自动程序),通过改变权重分配来提高性能。
让我们逐一讨论这些概念,以了解它们在实践中是如何结合的。首先,我们需要理解塞缪尔所说的权重分配是什么意思。
权重只是变量,而权重分配是对这些变量数值的特定选择。程序的输入是它为了产生结果而处理的数值--例如,将图像像素作为输入,并将分类 "狗 "作为结果返回。程序的权重分配是定义程序如何运行的其他数值。
由于它们会影响程序,在某种意义上它们是另一种输入,所以我们将更新<<basic_program>>中的基本图片,用<<weight_assignment>>来代替它,以便考虑到这一点。
#hide_input
#caption A program using weight assignment
#id weight_assignment
gv('''model[shape=box3d width=1 height=0.7]
inputs->model->results; weights->model''')Output
<graphviz.files.Source at 0x7efcae0c5250>
We've changed the name of our box from program to model. This is to follow modern terminology and to reflect that the model is a special kind of program: it's one that can do many different things, depending on the weights. It can be implemented in many different ways. For instance, in Samuel's checkers program, different values of the weights would result in different checkers-playing strategies.
(By the way, what Samuel called "weights" are most generally referred to as model parameters these days, in case you have encountered that term. The term weights is reserved for a particular type of model parameter.)
Next, Samuel said we need an automatic means of testing the effectiveness of any current weight assignment in terms of actual performance. In the case of his checkers program, the "actual performance" of a model would be how well it plays. And you could automatically test the performance of two models by setting them to play against each other, and seeing which one usually wins.
Finally, he says we need a mechanism for altering the weight assignment so as to maximize the performance. For instance, we could look at the difference in weights between the winning model and the losing model, and adjust the weights a little further in the winning direction.
We can now see why he said that such a procedure could be made entirely automatic and... a machine so programmed would "learn" from its experience. Learning would become entirely automatic when the adjustment of the weights was also automatic—when instead of us improving a model by adjusting its weights manually, we relied on an automated mechanism that produced adjustments based on performance.
<<training_loop>> shows the full picture of Samuel's idea of training a machine learning model.
我们已经将我们的盒子的名字从程序改为模型。这是为了遵循现代术语,并反映出模型是一种特殊的程序:它是一个可以做许多不同事情的程序,这取决于权重。它可以用许多不同的方式来实现。例如,在塞缪尔的跳棋程序中,不同的权重值会导致不同的跳棋游戏策略。
(顺便说一下,塞缪尔所说的 "权重 "现在一般被称为模型参数,如果你遇到过这个词的话。权重一词是为一种特殊类型的模型参数保留的)。
接下来,塞缪尔说,我们需要一种自动手段来测试任何当前权重分配在实际性能方面的有效性。在他的跳棋程序中,一个模型的 "实际表现 "就是它的棋艺如何。你可以自动测试两个模型的性能,将它们设置为相互对弈,看看哪一个通常会赢。
最后,他说我们需要一个改变权重分配的机制,以使性能最大化。例如,我们可以看一下获胜模型和失败模型之间的权重差异,然后在获胜的方向上再调整一下权重。
我们现在可以看到,为什么他说这样的程序可以完全自动化,而且......如此编程的机器将从其经验中 "学习"。当权重的调整也是自动的时候,学习就会变得完全自动--当我们不是通过手动调整权重来改善一个模型,而是依靠一个自动机制,根据性能来进行调整。
<<training_loop>>显示了塞缪尔训练机器学习模型的想法的全貌。
#hide_input
#caption Training a machine learning model
#id training_loop
#alt The basic training loop
gv('''ordering=in
model[shape=box3d width=1 height=0.7]
inputs->model->results; weights->model; results->performance
performance->weights[constraint=false label=update]''')Output
<graphviz.files.Source at 0x7efcac812410>
Notice the distinction between the model's results (e.g., the moves in a checkers game) and its performance (e.g., whether it wins the game, or how quickly it wins).
Also note that once the model is trained—that is, once we've chosen our final, best, favorite weight assignment—then we can think of the weights as being part of the model, since we're not varying them any more.
Therefore, actually using a model after it's trained looks like <<using_model>>.
注意模型的结果(如跳棋游戏中的棋子)和它的性能(如它是否赢得游戏,或它赢得多快)之间的区别。
还要注意的是,一旦模型被训练好了,也就是说,一旦我们选择了最终的、最好的、最喜欢的权重分配,那么我们就可以把权重看作是模型的一部分,因为我们不再改变它们了。
因此,在模型训练完成后,实际使用该模型看起来像<<using_model>>。
#hide_input
#caption Using a trained model as a program
#id using_model
gv('''model[shape=box3d width=1 height=0.7]
inputs->model->results''')Output
<graphviz.files.Source at 0x7efcac8129d0>
This looks identical to our original diagram in <<basic_program>>, just with the word program replaced with model. This is an important insight: a trained model can be treated just like a regular computer program.
这看起来与我们在<<basic_program>>中的原图相同,只是将程序一词替换为模型。这是一个重要的见解:一个经过训练的模型可以像一个普通的计算机程序一样被对待。
jargon: Machine Learning: The training of programs developed by allowing a computer to learn from its experience, rather than through manually coding the individual steps.
术语:机器学习。通过让计算机从其经验中学习,而不是通过手动编码各个步骤来开发程序的训练。
What Is a Neural Network?
什么是神经网络
It's not too hard to imagine what the model might look like for a checkers program. There might be a range of checkers strategies encoded, and some kind of search mechanism, and then the weights could vary how strategies are selected, what parts of the board are focused on during a search, and so forth. But it's not at all obvious what the model might look like for an image recognition program, or for understanding text, or for many other interesting problems we might imagine.
What we would like is some kind of function that is so flexible that it could be used to solve any given problem, just by varying its weights. Amazingly enough, this function actually exists! It's the neural network, which we already discussed. That is, if you regard a neural network as a mathematical function, it turns out to be a function which is extremely flexible depending on its weights. A mathematical proof called the universal approximation theorem shows that this function can solve any problem to any level of accuracy, in theory. The fact that neural networks are so flexible means that, in practice, they are often a suitable kind of model, and you can focus your effort on the process of training them—that is, of finding good weight assignments.
But what about that process? One could imagine that you might need to find a new "mechanism" for automatically updating weights for every problem. This would be laborious. What we'd like here as well is a completely general way to update the weights of a neural network, to make it improve at any given task. Conveniently, this also exists!
This is called stochastic gradient descent (SGD). We'll see how neural networks and SGD work in detail in <<chapter_mnist_basics>>, as well as explaining the universal approximation theorem. For now, however, we will instead use Samuel's own words: We need not go into the details of such a procedure to see that it could be made entirely automatic and to see that a machine so programmed would "learn" from its experience.
不难想象,跳棋程序的模型可能是什么样子的。可能会有一系列的跳棋策略被编码,还有某种搜索机制,然后权重可以改变策略的选择方式,在搜索过程中关注棋盘的哪些部分,等等。但是,对于一个图像识别程序,或者对于理解文本,或者对于我们可能想象的许多其他有趣的问题,这个模型可能是什么样子的,可能一点也不明显。
我们想要的是某种函数,它非常灵活,可以用来解决任何给定的问题,只需改变其权重。令人惊讶的是,这种函数实际上是存在的 它就是我们已经讨论过的神经网络。也就是说,如果你把神经网络看作是一个数学函数,那么它就变成了一个根据其权重而极其灵活的函数。一个被称为普遍近似定理的数学证明表明,从理论上讲,这个函数可以解决任何问题,达到任何程度的精度。神经网络如此灵活的事实意味着,在实践中,它们往往是一种合适的模型,你可以把精力集中在训练它们的过程中,也就是找到好的权重分配。
但这个过程怎么办?我们可以想象,你可能需要找到一个新的 "机制 "来自动更新每个问题的权重。这将是很费力的。我们在这里也希望有一个完全通用的方法来更新神经网络的权重,以使它在任何给定的任务中得到改善。方便的是,这也是存在的!
这就是所谓的随机梯度下降(SGD)。我们将在<>中看到神经网络和SGD的详细工作情况,以及解释通用近似定理。不过现在,我们将改用塞缪尔自己的话。我们不需要深入了解这种程序的细节,就可以看到它可以完全自动化,并看到如此编程的机器将从其经验中 "学习"。
J: Don't worry, neither SGD nor neural nets are mathematically complex. Both nearly entirely rely on addition and multiplication to do their work (but they do a lot of addition and multiplication!). The main reaction we hear from students when they see the details is: "Is that all it is?"
J:别担心,SGD和神经网络在数学上都不复杂。两者都几乎完全依靠加法和乘法来完成它们的工作(但它们做了大量的加法和乘法!)。我们从学生那里听到的主要反应是,当他们看到这些细节时。"这就是它的全部吗?"
In other words, to recap, a neural network is a particular kind of machine learning model, which fits right in to Samuel's original conception. Neural networks are special because they are highly flexible, which means they can solve an unusually wide range of problems just by finding the right weights. This is powerful, because stochastic gradient descent provides us a way to find those weight values automatically.
Having zoomed out, let's now zoom back in and revisit our image classification problem using Samuel's framework.
Our inputs are the images. Our weights are the weights in the neural net. Our model is a neural net. Our results are the values that are calculated by the neural net, like "dog" or "cat."
What about the next piece, an automatic means of testing the effectiveness of any current weight assignment in terms of actual performance? Determining "actual performance" is easy enough: we can simply define our model's performance as its accuracy at predicting the correct answers.
Putting this all together, and assuming that SGD is our mechanism for updating the weight assignments, we can see how our image classifier is a machine learning model, much like Samuel envisioned.
换句话说,神经网络是一种特殊的机器学习模型,它正好符合塞缪尔的原始概念。神经网络很特别,因为它们高度灵活,这意味着它们可以通过找到正确的权重来解决异常广泛的问题。这很强大,因为随机梯度下降为我们提供了一种自动寻找这些权重值的方法。
在放大了之后,现在让我们再放大,用塞缪尔的框架重新审视我们的图像分类问题。
我们的输入是图像。我们的权重是神经网络中的权重。我们的模型是一个神经网络。我们的结果是由神经网络计算出来的值,如 "狗 "或 "猫"。
那么,下一块,即以实际性能来测试任何当前权重分配的有效性的自动手段呢?确定 "实际性能 "很容易:我们可以简单地将我们的模型的性能定义为预测正确答案的准确性。
把这一切放在一起,并假设SGD是我们更新权重分配的机制,我们可以看到我们的图像分类器是一个机器学习模型,很像塞缪尔设想的那样。
A Bit of Deep Learning Jargon
一些深度学习的术语
Samuel was working in the 1960s, and since then terminology has changed. Here is the modern deep learning terminology for all the pieces we have discussed:
- The functional form of the model is called its architecture (but be careful—sometimes people use model as a synonym of architecture, so this can get confusing).
- The weights are called parameters.
- The predictions are calculated from the independent variable, which is the data not including the labels.
- The results of the model are called predictions.
- The measure of performance is called the loss.
- The loss depends not only on the predictions, but also the correct labels (also known as targets or the dependent variable); e.g., "dog" or "cat."
After making these changes, our diagram in <<training_loop>> looks like <<detailed_loop>>.
塞缪尔是在20世纪60年代工作的,从那时起,术语已经改变了。下面是我们讨论过的所有件的现代深度学习术语。
- 模型的功能形式被称为其架构(但要小心--有时人们把模型作为架构的同义词,所以这可能会引起混淆)。
- 权重被称为参数。
- 预测是根据自变量计算的,自变量是不包括标签的数据。
- 该模型的结果被称为预测。
- 衡量性能的标准被称为损失。
- 损失不仅取决于预测结果,而且还取决于正确的标签(也称为目标或因变量);例如,"狗 "或 "猫"。
做了这些改动后,我们在<<training_loop>>中的图看起来像<<detailed_loop>>。
#hide_input
#caption Detailed training loop
#id detailed_loop
gv('''ordering=in
model[shape=box3d width=1 height=0.7 label=architecture]
inputs->model->predictions; parameters->model; labels->loss; predictions->loss
loss->parameters[constraint=false label=update]''')Limitations Inherent To Machine Learning
From this picture we can now see some fundamental things about training a deep learning model:
- A model cannot be created without data.
- A model can only learn to operate on the patterns seen in the input data used to train it.
- This learning approach only creates predictions, not recommended actions.
- It's not enough to just have examples of input data; we need labels for that data too (e.g., pictures of dogs and cats aren't enough to train a model; we need a label for each one, saying which ones are dogs, and which are cats).
Generally speaking, we've seen that most organizations that say they don't have enough data, actually mean they don't have enough labeled data. If any organization is interested in doing something in practice with a model, then presumably they have some inputs they plan to run their model against. And presumably they've been doing that some other way for a while (e.g., manually, or with some heuristic program), so they have data from those processes! For instance, a radiology practice will almost certainly have an archive of medical scans (since they need to be able to check how their patients are progressing over time), but those scans may not have structured labels containing a list of diagnoses or interventions (since radiologists generally create free-text natural language reports, not structured data). We'll be discussing labeling approaches a lot in this book, because it's such an important issue in practice.
Since these kinds of machine learning models can only make predictions (i.e., attempt to replicate labels), this can result in a significant gap between organizational goals and model capabilities. For instance, in this book you'll learn how to create a recommendation system that can predict what products a user might purchase. This is often used in e-commerce, such as to customize products shown on a home page by showing the highest-ranked items. But such a model is generally created by looking at a user and their buying history (inputs) and what they went on to buy or look at (labels), which means that the model is likely to tell you about products the user already has or already knows about, rather than new products that they are most likely to be interested in hearing about. That's very different to what, say, an expert at your local bookseller might do, where they ask questions to figure out your taste, and then tell you about authors or series that you've never heard of before.
机器学习固有的局限性
从这幅图中,我们现在可以看到关于训练深度学习模型的一些基本情况。
- 没有数据就无法创建一个模型。
- 一个模型只能在用于训练它的输入数据中看到的模式上学习操作。
- 这种学习方法只能创建预测,而不是推荐行动。
- 仅仅有输入数据的例子是不够的;我们还需要为这些数据贴上标签(例如,狗和猫的图片不足以训练一个模型;我们需要为每张图片贴上标签,说明哪些是狗,哪些是猫)。
一般来说,我们看到,大多数组织说他们没有足够的数据,实际上是指他们没有足够的标记数据。如果任何组织有兴趣在实践中用一个模型做一些事情,那么他们可能有一些计划运行的模型的输入。而且,他们可能已经用其他方式做了一段时间(例如,手动,或用一些启发式程序),所以他们有来自这些过程的数据。例如,一个放射科诊所几乎肯定会有一个医疗扫描档案(因为他们需要能够检查他们的病人在一段时间内的进展情况),但这些扫描可能没有包含诊断或干预措施清单的结构化标签(因为放射科医生通常创建自由文本自然语言报告,而不是结构化数据)。我们将在本书中大量讨论标签方法,因为它是实践中的一个重要问题。
由于这类机器学习模型只能进行预测(即试图复制标签),这可能导致组织目标和模型能力之间的巨大差距。例如,在本书中,你将学习如何创建一个能够预测用户可能购买的产品的推荐系统。这通常用于电子商务,例如,通过显示排名最高的商品来定制显示在主页上的产品。但这样的模型一般是通过查看用户和他们的购买历史(输入)以及他们继续购买或看的东西(标签)来创建的,这意味着该模型可能会告诉你用户已经拥有或已经知道的产品,而不是他们最有可能感兴趣的新产品。这与比如说,你当地书商的专家可能做的非常不同,他们会问一些问题来了解你的品味,然后告诉你一些你以前从未听说过的作家或系列。
Another critical insight comes from considering how a model interacts with its environment. This can create feedback loops, as described here:
- A predictive policing model is created based on where arrests have been made in the past. In practice, this is not actually predicting crime, but rather predicting arrests, and is therefore partially simply reflecting biases in existing policing processes.
- Law enforcement officers then might use that model to decide where to focus their police activity, resulting in increased arrests in those areas.
- Data on these additional arrests would then be fed back in to retrain future versions of the model.
This is a positive feedback loop, where the more the model is used, the more biased the data becomes, making the model even more biased, and so forth.
Feedback loops can also create problems in commercial settings. For instance, a video recommendation system might be biased toward recommending content consumed by the biggest watchers of video (e.g., conspiracy theorists and extremists tend to watch more online video content than the average), resulting in those users increasing their video consumption, resulting in more of those kinds of videos being recommended. We'll consider this topic more in detail in <<chapter_ethics>>.
另一个关键的见解来自于考虑一个模型如何与环境互动。这可以创造反馈循环,正如这里所描述的。
- 一个预测性的警务模型是根据过去的逮捕地点创建的。在实践中,这实际上不是在预测犯罪,而是在预测逮捕,因此部分地只是反映了现有警务过程中的偏见。
- 然后,执法人员可能会使用该模型来决定将他们的警察活动集中在哪里,导致这些地区的逮捕人数增加。
- 关于这些额外逮捕的数据将被反馈到模型的未来版本中进行再训练。
这是一个正反馈循环,模型用得越多,数据就越有偏差,使模型更加偏颇,如此类推。
Now that you have seen the base of the theory, let's go back to our code example and see in detail how the code corresponds to the process we just described.
现在你已经看到了理论的基础,让我们回到我们的代码例子,详细看看代码是如何与我们刚才描述的过程相对应的。
How Our Image Recognizer Works
我们的图像识别器是如何工作的
Let's see just how our image recognizer code maps to these ideas. We'll put each line into a separate cell, and look at what each one is doing (we won't explain every detail of every parameter yet, but will give a description of the important bits; full details will come later in the book).
让我们来看看我们的图像识别器代码是如何与这些想法对应的。我们将把每一行放到一个单独的单元格中,看看每一行在做什么(我们还不会解释每一个参数的每一个细节,但会对重要的部分进行描述;完整的细节将在本书的后面出现)。
The first line imports all of the fastai.vision library.
from fastai.vision.all import *This gives us all of the functions and classes we will need to create a wide variety of computer vision models.
第一行导入了fastai.vision库的所有内容。
from fastai.vision.all import *这为我们提供了创建各种计算机视觉模型所需的所有函数和类。
J: A lot of Python coders recommend avoiding importing a whole library like this (using the
import *syntax), because in large software projects it can cause problems. However, for interactive work such as in a Jupyter notebook, it works great. The fastai library is specially designed to support this kind of interactive use, and it will only import the necessary pieces into your environment.
J: 很多Python编码人员建议避免像这样导入整个库(使用import * 语法),因为在大型软件项目中,这可能会导致问题。然而,对于交互式工作,比如在Jupyter笔记本中,它非常好用。fastai库是专门为支持这种交互式使用而设计的,它只将必要的部分导入你的环境中。
The second line downloads a standard dataset from the fast.ai datasets collection (if not previously downloaded) to your server, extracts it (if not previously extracted), and returns a Path object with the extracted location:
path = untar_data(URLs.PETS)/'images'S: Throughout my time studying at fast.ai, and even still today, I've learned a lot about productive coding practices. The fastai library and fast.ai notebooks are full of great little tips that have helped make me a better programmer. For instance, notice that the fastai library doesn't just return a string containing the path to the dataset, but a
Pathobject. This is a really useful class from the Python 3 standard library that makes accessing files and directories much easier. If you haven't come across it before, be sure to check out its documentation or a tutorial and try it out. Note that the https://book.fast.ai[website] contains links to recommended tutorials for each chapter. I'll keep letting you know about little coding tips I've found useful as we come across them.
第二行从fast.ai datasets集合中下载一个标准数据集(如果之前没有下载)到你的服务器,提取它(如果之前没有提取),并返回一个带有提取位置的Path对象。
path = untar_data(URLs.PETS)/'images'S: 在fast.ai学习的这段时间里,甚至到现在,我都学到了很多关于高效编码的做法。fastai库和fast.ai的笔记本上有很多很棒的小提示,帮助我成为一个更好的程序员。例如,注意到fastai库并不只是返回一个包含数据集路径的字符串,而是一个Path对象。这是Python 3标准库中一个非常有用的类,它使访问文件和目录变得更加容易。如果你以前没有接触过它,一定要看看它的文档或教程,并尝试一下。请注意,https://book.fast.ai[网站] 包含每一章的推荐教程的链接。 我将继续让你知道我发现的有用的编码小技巧,因为我们遇到了它们。
In the third line we define a function, is_cat, which labels cats based on a filename rule provided by the dataset creators:
def is_cat(x): return x[0].isupper()在第三行,我们定义了一个函数is_cat,它根据数据集创建者提供的文件名规则给猫贴上标签。
def is_cat(x): return x[0].isupper()We use that function in the fourth line, which tells fastai what kind of dataset we have and how it is structured:
dls = ImageDataLoaders.from_name_func(
path, get_image_files(path), valid_pct=0.2, seed=42,
label_func=is_cat, item_tfms=Resize(224))There are various different classes for different kinds of deep learning datasets and problems—here we're using ImageDataLoaders. The first part of the class name will generally be the type of data you have, such as image, or text.
The other important piece of information that we have to tell fastai is how to get the labels from the dataset. Computer vision datasets are normally structured in such a way that the label for an image is part of the filename, or path—most commonly the parent folder name. fastai comes with a number of standardized labeling methods, and ways to write your own. Here we're telling fastai to use the is_cat function we just defined.
Finally, we define the Transforms that we need. A Transform contains code that is applied automatically during training; fastai includes many predefined Transforms, and adding new ones is as simple as creating a Python function. There are two kinds: item_tfms are applied to each item (in this case, each item is resized to a 224-pixel square), while batch_tfms are applied to a batch of items at a time using the GPU, so they're particularly fast (we'll see many examples of these throughout this book).
Why 224 pixels? This is the standard size for historical reasons (old pretrained models require this size exactly), but you can pass pretty much anything. If you increase the size, you'll often get a model with better results (since it will be able to focus on more details), but at the price of speed and memory consumption; the opposite is true if you decrease the size.
我们在第四行使用该函数,它告诉fastai我们有什么样的数据集以及它的结构。
dls = ImageDataLoaders.from_name_func(
path, get_image_files(path), valid_pct=0.2, seed=42,
label_func=is_cat, item_tfms=Resize(224))对于不同类型的深度学习数据集和问题,有各种不同的类--这里我们使用的是ImageDataLoaders。类名的第一部分一般是你所拥有的数据类型,如图像,或文本。
我们必须告诉fastai的另一个重要信息是如何从数据集中获得标签。计算机视觉数据集的结构通常是这样的:图像的标签是文件名或路径的一部分,最常见的是父文件夹名称。这里我们要告诉fastai使用我们刚刚定义的is_cat函数。
最后,我们定义我们需要的Transform。变换包含在训练中自动应用的代码;fastai包括许多预定义的变换,添加新的变换就像创建一个Python函数一样简单。有两种类型: item_tfms应用于每个项目(在本例中,每个项目被调整为224像素的正方形),而batch_tfms使用GPU一次应用于一批项目,所以它们特别快(我们将在本书中看到许多这样的例子)。
为什么是224像素?由于历史原因,这是标准尺寸(旧的预训练模型正好需要这个尺寸),但你几乎可以通过任何尺寸。如果你增加尺寸,你通常会得到一个结果更好的模型(因为它将能够专注于更多的细节),但代价是速度和内存消耗;如果你减少尺寸,则相反。
Note: Classification and Regression: classification and regression have very specific meanings in machine learning. These are the two main types of model that we will be investigating in this book. A classification model is one which attempts to predict a class, or category. That is, it's predicting from a number of discrete possibilities, such as "dog" or "cat." A regression model is one which attempts to predict one or more numeric quantities, such as a temperature or a location. Sometimes people use the word regression to refer to a particular kind of model called a linear regression model; this is a bad practice, and we won't be using that terminology in this book!
注:分类和回归:分类和回归在机器学习中具有非常具体的含义。这是我们将在本书中研究的两种主要的模型类型。一个分类模型是试图预测一个类,或类别。也就是说,它是从一些离散的可能性中进行预测的,比如 "狗 "或 "猫"。回归模型是一个试图预测一个或多个数字量的模型,如温度或位置。有时,人们用回归这个词来指称一种特殊的模型,称为线性回归模型;这是一种不好的做法,我们在本书中不会使用这个术语。
The Pet dataset contains 7,390 pictures of dogs and cats, consisting of 37 different breeds. Each image is labeled using its filename: for instance the file great_pyrenees_173.jpg is the 173rd example of an image of a Great Pyrenees breed dog in the dataset. The filenames start with an uppercase letter if the image is a cat, and a lowercase letter otherwise. We have to tell fastai how to get labels from the filenames, which we do by calling from_name_func (which means that labels can be extracted using a function applied to the filename), and passing is_cat, which returns x[0].isupper(), which evaluates to True if the first letter is uppercase (i.e., it's a cat).
The most important parameter to mention here is valid_pct=0.2. This tells fastai to hold out 20% of the data and not use it for training the model at all. This 20% of the data is called the validation set; the remaining 80% is called the training set. The validation set is used to measure the accuracy of the model. By default, the 20% that is held out is selected randomly. The parameter seed=42 sets the random seed to the same value every time we run this code, which means we get the same validation set every time we run it—this way, if we change our model and retrain it, we know that any differences are due to the changes to the model, not due to having a different random validation set.
fastai will always show you your model's accuracy using only the validation set, never the training set. This is absolutely critical, because if you train a large enough model for a long enough time, it will eventually memorize the label of every item in your dataset! The result will not actually be a useful model, because what we care about is how well our model works on previously unseen images. That is always our goal when creating a model: for it to be useful on data that the model only sees in the future, after it has been trained.
Even when your model has not fully memorized all your data, earlier on in training it may have memorized certain parts of it. As a result, the longer you train for, the better your accuracy will get on the training set; the validation set accuracy will also improve for a while, but eventually it will start getting worse as the model starts to memorize the training set, rather than finding generalizable underlying patterns in the data. When this happens, we say that the model is overfitting.
<<img_overfit>> shows what happens when you overfit, using a simplified example where we have just one parameter, and some randomly generated data based on the function x**2. As you can see, although the predictions in the overfit model are accurate for data near the observed data points, they are way off when outside of that range.
宠物数据集包含7390张狗和猫的图片,包括37个不同的品种。每张图片都用其文件名进行标注:例如,文件great_pyrenees_173.jpg是数据集中第173张大白熊犬的图片。如果图像是一只猫,文件名就以大写字母开头,否则就以小写字母开头。我们必须告诉fastai如何从文件名中获取标签,我们通过调用from_name_func(这意味着可以使用应用于文件名的函数来提取标签),并传递is_cat,它返回x[0].isupper(),如果第一个字母是大写的(即,它是一只猫),则评估为真。
这里要提到的最重要的参数是valid_pct=0.2。这告诉fastai保留20%的数据,完全不用它来训练模型。这20%的数据被称为验证集;其余80%的数据被称为训练集。验证集是用来衡量模型的准确性的。默认情况下,被保留的20%的数据是随机选择的。参数seed=42将每次运行这段代码时的随机种子设置为相同的值,这意味着我们每次运行都会得到相同的验证集--这样,如果我们改变模型并重新训练它,我们知道任何差异都是由于模型的改变造成的,而不是由于拥有不同的随机验证集。
fastai会一直显示你的模型的准确性,只使用验证集,而不是训练集。这一点绝对是至关重要的,因为如果你训练一个足够大的模型的时间足够长,它最终会记住你的数据集中的每一个项目的标签!其结果实际上不会是一个有用的模型,因为我们关心的是我们的模型在以前未见过的图像上的效果如何。这始终是我们创建模型时的目标:让它在模型经过训练后,在未来才会看到的数据上发挥作用。
即使你的模型没有完全记住你的所有数据,在训练的早期,它可能已经记住了其中的某些部分。因此,你训练的时间越长,你在训练集上的准确率就越高;验证集的准确率也会提高一段时间,但最终会开始变差,因为模型开始记忆训练集,而不是在数据中找到可归纳的基本模式。当这种情况发生时,我们说模型是过拟合的。
<>显示了当你过度拟合时会发生什么,使用一个简化的例子,我们只有一个参数,以及一些基于函数x**2的随机生成的数据。正如你所看到的,尽管过拟合模型中的预测对于靠近观察到的数据点的数据是准确的,但当超出这个范围时,它们就会有很大的偏差。

Overfitting is the single most important and challenging issue when training for all machine learning practitioners, and all algorithms. As you will see, it is very easy to create a model that does a great job at making predictions on the exact data it has been trained on, but it is much harder to make accurate predictions on data the model has never seen before. And of course, this is the data that will actually matter in practice. For instance, if you create a handwritten digit classifier (as we will very soon!) and use it to recognize numbers written on checks, then you are never going to see any of the numbers that the model was trained on—checks will have slightly different variations of writing to deal with. You will learn many methods to avoid overfitting in this book. However, you should only use those methods after you have confirmed that overfitting is actually occurring (i.e., you have actually observed the validation accuracy getting worse during training). We often see practitioners using over-fitting avoidance techniques even when they have enough data that they didn't need to do so, ending up with a model that may be less accurate than what they could have achieved.
过度拟合是所有机器学习从业者和所有算法训练时最重要和最具挑战性的问题正如你所看到的,创建一个模型,在它所训练的确切数据上做一个很好的预测是很容易的,但要在模型以前从未见过的数据上做出准确的预测就难得多了。当然,这也是在实践中真正重要的数据。例如,如果你创建了一个手写数字分类器(我们很快就会这样做!),并使用它来识别写在支票上的数字,那么你永远不会看到该模型所训练的任何数字--支票会有稍微不同的书写变化需要处理。在本书中,你将学到许多避免过度拟合的方法。然而,你应该在确认过拟合确实发生后再使用这些方法(即你确实观察到验证精度在训练过程中越来越差)。我们经常看到从业者使用避免过度拟合的技术,即使他们有足够的数据,他们不需要这样做,最后得到的模型可能比他们能达到的精度要低。
important: Validation Set: When you train a model, you must always have both a training set and a validation set, and must measure the accuracy of your model only on the validation set. If you train for too long, with not enough data, you will see the accuracy of your model start to get worse; this is called overfitting. fastai defaults
valid_pctto0.2, so even if you forget, fastai will create a validation set for you!
重点:验证集。当你训练一个模型时,你必须总是有一个训练集和一个验证集,而且必须只在验证集上测量你的模型的准确性。如果你训练的时间太长,没有足够的数据,你会看到你的模型的准确性开始变差;这就是所谓的过度拟合。fastai默认valid_pct为0.2,所以即使你忘记了,fastai也会为你创建一个验证集。
The fifth line of the code training our image recognizer tells fastai to create a convolutional neural network (CNN) and specifies what architecture to use (i.e. what kind of model to create), what data we want to train it on, and what metric to use:
learn = vision_learner(dls, resnet34, metrics=error_rate)Why a CNN? It's the current state-of-the-art approach to creating computer vision models. We'll be learning all about how CNNs work in this book. Their structure is inspired by how the human vision system works.
There are many different architectures in fastai, which we will introduce in this book (as well as discussing how to create your own). Most of the time, however, picking an architecture isn't a very important part of the deep learning process. It's something that academics love to talk about, but in practice it is unlikely to be something you need to spend much time on. There are some standard architectures that work most of the time, and in this case we're using one called ResNet that we'll be talking a lot about during the book; it is both fast and accurate for many datasets and problems. The 34 in resnet34 refers to the number of layers in this variant of the architecture (other options are 18, 50, 101, and 152). Models using architectures with more layers take longer to train, and are more prone to overfitting (i.e. you can't train them for as many epochs before the accuracy on the validation set starts getting worse). On the other hand, when using more data, they can be quite a bit more accurate.
What is a metric? A metric is a function that measures the quality of the model's predictions using the validation set, and will be printed at the end of each epoch. In this case, we're using error_rate, which is a function provided by fastai that does just what it says: tells you what percentage of images in the validation set are being classified incorrectly. Another common metric for classification is accuracy (which is just 1.0 - error_rate). fastai provides many more, which will be discussed throughout this book.
The concept of a metric may remind you of loss, but there is an important distinction. The entire purpose of loss is to define a "measure of performance" that the training system can use to update weights automatically. In other words, a good choice for loss is a choice that is easy for stochastic gradient descent to use. But a metric is defined for human consumption, so a good metric is one that is easy for you to understand, and that hews as closely as possible to what you want the model to do. At times, you might decide that the loss function is a suitable metric, but that is not necessarily the case.
训练我们的图像识别器的第五行代码告诉fastai创建一个卷积神经网络(CNN),并指定使用什么架构(即创建什么样的模型),我们要在什么数据上训练它,以及使用什么指标。
learn = vision_learner(dls, resnet34, metrics=error_rate)为什么使用CNN?它是目前最先进的创建计算机视觉模型的方法。我们将在本书中学习所有关于CNN的工作原理。它们的结构是受人类视觉系统工作方式的启发。
fastai中有许多不同的架构,我们将在本书中介绍这些架构(以及讨论如何创建你自己的架构)。然而,大多数时候,挑选一个架构并不是深度学习过程中非常重要的部分。这是学者们喜欢谈论的东西,但在实践中,它不太可能是你需要花很多时间的东西。有一些标准的架构在大多数情况下是有效的,在这种情况下,我们使用的是一个叫做ResNet的架构,我们在书中会经常谈到它;它对许多数据集和问题都是快速而准确的。resnet34中的34指的是这个架构变体中的层数(其他选项是18、50、101和152)。使用更多层的架构的模型需要更长的时间来训练,而且更容易出现过拟合(即在验证集的准确性开始变差之前,你不能训练它们那么多的epochs)。另一方面,当使用更多的数据时,它们可以更准确一些。
什么是度量?度量是一个衡量模型使用验证集的预测质量的函数,并将在每个历时结束时打印。在这种情况下,我们使用error_rate,它是fastai提供的一个函数,正如它所说的那样:告诉你验证集中被错误分类的图像的百分比。另一个常见的分类指标是准确率(也就是1.0 - error_rate)。
度量的概念可能会让你想起损失,但是有一个重要的区别。损失的整个目的是定义一个 "性能的衡量标准",训练系统可以用它来自动更新权重。换句话说,一个好的损失选择是一个容易让随机梯度下降使用的选择。但是,衡量标准是为人类消费而定义的,所以一个好的衡量标准是让你容易理解的,并且尽可能地接近你希望模型做的事情。有时,你可能会决定损失函数是一个合适的度量,但情况并不一定如此。
vision_learner also has a parameter pretrained, which defaults to True (so it's used in this case, even though we haven't specified it), which sets the weights in your model to values that have already been trained by experts to recognize a thousand different categories across 1.3 million photos (using the famous ImageNet dataset). A model that has weights that have already been trained on some other dataset is called a pretrained model. You should nearly always use a pretrained model, because it means that your model, before you've even shown it any of your data, is already very capable. And, as you'll see, in a deep learning model many of these capabilities are things you'll need, almost regardless of the details of your project. For instance, parts of pretrained models will handle edge, gradient, and color detection, which are needed for many tasks.
When using a pretrained model, vision_learner will remove the last layer, since that is always specifically customized to the original training task (i.e. ImageNet dataset classification), and replace it with one or more new layers with randomized weights, of an appropriate size for the dataset you are working with. This last part of the model is known as the head.
Using pretrained models is the most important method we have to allow us to train more accurate models, more quickly, with less data, and less time and money. You might think that would mean that using pretrained models would be the most studied area in academic deep learning... but you'd be very, very wrong! The importance of pretrained models is generally not recognized or discussed in most courses, books, or software library features, and is rarely considered in academic papers. As we write this at the start of 2020, things are just starting to change, but it's likely to take a while. So be careful: most people you speak to will probably greatly underestimate what you can do in deep learning with few resources, because they probably won't deeply understand how to use pretrained models.
Using a pretrained model for a task different to what it was originally trained for is known as transfer learning. Unfortunately, because transfer learning is so under-studied, few domains have pretrained models available. For instance, there are currently few pretrained models available in medicine, making transfer learning challenging to use in that domain. In addition, it is not yet well understood how to use transfer learning for tasks such as time series analysis.
vision_learner还有一个参数pretrained,默认为True(所以在本例中使用,尽管我们没有指定它),它将你的模型中的权重设置为已经被专家训练过的值,以识别130万张照片中的1000个不同类别(使用著名的ImageNet数据集)。一个拥有已经在其他数据集上训练过的权重的模型被称为预训练过的模型。你几乎应该总是使用预训练的模型,因为这意味着你的模型在你向它展示任何数据之前,就已经非常有能力了。而且,正如你将看到的,在深度学习模型中,许多这些能力是你需要的,几乎不考虑你的项目的细节。例如,预训练模型的一部分将处理边缘、梯度和颜色检测,这些都是许多任务所需要的。
当使用预训练模型时,vision_learner会移除最后一层,因为那总是专门为原始训练任务(即ImageNet数据集分类)定制的,并用一个或多个具有随机权重的新层取代它,其大小适合你正在处理的数据集。模型的这最后一部分被称为 "头"。
使用预训练的模型是我们拥有的最重要的方法,可以让我们用更少的数据、更少的时间和金钱来训练更准确的模型,更快。你可能会认为这意味着使用预训练模型将是学术界深度学习中研究最多的领域......但你会非常、非常的错误 预训练模型的重要性在大多数课程、书籍或软件库功能中一般不被认可或讨论,在学术论文中也很少被考虑。当我们在2020年初写这篇文章时,事情刚刚开始改变,但可能需要一段时间。所以要小心:与你交谈的大多数人可能会大大低估你在深度学习中用很少的资源能做什么,因为他们可能不会深刻理解如何使用预训练的模型。
将预训练过的模型用于与它最初训练的任务不同的任务,被称为迁移学习。不幸的是,由于迁移学习的研究太少,很少有领域有预训练的模型可用。例如,目前在医学领域很少有预训练的模型可用,这使得迁移学习在该领域的使用具有挑战性。此外,人们对如何将迁移学习用于时间序列分析等任务还不甚了解。
jargon: Transfer learning: Using a pretrained model for a task different to what it was originally trained for.
learn = vision_learner(dls, resnet34, metrics=error_rate)术语:迁移学习。将预先训练好的模型用于与最初训练的不同的任务。
learn = vision_learner(dls, resnet34, metrics=error_rate)The sixth line of our code tells fastai how to fit the model:
learn.fine_tune(1)As we've discussed, the architecture only describes a template for a mathematical function; it doesn't actually do anything until we provide values for the millions of parameters it contains.
This is the key to deep learning—determining how to fit the parameters of a model to get it to solve your problem. In order to fit a model, we have to provide at least one piece of information: how many times to look at each image (known as number of epochs). The number of epochs you select will largely depend on how much time you have available, and how long you find it takes in practice to fit your model. If you select a number that is too small, you can always train for more epochs later.
But why is the method called fine_tune, and not fit? fastai actually does have a method called fit, which does indeed fit a model (i.e. look at images in the training set multiple times, each time updating the parameters to make the predictions closer and closer to the target labels). But in this case, we've started with a pretrained model, and we don't want to throw away all those capabilities that it already has. As you'll learn in this book, there are some important tricks to adapt a pretrained model for a new dataset—a process called fine-tuning.
我们代码的第六行告诉fastai如何拟合模型。
learn.fine_tune(1)正如我们所讨论的,架构只是描述了一个数学函数的模板;在我们为它所包含的数百万个参数提供数值之前,它实际上并没有做任何事情。
这是深度学习的关键--确定如何拟合一个模型的参数,让它解决你的问题。为了拟合一个模型,我们必须提供至少一个信息:每张图片要看多少次(称为epochs的数量)。你选择的历时数主要取决于你有多少时间,以及你发现在实践中需要多长时间来拟合你的模型。如果你选择了一个太小的数字,你可以在以后训练更多的epochs。
但为什么这个方法叫fine_tune,而不是fit? fastai实际上有一个叫fit的方法,它确实可以拟合一个模型(即多次查看训练集中的图像,每次更新参数,使预测结果越来越接近目标标签)。但在这种情况下,我们已经从一个预训练的模型开始,我们不想丢掉它已经具备的所有能力。正如你在本书中学到的,有一些重要的技巧可以让预训练过的模型适应新的数据集--这个过程叫做微调。
jargon: Fine-tuning: A transfer learning technique where the parameters of a pretrained model are updated by training for additional epochs using a different task to that used for pretraining.
术语:微调:一种迁移学习技术,通过使用与预训练不同的任务进行额外的历时训练来更新预训练模型的参数。
When you use the fine_tune method, fastai will use these tricks for you. There are a few parameters you can set (which we'll discuss later), but in the default form shown here, it does two steps:
- Use one epoch to fit just those parts of the model necessary to get the new random head to work correctly with your dataset.
- Use the number of epochs requested when calling the method to fit the entire model, updating the weights of the later layers (especially the head) faster than the earlier layers (which, as we'll see, generally don't require many changes from the pretrained weights).
The head of a model is the part that is newly added to be specific to the new dataset. An epoch is one complete pass through the dataset. After calling fit, the results after each epoch are printed, showing the epoch number, the training and validation set losses (the "measure of performance" used for training the model), and any metrics you've requested (error rate, in this case).
当你使用fine_tune方法时,fastai将为你使用这些技巧。你可以设置一些参数(我们将在后面讨论),但在这里显示的默认形式下,它做两个步骤。
- 使用一个世代来拟合模型中那些必要的部分,以使新的随机头能与你的数据集正确地工作。
- 使用调用该方法时要求的世代数来拟合整个模型,更新后面各层(尤其是头部)的权重比前面各层(正如我们将看到的,一般不需要对预训练的权重做太多改变)更快。
一个模型的头部是新添加的部分,专门用于新的数据集。一个epoch是对数据集的一次完整传递。调用fit后,每个epoch后的结果都会被打印出来,显示epoch数、训练和验证集损失(用于训练模型的 "性能测量"),以及你要求的任何指标(本例中为错误率)。
So, with all this code our model learned to recognize cats and dogs just from labeled examples. But how did it do it?
因此,通过所有这些代码,我们的模型学会了仅从标记的例子中识别猫和狗。但它是如何做到的呢?
What Our Image Recognizer Learned
我们的图像识别器学到了什么
At this stage we have an image recognizer that is working very well, but we have no idea what it is actually doing! Although many people complain that deep learning results in impenetrable "black box" models (that is, something that gives predictions but that no one can understand), this really couldn't be further from the truth. There is a vast body of research showing how to deeply inspect deep learning models, and get rich insights from them. Having said that, all kinds of machine learning models (including deep learning, and traditional statistical models) can be challenging to fully understand, especially when considering how they will behave when coming across data that is very different to the data used to train them. We'll be discussing this issue throughout this book.
In 2013 a PhD student, Matt Zeiler, and his supervisor, Rob Fergus, published the paper "Visualizing and Understanding Convolutional Networks", which showed how to visualize the neural network weights learned in each layer of a model. They carefully analyzed the model that won the 2012 ImageNet competition, and used this analysis to greatly improve the model, such that they were able to go on to win the 2013 competition! <<img_layer1>> is the picture that they published of the first layer's weights.
在这个阶段,我们有一个工作得非常好的图像识别器,但我们不知道它到底在做什么! 虽然很多人抱怨说深度学习的结果是坚不可摧的 "黑匣子 "模型(也就是说,有些东西可以提供预测,但没有人能够理解),但这确实与事实相去甚远。有大量的研究表明,如何深入检查深度学习模型,并从中获得丰富的洞察力。尽管如此,所有类型的机器学习模型(包括深度学习和传统的统计模型)都是具有挑战性的,特别是当考虑到它们在遇到与训练它们的数据非常不同的数据时,会有什么表现。我们将在本书中讨论这个问题。
2013年,一位博士生Matt Zeiler和他的导师Rob Fergus发表了 "可视化和理解卷积网络 "的论文,其中展示了如何可视化在模型的每一层学到的神经网络权重。他们仔细分析了在2012年ImageNet比赛中获胜的模型,并利用这些分析大大改进了模型,从而使他们能够在2013年的比赛中继续获胜 <<img_layer1>>是他们公布的第一层权重的图片。

This picture requires some explanation. For each layer, the image part with the light gray background shows the reconstructed weights pictures, and the larger section at the bottom shows the parts of the training images that most strongly matched each set of weights. For layer 1, what we can see is that the model has discovered weights that represent diagonal, horizontal, and vertical edges, as well as various different gradients. (Note that for each layer only a subset of the features are shown; in practice there are thousands across all of the layers.) These are the basic building blocks that the model has learned for computer vision. They have been widely analyzed by neuroscientists and computer vision researchers, and it turns out that these learned building blocks are very similar to the basic visual machinery in the human eye, as well as the handcrafted computer vision features that were developed prior to the days of deep learning. The next layer is represented in <<img_layer2>>.
这张图片需要一些解释。对于每一层,浅灰色背景的图像部分显示了重建的权重图片,而底部较大的部分显示了训练图像中与每组权重最匹配的部分。对于第1层,我们可以看到的是,该模型发现了代表对角线、水平和垂直边缘的权重,以及各种不同的梯度。(请注意,每一层都只显示了一个特征子集;实际上,所有的层都有成千上万的特征)。这些是模型为计算机视觉学习的基本构件。神经科学家和计算机视觉研究人员已经对它们进行了广泛的分析,事实证明,这些学习到的构件与人眼的基本视觉机器以及在深度学习时代之前开发的手工计算机视觉特征非常相似。下一层用<<img_layer2>>表示。

For layer 2, there are nine examples of weight reconstructions for each of the features found by the model. We can see that the model has learned to create feature detectors that look for corners, repeating lines, circles, and other simple patterns. These are built from the basic building blocks developed in the first layer. For each of these, the right-hand side of the picture shows small patches from actual images which these features most closely match. For instance, the particular pattern in row 2, column 1 matches the gradients and textures associated with sunsets.
<<img_layer3>> shows the image from the paper showing the results of reconstructing the features of layer 3.
对于第2层,有9个关于模型发现的每个特征的权重重建的例子。我们可以看到,该模型已经学会了创建特征检测器,以寻找拐角、重复的线条、圆圈和其他简单的模式。这些都是由第一层中开发的基本构件建立的。对于其中的每一个,图片的右侧显示了这些特征最接近的实际图像中的小斑块。例如,第2行第1列中的特定图案与日落相关的梯度和纹理相匹配。
<<img_layer3>>显示了论文中的图片和重建第3层特征的结果。
As you can see by looking at the righthand side of this picture, the features are now able to identify and match with higher-level semantic components, such as car wheels, text, and flower petals. Using these components, layers four and five can identify even higher-level concepts, as shown in <<img_layer4>>.
通过观察这张图片的右侧可以看到,现在的特征能够识别并与更高层次的语义成分相匹配,例如汽车车轮、文字和花瓣。利用这些组件,第四层和第五层可以识别更高层次的概念,如<<img_layer4>>所示。
This article was studying an older model called AlexNet that only contained five layers. Networks developed since then can have hundreds of layers—so you can imagine how rich the features developed by these models can be!
When we fine-tuned our pretrained model earlier, we adapted what those last layers focus on (flowers, humans, animals) to specialize on the cats versus dogs problem. More generally, we could specialize such a pretrained model on many different tasks. Let's have a look at some examples.
这篇文章研究的是一个叫做AlexNet的旧模型,它只包含五层。从那时起开发的网络可以有数百个层--所以你可以想象这些模型开发的功能有多丰富!
当我们先前对预训练模型进行微调时,我们调整了最后几层的关注点(花、人、动物),以专门处理猫和狗的问题。更广泛地说,我们可以将这样的预训练模型专门用于许多不同的任务。让我们看一下一些例子。
Image Recognizers Can Tackle Non-Image Tasks
图像识别器可以处理非图像任务
An image recognizer can, as its name suggests, only recognize images. But a lot of things can be represented as images, which means that an image recogniser can learn to complete many tasks.
For instance, a sound can be converted to a spectrogram, which is a chart that shows the amount of each frequency at each time in an audio file. Fast.ai student Ethan Sutin used this approach to easily beat the published accuracy of a state-of-the-art environmental sound detection model using a dataset of 8,732 urban sounds. fastai's show_batch clearly shows how each different sound has a quite distinctive spectrogram, as you can see in <<img_spect>>.
顾名思义,图像识别器只能识别图像。但很多东西都可以用图像来表示,这意味着图像识别器可以学习完成很多任务。
例如,一个声音可以被转换为频谱图,这是一个显示音频文件中每个时间段的每个频率的数量的图表。Fast.ai的学生Ethan Sutin用这种方法轻松地击败了最先进的环境声音检测模型的公布的准确性,使用的数据集包括8732个城市声音。Fastai的show_batch清楚地显示了每个不同的声音都有相当独特的频谱图,你可以在<<img_spect>>中看到。

A time series can easily be converted into an image by simply plotting the time series on a graph. However, it is often a good idea to try to represent your data in a way that makes it as easy as possible to pull out the most important components. In a time series, things like seasonality and anomalies are most likely to be of interest. There are various transformations available for time series data. For instance, fast.ai student Ignacio Oguiza created images from a time series dataset for olive oil classification, using a technique called Gramian Angular Difference Field (GADF); you can see the result in <<ts_image>>. He then fed those images to an image classification model just like the one you see in this chapter. His results, despite having only 30 training set images, were well over 90% accurate, and close to the state of the art.
通过简单地将时间序列绘制在图表上,一个时间序列可以很容易地被转换成图像。然而,尝试用一种能使你的数据尽可能容易地拉出最重要的组成部分的方式来表示你的数据往往是一个好主意。在一个时间序列中,像季节性和反常现象这样的东西最有可能被关注。对于时间序列数据,有多种转换方式。例如,fast.ai的学生Ignacio Oguiza使用一种叫做Gramian Angular Difference Field(GADF)的技术,从一个时间序列数据集中创建了用于橄榄油分类的图像;你可以在<>中看到这个结果。然后他将这些图像输入到一个图像分类模型中,就像你在本章中看到的那个模型一样。他的结果,尽管只有30张训练集图像,但准确率远远超过90%,并且接近技术水平。

Another interesting fast.ai student project example comes from Gleb Esman. He was working on fraud detection at Splunk, using a dataset of users' mouse movements and mouse clicks. He turned these into pictures by drawing an image where the position, speed, and acceleration of the mouse pointer was displayed using coloured lines, and the clicks were displayed using small colored circles, as shown in <>. He then fed this into an image recognition model just like the one we've used in this chapter, and it worked so well that it led to a patent for this approach to fraud analytics!
另一个有趣的fast.ai学生项目案例来自Gleb Esman。他在Splunk从事欺诈检测工作,使用的是用户的鼠标动作和鼠标点击的数据集。他把这些变成了图片,绘制了一张图片,其中鼠标指针的位置、速度和加速度用彩色线条显示,而点击则用彩色小圆圈显示,如<>所示。然后,他将其输入图像识别模型,就像我们在本章中使用的模型一样,效果非常好,以至于为这种欺诈分析方法申请了专利!

Another example comes from the paper "Malware Classification with Deep Convolutional Neural Networks" by Mahmoud Kalash et al., which explains that "the malware binary file is divided into 8-bit sequences which are then converted to equivalent decimal values. This decimal vector is reshaped and a gray-scale image is generated that represents the malware sample," like in <<malware_proc>>.
另一个例子来自Mahmoud Kalash等人的论文 "Malware Classification with Deep Convolutional Neural Networks",其中解释说:"恶意软件的二进制文件被分为8位序列,然后被转换为等价的十进制值。这个十进制矢量被重塑,并生成一个代表恶意软件样本的灰度图像,"就像<<malware_proc>>中所说的。

The authors then show "pictures" generated through this process of malware in different categories, as shown in <<malware_eg>>.
然后,作者展示了通过这个过程产生的不同类别的恶意软件的 "图片",如<<malware_eg>>所示。

As you can see, the different types of malware look very distinctive to the human eye. The model the researchers trained based on this image representation was more accurate at malware classification than any previous approach shown in the academic literature. This suggests a good rule of thumb for converting a dataset into an image representation: if the human eye can recognize categories from the images, then a deep learning model should be able to do so too.
In general, you'll find that a small number of general approaches in deep learning can go a long way, if you're a bit creative in how you represent your data! You shouldn't think of approaches like the ones described here as "hacky workarounds," because actually they often (as here) beat previously state-of-the-art results. These really are the right ways to think about these problem domains.
正如你所看到的,不同类型的恶意软件在人眼里看起来非常有特点。研究人员根据这种图像表示法训练的模型在恶意软件分类方面比以前学术文献中显示的任何方法都要准确。这表明将数据集转换为图像表示的一个很好的经验法则:如果人眼能够从图像中识别出类别,那么深度学习模型也应该能够做到这一点。
总的来说,你会发现深度学习中的少量一般方法可以发挥很大的作用,如果你在如何表示你的数据方面有点创意的话 你不应该认为像这里描述的方法是 "黑客的变通方法",因为实际上他们经常(如这里)击败以前最先进的结果。这些确实是思考这些问题领域的正确方法。
Jargon Recap
术语回顾
We just covered a lot of information so let's recap briefly, <> provides a handy vocabulary.
.Deep learning vocabulary
[options="header"]
|=====
| Term | Meaning
|Label | The data that we're trying to predict, such as "dog" or "cat"
|Architecture | The _template_ of the model that we're trying to fit; the actual mathematical function that we're passing the input data and parameters to
|Model | The combination of the architecture with a particular set of parameters
|Parameters | The values in the model that change what task it can do, and are updated through model training
|Fit | Update the parameters of the model such that the predictions of the model using the input data match the target labels
|Train | A synonym for _fit_
|Pretrained model | A model that has already been trained, generally using a large dataset, and will be fine-tuned
|Fine-tune | Update a pretrained model for a different task
|Epoch | One complete pass through the input data
|Loss | A measure of how good the model is, chosen to drive training via SGD
|Metric | A measurement of how good the model is, using the validation set, chosen for human consumption
|Validation set | A set of data held out from training, used only for measuring how good the model is
|Training set | The data used for fitting the model; does not include any data from the validation set
|Overfitting | Training a model in such a way that it _remembers_ specific features of the input data, rather than generalizing well to data not seen during training
|CNN | Convolutional neural network; a type of neural network that works particularly well for computer vision tasks
|=====我们刚刚涵盖了很多信息,所以让我们简单地回顾一下,<>提供了一个方便的词汇表。
.Deep learning vocabulary
[options="header"]
|=====
| Term | Meaning
|Label | The data that we're trying to predict, such as "dog" or "cat"
|Architecture | The _template_ of the model that we're trying to fit; the actual mathematical function that we're passing the input data and parameters to
|Model | The combination of the architecture with a particular set of parameters
|Parameters | The values in the model that change what task it can do, and are updated through model training
|Fit | Update the parameters of the model such that the predictions of the model using the input data match the target labels
|Train | A synonym for _fit_
|Pretrained model | A model that has already been trained, generally using a large dataset, and will be fine-tuned
|Fine-tune | Update a pretrained model for a different task
|Epoch | One complete pass through the input data
|Loss | A measure of how good the model is, chosen to drive training via SGD
|Metric | A measurement of how good the model is, using the validation set, chosen for human consumption
|Validation set | A set of data held out from training, used only for measuring how good the model is
|Training set | The data used for fitting the model; does not include any data from the validation set
|Overfitting | Training a model in such a way that it _remembers_ specific features of the input data, rather than generalizing well to data not seen during training
|CNN | Convolutional neural network; a type of neural network that works particularly well for computer vision tasks
|=====With this vocabulary in hand, we are now in a position to bring together all the key concepts introduced so far. Take a moment to review those definitions and read the following summary. If you can follow the explanation, then you're well equipped to understand the discussions to come.
Machine learning is a discipline where we define a program not by writing it entirely ourselves, but by learning from data. Deep learning is a specialty within machine learning that uses neural networks with multiple layers. Image classification is a representative example (also known as image recognition). We start with labeled data; that is, a set of images where we have assigned a label to each image indicating what it represents. Our goal is to produce a program, called a model, which, given a new image, will make an accurate prediction regarding what that new image represents.
Every model starts with a choice of architecture, a general template for how that kind of model works internally. The process of training (or fitting) the model is the process of finding a set of parameter values (or weights) that specialize that general architecture into a model that works well for our particular kind of data. In order to define how well a model does on a single prediction, we need to define a loss function, which determines how we score a prediction as good or bad.
To make the training process go faster, we might start with a pretrained model—a model that has already been trained on someone else's data. We can then adapt it to our data by training it a bit more on our data, a process called fine-tuning.
When we train a model, a key concern is to ensure that our model generalizes—that is, that it learns general lessons from our data which also apply to new items it will encounter, so that it can make good predictions on those items. The risk is that if we train our model badly, instead of learning general lessons it effectively memorizes what it has already seen, and then it will make poor predictions about new images. Such a failure is called overfitting. In order to avoid this, we always divide our data into two parts, the training set and the validation set. We train the model by showing it only the training set and then we evaluate how well the model is doing by seeing how well it performs on items from the validation set. In this way, we check if the lessons the model learns from the training set are lessons that generalize to the validation set. In order for a person to assess how well the model is doing on the validation set overall, we define a metric. During the training process, when the model has seen every item in the training set, we call that an epoch.
All these concepts apply to machine learning in general. That is, they apply to all sorts of schemes for defining a model by training it with data. What makes deep learning distinctive is a particular class of architectures: the architectures based on neural networks. In particular, tasks like image classification rely heavily on convolutional neural networks, which we will discuss shortly.
有了这些词汇,我们现在就可以把迄今为止介绍的所有关键概念汇集起来。花点时间回顾一下这些定义,并阅读下面的总结。如果你能听懂解释,那么你就有能力理解接下来的讨论了。
机器学习是一门学科,我们不是通过完全自己编写程序,而是通过从数据中学习来定义一个程序。深度学习是机器学习中的一个专业,它使用具有多层的神经网络。图像分类是一个代表性的例子(也被称为图像识别)。我们从有标签的数据开始;也就是说,在一组图像中,我们为每张图像分配了一个标签,表明它代表什么。我们的目标是产生一个程序,称为模型,给定一个新的图像,它将对该新图像代表的内容作出准确的预测。
每个模型开始时都有一个架构的选择,即那种模型内部如何工作的一般模板。训练(或拟合)模型的过程是寻找一组参数值(或权重)的过程,将一般的架构专门化为一个对我们特定的数据类型工作良好的模型。为了定义一个模型在单个预测上的表现,我们需要定义一个损失函数,它决定了我们如何对一个预测的好坏进行评分。
为了使训练过程进行得更快,我们可能从一个预训练的模型开始--一个已经在别人的数据上训练过的模型。然后,我们可以通过在我们的数据上进行更多的训练来使其适应我们的数据,这个过程称为微调。
当我们训练一个模型时,一个关键的问题是要确保我们的模型具有普适性--也就是说,它从我们的数据中学习到的一般经验也适用于它将遇到的新项目,这样它就能对这些项目做出良好的预测。风险在于,如果我们的模型训练得不好,而不是学习一般的经验,它就会有效地记住它已经看到的东西,然后它就会对新的图像做出糟糕的预测。这样的失败被称为过度拟合。为了避免这种情况,我们总是把我们的数据分成两部分,训练集和验证集。我们只用训练集来训练模型,然后通过观察模型在验证集的项目上的表现来评估模型的表现。通过这种方式,我们检查模型从训练集学到的经验是否可以推广到验证集上。为了让人们能够评估模型在验证集上的总体表现,我们定义了一个指标。在训练过程中,当模型看到训练集中的每一个项目时,我们称其为一个 epoch。
所有这些概念都适用于一般的机器学习。也就是说,它们适用于各种通过用数据训练来定义模型的方案。让深度学习与众不同的是一类特殊的架构:基于神经网络的架构。特别是,像图像分类这样的任务在很大程度上依赖于卷积神经网络,我们很快就会讨论这个问题。
Deep Learning Is Not Just for Image Classification
深度学习不只适用于图像分类
Deep learning's effectiveness for classifying images has been widely discussed in recent years, even showing superhuman results on complex tasks like recognizing malignant tumors in CT scans. But it can do a lot more than this, as we will show here.
For instance, let's talk about something that is critically important for autonomous vehicles: localizing objects in a picture. If a self-driving car doesn't know where a pedestrian is, then it doesn't know how to avoid one! Creating a model that can recognize the content of every individual pixel in an image is called segmentation. Here is how we can train a segmentation model with fastai, using a subset of the Camvid dataset from the paper "Semantic Object Classes in Video: A High-Definition Ground Truth Database" by Gabruel J. Brostow, Julien Fauqueur, and Roberto Cipolla:
近年来,深度学习对图像分类的有效性被广泛讨论,甚至在识别CT扫描中的恶性肿瘤等复杂任务上显示出超过人工的结果。但它能做的远不止这些,正如我们将在这里展示的。
例如,让我们来谈谈对自动驾驶汽车至关重要的事情:定位图片中的物体。如果一辆自动驾驶汽车不知道哪里有行人,那么它就不知道如何避开行人! 创建一个能够识别图像中每个像素内容的模型被称为分割。下面是我们如何用fastai训练一个分割模型,使用论文《视频中的语义物体类别》中的Camvid数据集的一个子集。一个高清晰度的真实数据库",作者是Gabruel J. Brostow, Julien Fauqueur, 和Roberto Cipolla。
path = untar_data(URLs.CAMVID_TINY)
dls = SegmentationDataLoaders.from_label_func(
path, bs=8, fnames = get_image_files(path/"images"),
label_func = lambda o: path/'labels'/f'{o.stem}_P{o.suffix}',
codes = np.loadtxt(path/'codes.txt', dtype=str)
)
learn = unet_learner(dls, resnet34)
learn.fine_tune(8)Output
<IPython.core.display.HTML object>
| epoch | train_loss | valid_loss | time |
|---|---|---|---|
| 0 | 2.641862 | 2.140568 | 00:02 |
<IPython.core.display.HTML object>
| epoch | train_loss | valid_loss | time |
|---|---|---|---|
| 0 | 1.624964 | 1.464210 | 00:02 |
| 1 | 1.454148 | 1.284032 | 00:02 |
| 2 | 1.342955 | 1.048562 | 00:02 |
| 3 | 1.199765 | 0.852787 | 00:02 |
| 4 | 1.078090 | 0.838206 | 00:02 |
| 5 | 0.975496 | 0.746806 | 00:02 |
| 6 | 0.892793 | 0.725384 | 00:02 |
| 7 | 0.827645 | 0.726778 | 00:02 |
We are not even going to walk through this code line by line, because it is nearly identical to our previous example! (Although we will be doing a deep dive into segmentation models in <<chapter_arch_details>>, along with all of the other models that we are briefly introducing in this chapter, and many, many more.)
We can visualize how well it achieved its task, by asking the model to color-code each pixel of an image. As you can see, it nearly perfectly classifies every pixel in every object. For instance, notice that all of the cars are overlaid with the same color and all of the trees are overlaid with the same color (in each pair of images, the lefthand image is the ground truth label and the right is the prediction from the model):
我们甚至不打算逐行浏览这段代码,因为它与我们之前的例子几乎完全相同!(尽管我们将在<>中深入研究分割模型,以及我们在本章中简要介绍的所有其他模型。(尽管我们将在<>中对分割模型进行深入研究,以及我们在本章中简要介绍的所有其他模型,还有很多很多)。
我们可以通过要求该模型对图像的每个像素进行颜色编码,来直观地了解它的任务完成得如何。正如你所看到的,它几乎完美地对每个物体中的每个像素进行了分类。例如,注意到所有的汽车都用相同的颜色覆盖,所有的树都用相同的颜色覆盖(在每一对图像中,左边的图像是真实标签,右边的是模型的预测)。
learn.show_results(max_n=6, figsize=(7,8))Output
<IPython.core.display.HTML object>
<Figure size 504x576 with 4 Axes>
[省略较大 image/png 输出]
One other area where deep learning has dramatically improved in the last couple of years is natural language processing (NLP). Computers can now generate text, translate automatically from one language to another, analyze comments, label words in sentences, and much more. Here is all of the code necessary to train a model that can classify the sentiment of a movie review better than anything that existed in the world just five years ago:
在过去几年中,深度学习得到极大改善的另一个领域是自然语言处理(NLP)。计算机现在可以生成文本,从一种语言自动翻译成另一种语言,分析评论,标记句子中的单词,以及更多。这里是训练一个模型所需的所有代码,该模型可以对电影评论的情绪进行分类,比五年前世界上存在的任何东西都好。
from fastai.text.all import *
dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test')
learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy)
learn.fine_tune(4, 1e-2)Output
<IPython.core.display.HTML object>
| epoch | train_loss | valid_loss | accuracy | time |
|---|---|---|---|---|
| 0 | 0.878776 | 0.748753 | 0.500400 | 01:27 |
<IPython.core.display.HTML object>
| epoch | train_loss | valid_loss | accuracy | time |
|---|---|---|---|---|
| 0 | 0.679118 | 0.674778 | 0.584040 | 02:45 |
| 1 | 0.653671 | 0.670396 | 0.618040 | 02:55 |
| 2 | 0.598665 | 0.551815 | 0.718920 | 05:28 |
| 3 | 0.556812 | 0.507450 | 0.752480 | 03:11 |
#clean If you hit a "CUDA out of memory error" after running this cell, click on the menu Kernel, then restart. Instead of executing the cell above, copy and paste the following code in it:
from fastai.text.all import *
dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test', bs=32)
learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy)
learn.fine_tune(4, 1e-2)This reduces the batch size to 32 (we will explain this later). If you keep hitting the same error, change 32 to 16.
#清除 如果你在运行这个单元后遇到了 "CUDA内存不足的错误",点击菜单Kernel,然后重新启动。不要执行上面的单元格,而是复制并粘贴下面的代码。
from fastai.text.all import *
dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test', bs=32)
learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy)
learn.fine_tune(4, 1e-2)这就把批处理量减少到32(我们将在后面解释)。如果你一直遇到同样的错误,把32改为16。
This model is using the "IMDb Large Movie Review dataset" from the paper "Learning Word Vectors for Sentiment Analysis" by Andrew Maas et al. It works well with movie reviews of many thousands of words, but let's test it out on a very short one to see how it does its thing:
这个模型使用的是Andrew Maas等人的论文 "Learning Word Vectors for Sentiment Analysis "中的 "IMDb Large Movie Review dataset",它在处理成千上万字的电影评论时效果很好,但让我们在一个很短的评论上测试一下,看看它是如何做的。
learn.predict("I really liked that movie!")Output
<IPython.core.display.HTML object>
('neg', tensor(0), tensor([0.8786, 0.1214]))Here we can see the model has considered the review to be positive. The second part of the result is the index of "pos" in our data vocabulary and the last part is the probabilities attributed to each class (99.6% for "pos" and 0.4% for "neg").
Now it's your turn! Write your own mini movie review, or copy one from the internet, and you can see what this model thinks about it.
在这里,我们可以看到模型认为该评论是积极的。结果的第二部分是 "正面 "在我们的数据词汇中的索引,最后一部分是归属于每个类别的概率("正面 "为99.6%,"负面 "为0.4%)。
现在轮到你了! 写下你自己的迷你影评,或者从互联网上复制一个,你可以看看这个模型对它的看法。
Sidebar: The Order Matters
题外话:顺序很重要
In a Jupyter notebook, the order in which you execute each cell is very important. It's not like Excel, where everything gets updated as soon as you type something anywhere—it has an inner state that gets updated each time you execute a cell. For instance, when you run the first cell of the notebook (with the "CLICK ME" comment), you create an object called learn that contains a model and data for an image classification problem. If we were to run the cell just shown in the text (the one that predicts if a review is good or not) straight after, we would get an error as this learn object does not contain a text classification model. This cell needs to be run after the one containing:
from fastai.text.all import *
dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test')
learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5,
metrics=accuracy)
learn.fine_tune(4, 1e-2)The outputs themselves can be deceiving, because they include the results of the last time the cell was executed; if you change the code inside a cell without executing it, the old (misleading) results will remain.
Except when we mention it explicitly, the notebooks provided on the book website are meant to be run in order, from top to bottom. In general, when experimenting, you will find yourself executing cells in any order to go fast (which is a super neat feature of Jupyter Notebook), but once you have explored and arrived at the final version of your code, make sure you can run the cells of your notebooks in order (your future self won't necessarily remember the convoluted path you took otherwise!).
In command mode, pressing 0 twice will restart the kernel (which is the engine powering your notebook). This will wipe your state clean and make it as if you had just started in the notebook. Choose Run All Above from the Cell menu to run all cells above the point where you are. We have found this to be very useful when developing the fastai library.
在Jupyter笔记本中,你执行每个单元格的顺序是非常重要的。它不像Excel那样,只要你在任何地方输入东西,一切都会被更新--它有一个内部状态,在你每次执行单元格时都会被更新。例如,当你运行笔记本的第一个单元格(带有 "CLICK ME "注释)时,你创建了一个名为Learn的对象,其中包含一个模型和图像分类问题的数据。如果我们直接运行文本中刚刚显示的单元(预测评论是否好的单元),我们会得到一个错误,因为这个学习对象不包含文本分类模型。这个单元需要在包含的那个单元之后运行。
from fastai.text.all import *
dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test')
learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5,
metrics=accuracy)
learn.fine_tune(4, 1e-2)输出本身可能具有欺骗性,因为它们包括单元格最后一次被执行的结果;如果你改变了单元格内的代码而不执行它,旧的(误导性的)结果将被保留。
除非我们明确提到,本书网站上提供的笔记本是要按顺序运行的,从上到下。一般来说,在实验时,你会发现自己以任何顺序执行单元,以达到快速的目的(这是Jupyter笔记本的一个超级整洁的功能),但是一旦你已经探索并到达了你的代码的最终版本,请确保你可以按顺序运行你的笔记本的单元(否则你未来的自己不一定会记得你走过的曲折的路径!)。
在命令模式下,按两次 "0 "将重新启动内核(它是为你的笔记本提供动力的引擎)。这将把你的状态清除干净,使之成为你刚开始使用的笔记本。从 "单元 "菜单中选择 "运行上面的所有单元 "来运行你所在的点上面的所有单元。我们发现这在开发fastai库时非常有用。
End sidebar
题外话结束
If you ever have any questions about a fastai method, you should use the function doc, passing it the method name:
doc(learn.predict)This will make a small window pop up with content like this:

如果你对一个fastai方法有任何疑问,你应该使用函数文档,把方法名称传给它。
doc(learn.predict)这将使一个小窗口弹出,内容如下。

A brief one-line explanation is provided by doc. The "Show in docs" link takes you to the full documentation, where you'll find all the details and lots of examples. Also, most of fastai's methods are just a handful of lines, so you can click the "source" link to see exactly what's going on behind the scenes.
Let's move on to something much less sexy, but perhaps significantly more widely commercially useful: building models from plain tabular data.
文档中提供了一个简短的单行解释。"显示在文档中 "的链接会带你到完整的文档,在那里你会发现所有的细节和很多的例子。另外,fastai的大多数方法都只有几行,所以你可以点击 "源码 "链接,看看幕后到底发生了什么。
让我们来看看一些不那么诱人的东西,但也许更广泛的商业用途:从普通的表格数据建立模型。
jargon: Tabular: Data that is in the form of a table, such as from a spreadsheet, database, or CSV file. A tabular model is a model that tries to predict one column of a table based on information in other columns of the table.
术语:表格式:以表格形式存在的数据,如来自电子表格、数据库或CSV文件的数据。表格式模型是一个试图根据表中其他列的信息来预测一列的模型。
It turns out that looks very similar too. Here is the code necessary to train a model that will predict whether a person is a high-income earner, based on their socioeconomic background:
事实证明,这看起来也非常相似。下面是训练一个模型所需的代码,该模型将根据一个人的社会经济背景,预测他是否是高收入者。
from fastai.tabular.all import *
path = untar_data(URLs.ADULT_SAMPLE)
dls = TabularDataLoaders.from_csv(path/'adult.csv', path=path, y_names="salary",
cat_names = ['workclass', 'education', 'marital-status', 'occupation',
'relationship', 'race'],
cont_names = ['age', 'fnlwgt', 'education-num'],
procs = [Categorify, FillMissing, Normalize])
learn = tabular_learner(dls, metrics=accuracy)As you see, we had to tell fastai which columns are categorical (that is, contain values that are one of a discrete set of choices, such as occupation) and which are continuous (that is, contain a number that represents a quantity, such as age).
There is no pretrained model available for this task (in general, pretrained models are not widely available for any tabular modeling tasks, although some organizations have created them for internal use), so we don't use fine_tune in this case. Instead we use fit_one_cycle, the most commonly used method for training fastai models from scratch (i.e. without transfer learning):
正如你所看到的,我们必须告诉fastai哪些列是分类的(也就是说,包含的值是一组离散的选择之一,如职业),哪些是连续的(也就是说,包含一个代表数量的数字,如年龄)。
这个任务没有预训练的模型(一般来说,预训练的模型并不广泛用于任何表格建模任务,尽管一些组织已经创建了它们供内部使用),所以我们在这种情况下没有使用fine_tune。相反,我们使用fit_one_cycle,这是最常用的从头开始训练fastai模型的方法(即没有迁移学习)。
learn.fit_one_cycle(3)Output
<IPython.core.display.HTML object>
| epoch | train_loss | valid_loss | accuracy | time |
|---|---|---|---|---|
| 0 | 0.372397 | 0.357177 | 0.832463 | 00:08 |
| 1 | 0.351544 | 0.341505 | 0.841523 | 00:08 |
| 2 | 0.338763 | 0.339184 | 0.845670 | 00:08 |
This model is using the Adult dataset, from the paper "Scaling Up the Accuracy of Naive-Bayes Classifiers: a Decision-Tree Hybrid" by Rob Kohavi, which contains some demographic data about individuals (like their education, marital status, race, sex, and whether or not they have an annual income greater than $50k). The model is over 80% accurate, and took around 30 seconds to train.
这个模型使用的是Adult数据集,来自Rob Kohavi的论文 "Scaling Up the Accuracy of Naive-Bayes Classifiers: a Decision-Tree Hybrid",其中包含一些关于个人的人口数据(比如他们的教育、婚姻状况、种族、性别以及他们的年收入是否超过5万美元)。该模型的准确率超过80%,并花了大约30秒的时间来训练。
Let's look at one more. Recommendation systems are very important, particularly in e-commerce. Companies like Amazon and Netflix try hard to recommend products or movies that users might like. Here's how to train a model that will predict movies people might like, based on their previous viewing habits, using the MovieLens dataset:
让我们再看一个。推荐系统是非常重要的,特别是在电子商务领域。像亚马逊和Netflix这样的公司努力向用户推荐他们可能喜欢的产品或电影。下面是如何使用MovieLens数据集来训练一个模型,根据人们以前的观看习惯来预测他们可能喜欢的电影。
from fastai.collab import *
path = untar_data(URLs.ML_SAMPLE)
dls = CollabDataLoaders.from_csv(path/'ratings.csv')
learn = collab_learner(dls, y_range=(0.5,5.5))
learn.fine_tune(10)Output
<IPython.core.display.HTML object>
| epoch | train_loss | valid_loss | time |
|---|---|---|---|
| 0 | 1.510897 | 1.410028 | 00:00 |
<IPython.core.display.HTML object>
| epoch | train_loss | valid_loss | time |
|---|---|---|---|
| 0 | 1.375435 | 1.350930 | 00:00 |
| 1 | 1.270062 | 1.173962 | 00:00 |
| 2 | 1.023159 | 0.879298 | 00:00 |
| 3 | 0.797398 | 0.739787 | 00:00 |
| 4 | 0.685500 | 0.700903 | 00:00 |
| 5 | 0.646508 | 0.686387 | 00:00 |
| 6 | 0.623985 | 0.681087 | 00:00 |
| 7 | 0.606319 | 0.676885 | 00:00 |
| 8 | 0.606975 | 0.675833 | 00:00 |
| 9 | 0.602670 | 0.675682 | 00:00 |
This model is predicting movie ratings on a scale of 0.5 to 5.0 to within around 0.6 average error. Since we're predicting a continuous number, rather than a category, we have to tell fastai what range our target has, using the y_range parameter.
Although we're not actually using a pretrained model (for the same reason that we didn't for the tabular model), this example shows that fastai lets us use fine_tune anyway in this case (you'll learn how and why this works in <<chapter_pet_breeds>>). Sometimes it's best to experiment with fine_tune versus fit_one_cycle to see which works best for your dataset.
We can use the same show_results call we saw earlier to view a few examples of user and movie IDs, actual ratings, and predictions:
这个模型预测的是0.5到5.0级的电影评分,平均误差在0.6以内。由于我们预测的是一个连续的数字,而不是一个类别,我们必须使用y_range参数告诉fastai我们的目标范围。
尽管我们实际上没有使用预训练的模型(原因与我们没有使用表格模型的原因相同),这个例子表明fastai让我们在这种情况下使用fine_tune(你会在<>中了解它的作用和原因)。有时,最好用fine_tune和fit_one_cycle进行试验,看看哪种方法最适合你的数据集。
我们可以使用我们先前看到的同样的show_results调用来查看一些用户和电影ID、实际评分和预测的例子。
learn.show_results()Output
<IPython.core.display.HTML object>
| userId | movieId | rating | rating_pred | |
|---|---|---|---|---|
| 0 | 66.0 | 79.0 | 4.0 | 3.978900 |
| 1 | 97.0 | 15.0 | 4.0 | 3.851795 |
| 2 | 55.0 | 79.0 | 3.5 | 3.945623 |
| 3 | 98.0 | 91.0 | 4.0 | 4.458704 |
| 4 | 53.0 | 7.0 | 5.0 | 4.670005 |
| 5 | 26.0 | 69.0 | 5.0 | 4.319870 |
| 6 | 81.0 | 16.0 | 4.5 | 4.426761 |
| 7 | 80.0 | 7.0 | 4.0 | 4.046183 |
| 8 | 51.0 | 94.0 | 5.0 | 3.499996 |
<IPython.core.display.HTML object>
Sidebar: Datasets: Food for Models
题外话:数据集:模型的食物
You’ve already seen quite a few models in this section, each one trained using a different dataset to do a different task. In machine learning and deep learning, we can’t do anything without data. So, the people that create datasets for us to train our models on are the (often underappreciated) heroes. Some of the most useful and important datasets are those that become important academic baselines; that is, datasets that are widely studied by researchers and used to compare algorithmic changes. Some of these become household names (at least, among households that train models!), such as MNIST, CIFAR-10, and ImageNet.
The datasets used in this book have been selected because they provide great examples of the kinds of data that you are likely to encounter, and the academic literature has many examples of model results using these datasets to which you can compare your work.
Most datasets used in this book took the creators a lot of work to build. For instance, later in the book we’ll be showing you how to create a model that can translate between French and English. The key input to this is a French/English parallel text corpus prepared back in 2009 by Professor Chris Callison-Burch of the University of Pennsylvania. This dataset contains over 20 million sentence pairs in French and English. He built the dataset in a really clever way: by crawling millions of Canadian web pages (which are often multilingual) and then using a set of simple heuristics to transform URLs of French content onto URLs pointing to the same content in English.
As you look at datasets throughout this book, think about where they might have come from, and how they might have been curated. Then think about what kinds of interesting datasets you could create for your own projects. (We’ll even take you step by step through the process of creating your own image dataset soon.)
fast.ai has spent a lot of time creating cut-down versions of popular datasets that are specially designed to support rapid prototyping and experimentation, and to be easier to learn with. In this book we will often start by using one of the cut-down versions and later scale up to the full-size version (just as we're doing in this chapter!). In fact, this is how the world’s top practitioners do their modeling in practice; they do most of their experimentation and prototyping with subsets of their data, and only use the full dataset when they have a good understanding of what they have to do.
在这一节中,你已经看到了不少模型,每一个模型都是用不同的数据集来训练的,以完成不同的任务。在机器学习和深度学习中,没有数据我们什么都做不了。因此,那些为我们创造数据集来训练模型的人是(经常被低估的)英雄。一些最有用和最重要的数据集是那些成为重要的学术基线的数据集;也就是说,被研究人员广泛研究并用于比较算法变化的数据集。其中一些成为家喻户晓的名字(至少,在训练模型的家庭中!),如MNIST、CIFAR-10和ImageNet。
本书所使用的数据集之所以被选中,是因为它们为你可能遇到的各种数据提供了很好的例子,而且学术文献中有许多使用这些数据集的模型结果的例子,你可以将你的工作与之比较。
本书中使用的大多数数据集都是创作者花了很多功夫建立的。例如,在本书的后面,我们将向你展示如何创建一个可以在法语和英语之间进行翻译的模型。这方面的关键输入是宾夕法尼亚大学的克里斯-卡利森-伯奇教授早在2009年准备的法/英平行文本语料库。这个数据集包含超过2000万个法语和英语的句子对。他以一种非常聪明的方式建立了这个数据集:通过抓取数以百万计的加拿大网页(这些网页通常是多语种的),然后使用一套简单的启发式方法将法语内容的URL转换为指向英语相同内容的URL。
当你看本书中的数据集时,想想它们可能来自哪里,以及它们可能是如何被策划的。然后想想你可以为自己的项目创造什么样的有趣的数据集。(我们甚至会在不久之后带你一步步完成创建你自己的图像数据集的过程)。
fast.ai已经花了很多时间来创建流行数据集的缩减版本,这些数据集是专门为支持快速原型设计和实验而设计的,并且更容易学习使用。在本书中,我们通常会先使用其中一个缩小的版本,然后再扩展到全尺寸的版本(就像我们在本章中所做的那样!)。事实上,这就是世界上顶尖的从业者在实践中做建模的方式;他们用数据的子集做大部分的实验和原型设计,只有当他们对自己要做的事情有了很好的理解后,才会使用完整的数据集。
End sidebar
题外话结束
Each of the models we trained showed a training and validation loss. A good validation set is one of the most important pieces of the training process. Let's see why and learn how to create one.
我们训练的每个模型都显示了训练和验证的损失。一个好的验证集是训练过程中最重要的部分之一。让我们来看看为什么,并学习如何创建一个。
Validation Sets and Test Sets
验证集和测试集
As we've discussed, the goal of a model is to make predictions about data. But the model training process is fundamentally dumb. If we trained a model with all our data, and then evaluated the model using that same data, we would not be able to tell how well our model can perform on data it hasn’t seen. Without this very valuable piece of information to guide us in training our model, there is a very good chance it would become good at making predictions about that data but would perform poorly on new data.
To avoid this, our first step was to split our dataset into two sets: the training set (which our model sees in training) and the validation set, also known as the development set (which is used only for evaluation). This lets us test that the model learns lessons from the training data that generalize to new data, the validation data.
One way to understand this situation is that, in a sense, we don't want our model to get good results by "cheating." If it makes an accurate prediction for a data item, that should be because it has learned characteristics of that kind of item, and not because the model has been shaped by actually having seen that particular item.
Splitting off our validation data means our model never sees it in training and so is completely untainted by it, and is not cheating in any way. Right?
In fact, not necessarily. The situation is more subtle. This is because in realistic scenarios we rarely build a model just by training its weight parameters once. Instead, we are likely to explore many versions of a model through various modeling choices regarding network architecture, learning rates, data augmentation strategies, and other factors we will discuss in upcoming chapters. Many of these choices can be described as choices of hyperparameters. The word reflects that they are parameters about parameters, since they are the higher-level choices that govern the meaning of the weight parameters.
正如我们已经讨论过的,模型的目标是对数据进行预测。但模型的训练过程从根本上说是愚蠢的。如果我们用所有的数据来训练一个模型,然后用同样的数据来评估这个模型,我们就无法知道我们的模型在它没有见过的数据上能有多好的表现。如果没有这个非常有价值的信息来指导我们训练模型,那么它很有可能在对该数据进行预测时变得很好,但在新数据上的表现却很差。
为了避免这种情况,我们的第一步是将我们的数据集分成两组:训练集(我们的模型在训练中看到的)和验证集,也被称为开发集(只用于评估)。这让我们可以测试模型从训练数据中学习到的经验是否可以推广到新数据,即验证数据。
对这种情况的一种理解是,从某种意义上说,我们不希望我们的模型通过 "作弊 "获得好的结果。如果它对一个数据项目做出了准确的预测,那应该是因为它已经学会了这种项目的特征,而不是因为模型已经被实际看到的那个特定项目所塑造。
分离我们的验证数据意味着我们的模型在训练中从未见过它,所以完全没有受到它的影响,这不是任何形式的欺骗。对吗?
事实上,不一定。这种情况更加微妙。这是因为在现实场景中,我们很少只通过训练一次权重参数来建立一个模型。相反,我们可能会通过各种建模选择来探索一个模型的许多版本,这些选择涉及网络结构、学习率、数据增强策略以及我们将在接下来的章节中讨论的其他因素。这些选择中有许多可以被描述为超参数的选择。这个词反映了它们是关于参数的参数,因为它们是支配权重参数意义的更高层次的选择。
The problem is that even though the ordinary training process is only looking at predictions on the training data when it learns values for the weight parameters, the same is not true of us. We, as modelers, are evaluating the model by looking at predictions on the validation data when we decide to explore new hyperparameter values! So subsequent versions of the model are, indirectly, shaped by us having seen the validation data. Just as the automatic training process is in danger of overfitting the training data, we are in danger of overfitting the validation data through human trial and error and exploration.
The solution to this conundrum is to introduce another level of even more highly reserved data, the test set. Just as we hold back the validation data from the training process, we must hold back the test set data even from ourselves. It cannot be used to improve the model; it can only be used to evaluate the model at the very end of our efforts. In effect, we define a hierarchy of cuts of our data, based on how fully we want to hide it from training and modeling processes: training data is fully exposed, the validation data is less exposed, and test data is totally hidden. This hierarchy parallels the different kinds of modeling and evaluation processes themselves—the automatic training process with back propagation, the more manual process of trying different hyper-parameters between training sessions, and the assessment of our final result.
The test and validation sets should have enough data to ensure that you get a good estimate of your accuracy. If you're creating a cat detector, for instance, you generally want at least 30 cats in your validation set. That means that if you have a dataset with thousands of items, using the default 20% validation set size may be more than you need. On the other hand, if you have lots of data, using some of it for validation probably doesn't have any downsides.
Having two levels of "reserved data"—a validation set and a test set, with one level representing data that you are virtually hiding from yourself—may seem a bit extreme. But the reason it is often necessary is because models tend to gravitate toward the simplest way to do good predictions (memorization), and we as fallible humans tend to gravitate toward fooling ourselves about how well our models are performing. The discipline of the test set helps us keep ourselves intellectually honest. That doesn't mean we always need a separate test set—if you have very little data, you may need to just have a validation set—but generally it's best to use one if at all possible.
This same discipline can be critical if you intend to hire a third party to perform modeling work on your behalf. A third party might not understand your requirements accurately, or their incentives might even encourage them to misunderstand them. A good test set can greatly mitigate these risks and let you evaluate whether their work solves your actual problem.
To put it bluntly, if you're a senior decision maker in your organization (or you're advising senior decision makers), the most important takeaway is this: if you ensure that you really understand what test and validation sets are and why they're important, then you'll avoid the single biggest source of failures we've seen when organizations decide to use AI. For instance, if you're considering bringing in an external vendor or service, make sure that you hold out some test data that the vendor never gets to see. Then you check their model on your test data, using a metric that you choose based on what actually matters to you in practice, and you decide what level of performance is adequate. (It's also a good idea for you to try out some simple baseline yourself, so you know what a really simple model can achieve. Often it'll turn out that your simple model performs just as well as one produced by an external "expert"!)
问题是,尽管普通的训练过程在学习权重参数值时只看训练数据上的预测,但我们的情况却不是这样的。作为建模者,当我们决定探索新的超参数值时,我们是通过观察验证数据的预测来评估模型的。因此,模型的后续版本是由我们看过验证数据后间接形成的。就像自动训练过程有可能对训练数据进行过度拟合一样,我们也有可能通过人为的试错和探索对验证数据进行过度拟合。
解决这个难题的办法是引入另一层更高度保留的数据,即测试集。就像我们在训练过程中保留验证数据一样,我们甚至必须对自己保留测试集的数据。它不能被用来改进模型;它只能在我们努力的最后阶段用来评估模型。实际上,我们定义了一个切割数据的层次结构,基于我们希望从训练和建模过程中完全隐藏数据的程度:训练数据完全暴露,验证数据较少暴露,而测试数据则完全隐藏。这种层次结构与不同类型的建模和评估过程本身相类似--带有反向传播的自动训练过程,在训练过程中尝试不同的超参数的更手动的过程,以及对我们最终结果的评估。
测试集和验证集应该有足够的数据,以确保你对你的准确性有一个很好的估计。例如,如果你要创建一个猫咪检测器,你一般希望在验证集中至少有30只猫。这意味着,如果你的数据集有成千上万的项目,使用默认的20%的验证集大小可能超过你的需要。另一方面,如果你有大量的数据,使用其中的一部分进行验证可能没有任何坏处。
有两层 "保留数据"--验证集和测试集,其中一层代表你几乎对自己隐藏的数据--可能看起来有点极端。但是,这通常是必要的,因为模型往往倾向于用最简单的方法来进行良好的预测(记忆),而我们作为易变的人类往往倾向于欺骗自己,不知道我们的模型表现如何。测试集的纪律帮助我们在理智上保持诚实。这并不意味着我们总是需要一个单独的测试集--如果你的数据很少,你可能只需要一个验证集,但一般来说,如果可能的话,最好使用一个测试集。
如果你打算雇用第三方代表你进行建模工作,这种纪律也很关键。第三方可能不能准确地理解你的需求,或者他们的动机甚至可能鼓励他们误解你的需求。一个好的测试集可以大大降低这些风险,让你评估他们的工作是否解决了你的实际问题。
直截了当地说,如果你是你的组织中的高级决策者(或者你在为高级决策者提供建议),最重要的收获是:如果你确保你真正理解什么是测试和验证集,以及为什么它们很重要,那么你将避免我们看到的组织决定使用人工智能时的最大失败来源。例如,如果你正在考虑引入一个外部供应商或服务,确保你持有一些供应商从未看到的测试数据。然后你在你的测试数据上检查他们的模型,使用一个你根据实际情况选择的指标,并决定什么水平的性能是足够的。(对你来说,自己尝试一些简单的基线也是一个好主意,这样你就知道一个真正简单的模型可以实现什么。通常情况下,你的简单模型和外部 "专家 "制作的模型表现得一样好!)
Use Judgment in Defining Test Sets
在定义测试集时要有判断力
To do a good job of defining a validation set (and possibly a test set), you will sometimes want to do more than just randomly grab a fraction of your original dataset. Remember: a key property of the validation and test sets is that they must be representative of the new data you will see in the future. This may sound like an impossible order! By definition, you haven’t seen this data yet. But you usually still do know some things.
It's instructive to look at a few example cases. Many of these examples come from predictive modeling competitions on the Kaggle platform, which is a good representation of problems and methods you might see in practice.
One case might be if you are looking at time series data. For a time series, choosing a random subset of the data will be both too easy (you can look at the data both before and after the dates you are trying to predict) and not representative of most business use cases (where you are using historical data to build a model for use in the future). If your data includes the date and you are building a model to use in the future, you will want to choose a continuous section with the latest dates as your validation set (for instance, the last two weeks or last month of available data).
Suppose you want to split the time series data in <> into training and validation sets.
为了做好验证集(可能还有测试集)的定义工作,你有时要做的不仅仅是随机抓取原始数据集的一部分。记住:验证集和测试集的一个关键属性是,它们必须能够代表你将来要看到的新数据。这听起来像是一个不可能的命令! 根据定义,你还没有看到这些数据。但是你通常还是知道一些事情。
看一下几个例子的情况是很有启发的。其中许多例子来自于Kaggle平台上的预测性建模比赛,这很好地代表了你在实践中可能看到的问题和方法。
一个案例可能是如果你在看时间序列数据。对于一个时间序列来说,选择一个随机的数据子集既太容易了(你可以看一下你要预测的日期之前和之后的数据),又不能代表大多数商业用例(你要用历史数据建立一个模型供未来使用)。如果你的数据包括日期,并且你要建立一个模型在未来使用,你要选择一个连续的部分,以最新的日期作为你的验证集(例如,最近两周或最后一个月的可用数据)。
假设你想把<>中的时间序列数据分成训练集和验证集。

A random subset is a poor choice (too easy to fill in the gaps, and not indicative of what you'll need in production), as we can see in <>.
正如我们在<>中所看到的,随机的子集是一个糟糕的选择(太容易填补空白,而且不能说明你在生产中需要什么)。

Instead, use the earlier data as your training set (and the later data for the validation set), as shown in <>.
相反,使用早期的数据作为你的训练集(后期的数据为验证集),如<>所示。

For example, Kaggle had a competition to predict the sales in a chain of Ecuadorian grocery stores. Kaggle's training data ran from Jan 1 2013 to Aug 15 2017, and the test data spanned Aug 16 2017 to Aug 31 2017. That way, the competition organizer ensured that entrants were making predictions for a time period that was in the future, from the perspective of their model. This is similar to the way quant hedge fund traders do back-testing to check whether their models are predictive of future periods, based on past data.
例如,Kaggle有一个预测厄瓜多尔连锁杂货店销售额的比赛。Kaggle的训练数据从2013年1月1日到2017年8月15日,测试数据横跨2017年8月16日至2017年8月31日。这样,比赛组织者确保参赛者从其模型的角度对未来的某个时间段进行预测。这类似于量化对冲基金交易员做回溯测试的方式,根据过去的数据,检查他们的模型是否对未来时期有预测作用。
A second common case is when you can easily anticipate ways the data you will be making predictions for in production may be qualitatively different from the data you have to train your model with.
In the Kaggle distracted driver competition, the independent variables are pictures of drivers at the wheel of a car, and the dependent variables are categories such as texting, eating, or safely looking ahead. Lots of pictures are of the same drivers in different positions, as we can see in <<img_driver>>. If you were an insurance company building a model from this data, note that you would be most interested in how the model performs on drivers it hasn't seen before (since you would likely have training data only for a small group of people). In recognition of this, the test data for the competition consists of images of people that don't appear in the training set.
第二个常见的情况是,当你可以很容易地预测到你在生产中要进行预测的数据可能与你用来训练模型的数据有质的不同。
在Kaggle分心驾驶比赛中,自变量是司机在汽车方向盘上的照片,因变量是发短信、吃东西或安全地看前面等类别。很多图片都是同一司机在不同位置的照片,我们可以在<>中看到。如果你是一家保险公司,利用这些数据建立一个模型,请注意你最感兴趣的是模型在它以前没有见过的司机身上的表现(因为你可能只有一小部分人的训练数据)。认识到这一点,比赛的测试数据包括训练集中没有出现的人的图像。
If you put one of the images in <<img_driver>> in your training set and one in the validation set, your model will have an easy time making a prediction for the one in the validation set, so it will seem to be performing better than it would on new people. Another perspective is that if you used all the people in training your model, your model might be overfitting to particularities of those specific people, and not just learning the states (texting, eating, etc.).
A similar dynamic was at work in the Kaggle fisheries competition to identify the species of fish caught by fishing boats in order to reduce illegal fishing of endangered populations. The test set consisted of boats that didn't appear in the training data. This means that you'd want your validation set to include boats that are not in the training set.
Sometimes it may not be clear how your validation data will differ. For instance, for a problem using satellite imagery, you'd need to gather more information on whether the training set just contained certain geographic locations, or if it came from geographically scattered data.
如果你把<<img_driver>>中的一张图片放在你的训练集中,另一张放在验证集中,你的模型会很容易对验证集中的那张图片进行预测,所以它的表现似乎比对新的人更好。另一个角度是,如果你用所有的人训练你的模型,你的模型可能会对那些特定的人的特殊性进行过度拟合,而不仅仅是学习状态(发短信、吃饭等)。
在Kaggle渔业竞赛中也有类似的动态,即识别渔船捕获的鱼的种类,以减少对濒危种群的非法捕捞。测试集由训练数据中没有出现的船只组成。这意味着你希望你的验证集包括训练集中没有的船只。
有时可能不清楚你的验证数据会有什么不同。例如,对于一个使用卫星图像的问题,你需要收集更多的信息,了解训练集是否只包含某些地理位置,或者它是否来自地理上的分散数据。
Now that you have gotten a taste of how to build a model, you can decide what you want to dig into next.
现在你已经尝到了如何建立一个模型的甜头,你可以决定下一步要钻研什么。
A Choose Your Own Adventure moment
选择你自己的冒险时刻
If you would like to learn more about how to use deep learning models in practice, including how to identify and fix errors, create a real working web application, and avoid your model causing unexpected harm to your organization or society more generally, then keep reading the next two chapters. If you would like to start learning the foundations of how deep learning works under the hood, skip to <<chapter_mnist_basics>>. (Did you ever read Choose Your Own Adventure books as a kid? Well, this is kind of like that… except with more deep learning than that book series contained.)
You will need to read all these chapters to progress further in the book, but it is totally up to you which order you read them in. They don't depend on each other. If you skip ahead to <<chapter_mnist_basics>>, we will remind you at the end to come back and read the chapters you skipped over before you go any further.
如果你想了解更多关于如何在实践中使用深度学习模型,包括如何识别和修复错误,创建一个真正的工作网络应用,并避免你的模型对你的组织或社会造成意外的伤害,那么继续阅读接下来的两章。如果你想开始学习深度学习在引擎盖下如何工作的基础,请跳到<<chapter_mnist_basics>>。(你小时候读过《选择你自己的冒险》的书吗?嗯,这有点像......除了比那套书中包含的更多的深度学习)。
你需要阅读所有这些章节,以便在本书中取得进一步进展,但你完全可以决定阅读它们的顺序。它们之间并不相互依赖。如果你跳到<<chapter_mnist_basics>>,我们会在最后提醒你回来阅读你跳过的章节,然后再进一步。
Questionnaire
调查表
It can be hard to know in pages and pages of prose what the key things are that you really need to focus on and remember. So, we've prepared a list of questions and suggested steps to complete at the end of each chapter. All the answers are in the text of the chapter, so if you're not sure about anything here, reread that part of the text and make sure you understand it. Answers to all these questions are also available on the book's website. You can also visit the forums if you get stuck to get help from other folks studying this material.
For more questions, including detailed answers and links to the video timeline, have a look at Radek Osmulski's aiquizzes.
在一页又一页的文章中,你可能很难知道哪些是你真正需要关注和记忆的关键内容。因此,我们在每一章的末尾都准备了一个问题清单和建议完成的步骤。所有的答案都在这一章的正文中,所以如果你对这里的任何内容不确定,请重读这部分内容,确保你理解它。所有这些问题的答案也都可以在本书的网站上找到。如果你被卡住了,你也可以访问论坛,从其他学习这种材料的人那里获得帮助。
对于更多的问题,包括详细的答案和视频时间线的链接,可以看看Radek Osmulski的aiquizzes。
-
Do you need these for deep learning?
- Lots of math T / F
- Lots of data T / F
- Lots of expensive computers T / F
- A PhD T / F
-
Name five areas where deep learning is now the best in the world.
-
What was the name of the first device that was based on the principle of the artificial neuron?
-
Based on the book of the same name, what are the requirements for parallel distributed processing (PDP)?
-
What were the two theoretical misunderstandings that held back the field of neural networks?
-
What is a GPU?
-
Open a notebook and execute a cell containing:
1+1. What happens? -
Follow through each cell of the stripped version of the notebook for this chapter. Before executing each cell, guess what will happen.
-
Complete the Jupyter Notebook online appendix.
-
Why is it hard to use a traditional computer program to recognize images in a photo?
-
What did Samuel mean by "weight assignment"?
-
What term do we normally use in deep learning for what Samuel called "weights"?
-
Draw a picture that summarizes Samuel's view of a machine learning model.
-
Why is it hard to understand why a deep learning model makes a particular prediction?
-
What is the name of the theorem that shows that a neural network can solve any mathematical problem to any level of accuracy?
-
What do you need in order to train a model?
-
How could a feedback loop impact the rollout of a predictive policing model?
-
Do we always have to use 224×224-pixel images with the cat recognition model?
-
What is the difference between classification and regression?
-
What is a validation set? What is a test set? Why do we need them?
-
What will fastai do if you don't provide a validation set?
-
Can we always use a random sample for a validation set? Why or why not?
-
What is overfitting? Provide an example.
-
What is a metric? How does it differ from "loss"?
-
How can pretrained models help?
-
What is the "head" of a model?
-
What kinds of features do the early layers of a CNN find? How about the later layers?
-
Are image models only useful for photos?
-
What is an "architecture"?
-
What is segmentation?
-
What is
y_rangeused for? When do we need it? -
What are "hyperparameters"?
-
What's the best way to avoid failures when using AI in an organization?
- 深度学习需要这些吗?
- 大量的数学运算 T / F
- 大量的数据 T / F
- 大量昂贵的计算机 T / F
- 一个博士生 T / F
-
说出五个深度学习现在是世界上最好的领域。
-
第一个基于人工神经元原理的设备的名称是什么?
-
根据同名书籍,并行分布式处理(PDP)的要求是什么?
-
阻碍神经网络领域发展的两个理论上的误区是什么?
-
什么是GPU?
-
打开一个笔记本,执行一个包含:1+1的单元。会发生什么?
-
按照本章的剥离版笔记本的每个单元进行操作。在执行每个单元之前,猜测会发生什么。
-
完成Jupyter笔记本的在线附录。
-
为什么用传统的计算机程序来识别照片中的图像是困难的?
-
塞缪尔说的 "权重分配 "是什么意思?
-
在深度学习中,我们通常用什么词来表示塞缪尔所说的 "权重"?
-
画一张图,总结一下塞缪尔对机器学习模型的看法。
-
为什么很难理解为什么一个深度学习模型会做出一个特定的预测?
-
这个定理的名称是什么,它表明神经网络可以解决任何数学问题,达到任何程度的准确性?
-
为了训练一个模型,你需要什么?
-
反馈回路会如何影响预测性警务模型的推广?
-
我们是否总是要使用224×224像素的图像来识别猫?
-
分类和回归之间的区别是什么?
-
什么是验证集?什么是测试集?我们为什么需要它们?
-
如果你不提供验证集,fastai会怎么做?
-
我们可以一直使用随机样本作为验证集吗?为什么或为什么不?
-
什么是过度拟合?提供一个例子。
-
什么是度量?它与 "损失 "有什么不同?
-
预训练的模型有什么帮助?
-
什么是模型的 "头部"?
-
CNN的早期层能找到什么样的特征?后面的层又是怎样的?
-
图像模型只对照片有用吗?
-
什么是 "架构"?
-
什么是分割?
-
y_range是用来做什么的?我们什么时候需要它?
-
什么是 "超参数"?
-
在组织中使用人工智能时,避免失败的最好方法是什么?
Further Research
进一步的研究
Each chapter also has a "Further Research" section that poses questions that aren't fully answered in the text, or gives more advanced assignments. Answers to these questions aren't on the book's website; you'll need to do your own research!
每一章还有一个 "进一步研究 "部分,提出了文本中没有完全回答的问题,或者给出了更多的高级作业。这些问题的答案并不在书中的网站上,你需要自己做研究
- Why is a GPU useful for deep learning? How is a CPU different, and why is it less effective for deep learning?
- Try to think of three areas where feedback loops might impact the use of machine learning. See if you can find documented examples of that happening in practice.
- 为什么GPU对深度学习有用?CPU有什么不同,为什么它对深度学习不太有效?
- 试着想一想,反馈回路可能影响机器学习的使用的三个领域。看看你是否能找到在实践中发生的有记录的例子。
