未登录,请登录后再发表信息
最新评论 (0)
播放视频

4分钟内创建一个神经网络

Build a Neural Net in 4 Minutes

大家好 欢迎来到Sirajology
Oh, hello world. Welcome to Sirajology.
今天我们来学习如何在四分钟内构建一个神经网络
Today we’re going to be building a neural net in four minutes.
现在开始
Let’s get started.
机器学习模型现在数不胜数
There are like a million and one machine learning models out there,
但神经网络目前尤为火热 是出于两个原因
but neural nets in particular have gotten really popular recently because of two things,
更快的计算机以及更多的数据
faster computers and more data.
神经网络帮助各项领域产生惊人突破 从图像识别到生成说唱歌曲
They’ve helped produce some amazing breakthroughs in everything from image recognition to generating rap songs.
但事实上它参与到机器学习的只有三步
There are really just three steps involved in machine learning:
建立模型 训练 和测试
build it, train it, and test it.
模型一旦建立 我们就可以用输入和输出数据来训练它
Once we build our model we can train it against our input and output data
使其在模式识别中越加完善
to make it better and better at pattern recognition.
所以 我们来用Python搭建一个三层神经网络模型
So, let’s build our model, a three layer neural network, in Python.
以导入Numpy开始
We’ll want to start off by importing NumPy
也是我在Python内进行科学计算会用到的常用库
which is my goto library for scientific computing in Python.
接下来 我们创建一个函数 这函数将把任意值映射为0与1之间的一个值
Then, we’ll want to create a function that will map any value to a value between zero and one.
这个函数叫做Sigmoid函数
This is called a Sigmoid.
当有数据输入时 我们网络中的每一个神经元都会调用这个函数
This function will be run in every neuron of our network when data hits it.
它可以利用这些数来生成概率
It’s useful for creating probabilities out of numbers.
函数创建好后 我们把输入的数据集初始化为一个矩阵
Once we’ve created that, let’s initialize our input data set as a matrix.
每一行是一个不同的训练样本
Each row is a different training example.
每一列代表一个不同的神经元
Each column represents a different neuron.
所以我们共有4个训练样本 每个训练样本有3个输入神经元
So we have four training examples with three input neurons each.
接着我们将创建输出数据集
Then we’ll create our output data set.
比如 一个神经元就有一个输出
For examples, one output neuron each.
由于我们马上要生成随机数
Since we’ll be generating random numbers in a second,
我们通过确定seed来使得他们变成一个确定的函数
let’s seed them to make them deterministic.
这是说随机数是由相同的起始点或者说seed的值生成的
This just means give random numbers that are generated the same starting point, or seed,
这样我们每次运行程序时 生成的数列都是相同的
so that we’ll get the same sequence of generated numbers every time we run our program.
这有助于进行调试
This is useful for debugging.
接下来我们将创建突触矩阵
Next we’ll create our synapse matrices.
突触是从上一层中的每个神经元与下一层每个神经元之间的连接
Synapses are the connections between each neuron in one layer to every neuron in the next layer.
因为我们的网络有三层
Since we’ll have three layers in our network,
我们就需要两个突触矩阵
we need two synapse matrices.
每一矩阵分配一个随机权重
Each synapse has a random weight assigned to it.
这步之后 将开始写训练代码
After that, we’ll begin the training code.
我们将创建一个在训练代码上进行迭代的循环语句
We’ll create a for-loop that iterates over the training code
利用给定的数据集来优化网络
to optimize the network for the given data set.
我们将从创建第一层开始就是我们的输入数据
We’ll start off by creating our first layer.It’s just our input data.
现在进行到预测步骤
Now comes the prediction step.
我们将在每层与其突触间执行矩阵相乘
We’ll perform matrix multiplication between each layer and it’s synapse.
接着对矩阵内的所有值运行Sigmoid函数 产生下一层
Then we’ll run our Sigmoid function on all the values in the matrix to create the next layer.
下一层包含输出数据的预测
The next layer contains a prediction of the output data.
这层我们执行相同步骤 得到下一层
Then we do the same thing on that layer to get our next layer
就是一个更加精确的预测
which is a more refined prediction.
所以现在我们有一个第二层输出数据的预测
So now that we have a prediction of the output value in layer two,
我们将它与期望的输出数据做差来进而得到误差率
let’s compare it to the expected output data using subtraction to get the error rate.
我们也在设定好的时间段打印输出平均误差率
We’ll also want to print out the average error rate at a set interval
确保它每次有所下降
to make sure it goes down every time.
接下来 我们将误差率乘上Sigmoid函数的结果
Next, we’ll multiply our error rate by the result of our Sigmoid function.
这个函数用来得出第二层的输出预测的导数
The function is used to get the derivative of our output prediction from layer two.
这会给我们一个delta 用来在每一次迭代中更新突触时减少预测的误差率
This will give us a delta which we’ll use to reduce the error rate of our predictions when we update our synapses every iteration.
然后我们能看到第一层对第二层的误差有多少贡献
Then we’ll want to see how much layer one contributed to the error in layer two.
这个叫做反向传播
This is called back propagation.
通过将第二层的delta与突触一的转置相乘 我们会得到这个误差
We’ll get this error by multiplying layer two’s delta by synapse one’s transpose.
然后将这个误差乘上Sigmoid函数的结果 得到第一层的delta
Then we’ll get layer one’s delta by multiplying its error by the result of our Sigmoid function.
这个函数用来得出第一层的导数
The function is used to get the derivative of layer one.
这样我们就得到了每层都有delta
Now that we have deltas for each of our layers,
我们能用他们来更新突触权重 这样在每次迭代中越来越多地减少误差率
we can use them to update our synapse weights to reduce the error rate more and more every iteration.
实现它是靠一个叫做梯度下降的算法
This is an algorithm called Gradient Descent.
要做这个 我们只需要将每层与delta相乘
To do this, we’ll just multiply each layer by a delta.
最后 我们打印出预测输出
Finally, let’s print the predicted output.
就这样 我们在终端运行一下 看结果会是什么
And there you have it.Let’s run this in terminal and see what we get.
太棒了 我们看到误差率在每次迭代都有所下降
Awesome, we can see that our error rate decreases every iteration
而且预测输出与实际输出十分接近
and the predicted output is very very close to the actual output.
对于改进神经网络 我们能做的有很多
There is so much we can do to improve our neural network.
如想获取更多信息 可查看下面描述中的链接
For more information check out the links in the description below,
并请订阅更多科技视频
and please subscribe for more technology videos.
感谢观看
Thanks for watching.

发表评论

译制信息
视频概述

震惊!4分钟内就可创建一个神经网络

听录译者

十八轮

翻译译者

老王家隔壁

审核员

审核员CX

视频来源

https://www.youtube.com/watch?v=h3l4qz76JhQ&t=2s

相关推荐