ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

5分钟搞定TensoFlow – 译学馆
未登录,请登录后再发表信息
最新评论 (0)
播放视频

5分钟搞定TensoFlow

TensorFlow in 5 Minutes (tutorial)

大家好 我是西拉杰
Hello, world! It’s Siraj!
现在全球最热门的机器学习库
The most popular machine learning library in the world
非谷歌的TensorFlow莫属
right now is Google’s TensorFlow.
我们将建一个分类器
We’re going to use it to build a classifier
它可以分析手写数字的图片
that can look at an image of a handwritten digit
识别出它是什么数字
and classify what digit it is
只需要40行简单的代码
in under 40 lines of code basic is she.
几乎每个谷歌产品都一定程度上应用机器学习
Pretty much every single Google product uses machine learning in some way.
无论它是图片搜索
Whether it’s image search,
图片说明 翻译 推荐
image captioning, translation, recommendations,
谷歌需要机器学习来发挥其拥有的无限数据的优势
Google needs machine learning to take advantage of their godlike datasets
去为用户提供最优的体验
to give users the dopest experience.
有三类人使用机器学习
There are three different crowds that use machine learning
研究学者 数据科学家和巫师 我的意思是开发者
researchers, data scientists and wizards, I mean developers.
理想情况下他们可以用相同的工具集彼此进行协作
Ideally they can all use the same tool setto collaborate with each other
并提高效率
and improve their efficiency.
TensorFlow就是他们创造出来解决问题的办法
TensorFlow was a solution they created to help solve this problem.
谷歌不只有庞大的数据
Google doesn’t just have a lot of data,
他们还有世界上最强大的电脑
they have the world’s largest computer.
因此这个库生来就是可扩展的
So the library was built to scale.
它可以运行在多个中央处理器和图形处理器上
It was made to run on multiple CPUs or GPUs
甚至是手机操作系统
and even mobile operating systems.
这个库有许多不同语言的封装
And it has several wrappers in several languages.
我最喜欢的是Python
My favorite one is Python.
Objective-C你伤了我的心
Objective-C you broke my heart.
我们需要先下载TensorFlow
We have to install TensorFlow first.
我们要用到Python包管理器的pip去下载它
We’re going to use pip,the Python package manager to install it.
当我们有了pip
Once we have pip,
我们可以创建一个环境变量
we can create an environment variable
指向TensorFlow的下载地址
and point this download URL for TensorFlow.
当我们配置好环境变量
Once we set the environment variable,
我们可以通过pip命令下载TensorFlow
we can download TensorFlow via pip install
并且在环境变量前加上upgrade参数
with the upgrade flag in the name of our environment variable.
棒极了 现在我们安装好了相关库
Dope! Now that we have our dependencies installed.
让我们开始敲代码
Let’s get to the code.
首先我们从导入手写数据集开始
We’ll start off by importing our handwritten digit data set,
这个input_data是个标准的Python类
the input data class is a standard Python class.
它会下载数据 将其分成训练和测试数据集
It downloads the dataset, splits it into training and testing data
并将数据集统一格式以便后续使用
and formats it for our use later on.
当然我们也要导入TensorFlow
And of course we’ll import TensorFlow.
现在我们可以设置超参数
Now we can set our hyper parameters
也就是我们的模型调节参数
or tuning knobs for our model.
首先要调节的参数是学习速率
The first one is the learning rate
学习速率代表了我们想要更新权重的速度
which defines how fast we want to update our weights.
如果学习速率太大 模型就可能跳过最优解
If the learning rate is too big our model might skip the optimal solution.
如果学习速率太小就可能需要多次迭代收敛到最优结果
If it’s too small we might need too many iterations to converge on the best results.
我们将学习速率设置为0.01
So we’ll set it to 0.01,
对于这个问题来说 这是一个合适的学习速率
Because it’s a known decent learning rate for this problem.
绝对比李尔·韦恩要快
Definitely faster than Lil Wayne’s.
现在我们开始建模型
Now we want to create our model.
在TensorFlow里 模型用一个数据流图表示
In TensorFlow, a model is represented as a data flow graph.
数据流图包含一系列叫做操作的节点
The graph contains a set of nodes called operations.
这些节点是计算单位
These are units of computation.
简单的有加法和乘法
They can be as simple as addition or multiplication.
复杂起来可以是一些多元方程
And can becomplicated at some multivariate equation.
每一次运算输入一个张量(tensor)输出一个张量
Each operation takes in as input a tensorand outputs a tensor as well.
数据在TensorFlow中用张量(tensor)表示
A tensor is how data is represented in TensorFlow.
其实它们就是数字的多维数组
They’re multi-dimensional arrays of numbers
在运算之间流动(flow)
and they flow between operations.
这就是名字的来源 TensorFlow
Hence the name, TensorFlow.
一切都合理了吧!
It all makes sense.
我们首先从创建两个运算开始建立我们的模型
We’ll start by building our model by creating two operations.
两个都是占位符运算
Both are placeholder operations.
占位符是一种之后才会给赋予数据的变量
A placeholder is just a variable that we will assign data to at a later date.
它不需要初始化且不包含数据
It’s never initialized and contains no data.
我们将定义数据的类型和形状作为参数
We’ll define the type and shape of our data as the parameters.
输入的图像X将由数字的二维张量表示
The input images X will be represented by a 2D tensor of numbers.
784是单个扁平MNIST图像的维度
784 is a dimensionality of a single flattened MNIST image.
查找图像以为着将二维数组转化为一位数组
Finding an image means converting a 2D array to a 1D array
将行旋转为列进行重新排列
by unstacking the rows in lining them up.
这是更有效的格式设定
This is more efficient formatting.
输出类Y也由二维张量组成
The output classes Y will consist of a 2D tensor as well
每行都是一个10维的one-hot向量
where each row is a one-hot 10 dimensional vector
表示MNIST图像所属的相对应数字类
showing which digit class the corresponding MNIST image belongs to.
然后我们将定义模型的权重W和偏置b
Then we’ll define our weights W and biases b for our model.
权重是影响数据在图表中流动方式的概率
The weights are the probabilities that affect how data flows in the graph.
它会在训练过程中不断更新
And they will be updated continuously during training
所以我们的结果才能越来越接近正确的解
so that our results get closer and closer to the right solution.
偏置让我们的回归线更贴合数据
The bias lets a shift our regression line to better fit the data.
接下来我们需要建一个作用域
We’ll then create a named scope.
作用域能帮我们在图形化的Tensor Board上整理节点
Scopes help us organize nodes in the graph visualizer called tensor board,
在最后我们会看到它
which we’ll view at the end.
我们将创建三个作用域
We’ll create three scopes.
在第一作用域内 我们将实现模型的逻辑回归
In the first scope we’ll implement our model, logistic regression
通过将输入图像x的矩阵乘以权重W的矩阵 并加上偏置 b
by matrix multiplying the input images x by the weight matrix W and adding the bias b
接下来我们创建总结运算
We’ll then create summary operations
帮助我们接下来更直观地形象化权重和偏置的分布
to help us later visualize the distribution of our weights and biases.
在第二个作用域里 我们创建了成本函数
In the second scope we’ll create our cost function
成本函数帮助我们在训练中减小误差
The cost function helps us minimize our error during training.
我们用的是流行的交叉熵函数
And we’ll use the popular cross-entropy function as it.
训练中我们将创建tensor_summary来进行监测
Then we’ll create a scalar summary to monitor it during training
便于之后数据的可视化
so we can visualize it later.
我们最后一个作用域叫做训练
Our last scope is called Train.
它将建一个优化方法
And it will create our optimization function
让我们的模型在训练中优化
that makes our model improve during training.
我们将使用流行的梯度下降算法
We’ll use the popular gradient descent algorithm
学习速率作为参数来控制步速
which takes our learning rate as a parameter for pacing
加上成本函数作为参数来减小误差
and our cost function as a parameter to help minimize the error.
现在图表创建好了 变量也初始化了
Now that we have our graph built, we’ll initialize all of our variables.
然后将所有的摘要合并到一个运算符
Then we’ll merge all of our summaries into a single operator
因为我们超懒
because we are extremely lazy.
现在图表已经准备好了
Now we’re ready to launch our graph
通过初始化会话
by initializing a session
数据流图开始执行
which lets us execute our data flow graph.
然后设置好SummaryWritter的路径
We’ll then set our SummaryWritter folder location
用来稍后让Tensor Board加载数据可视化显示
which will later load datato visualize in Tensor Board.
训练开始!我们来设置for循环的迭代次数
Training time! Let’s set our for loop for our specified number of iterations
以及初始化平均成本
and initialize our average cost
这样可以频繁打印结果
which will print out every so often
以确保我们的模型在训练中改进了
to make sure our model is improving during training.
我们将计算batch_size
We’ll compute our batch size
并用每条训练数据来训练
and start training over each example in our training data
接下来 为了训练模型
Next we’ll fit our model
我们在梯度下降算法中使用这批数据来做后向传播
using the batch data in the gradient descent algorithm for back propagation
还要计算平均损失
We’ll compute the average loss
并将每次迭代的结果写日志存到SummaryWritter
and write logs for each iteration via the SummaryWritter.
任何一步有错 错误日志都会被打印到屏幕上
For each display step we’ll display error logs to terminal.
这就是训练 接下来我们通过将模型值
That’s it for training we can then test the model
与输出值进行比较 来测试模型
by comparing our model values to our output values.
将测试数据的准确率打印出来
We’ll calculate the accuracy and print it out for test data.
准确率在训练中不断提高
The accuracy gets better with training
一旦我们对模型训练并测试完成
and once we’ve trained and tested our model,
它就能很好地对新的MNIST数字进行分类
it’ll be able to classify novel MNIST digits pretty well.
然后我们可以在Tensor Board里看到图像
We can then visualize our graph in TensorBoard.
唷 漂亮的颜色哦
Yo! Pretty colors and stuff.
浏览器里我们可以看到
In our browser we’ll be able to view
在事件标签下 成本函数输出在不断更新
the output of our cost function over time under the events tab.
在直方图标签下我们可以看到
Under histograms we’ll be able to see
不断变化的偏置和权重
the variance in our biases and weights over time.
在图表标签下我们可以看到创建的图表
Under graphs we can view the actual graph we created
以及权重和偏置变量
as well as the variables for weights and bias.
我们可以看到张量在
We can see the flow of tensors in the form of edges
节点(运算)之间的连线上流动
connecting our nodes or operations.
我们可以分别看到在之前代码中命名的三个作用域
We can see each of the three scopes we named in our code earlier.
双击可以看到更多细节
And by double clicking on each we can see a more detailed view of
比如张量如何相互流动
how tensors are flowing through each.
本视频的描述里有很多有趣的链接
Lots of cool links in the description
如果你想看到更多机器学习的视频 请点击订阅按钮
and please hit that subscribe button if you want to see more ML videos.
现在我得去Docker化我的开发环境了
For now I’ve got to go dockerize my environment.
谢谢观看
So, thanks for watching!

发表评论

译制信息
视频概述

该视频是关于如何使用40行(不包括空格和注释)Python代码构建一个手写数字图像分类器。我们将使用流行的TensorFlow做到这一点。

听录译者

收集自网络

翻译译者

。蓝

审核员

审核员1024

视频来源

https://www.youtube.com/watch?v=2FmcHiLCwTU

相关推荐