神经网络原来这么简单,机器学习入门贴送给你 | 干货
副标题[/!--empirenews.page--]
本文经AI新媒体量子位(公众号ID:QbitAI)授权转载,转载请联系出处。 你想学机器学习吗?这里有一个入门贴适合你。 什么神经网络、随机森林、计算机视觉通通一网打尽。 这个Facebook软件工程师做了一个入门贴。 专为基础为零的初学者打造。 有基础的同学,也可以来看看加深一下理解。
我们就以神经网络为例先来一睹为快吧! 神经网络概论 作者说,神经网络并不复杂! “神经网络”一词很流行,人们通常认为它很难,但其实要简单得多。 是不是这样呢?先看再说。 神经网络的理解主要分为三个部分,神经元、神经网络的构建、训练神经网络。 神经元——神经网络的基本单元 这是2-input神经元的样子。 首先神经元接受输入x1、x2,进行一些数学运算以后,然后产生一个输出y。 在神经元里,通常会发生三件事: 1、每个输入乘以相应的权重; 2、将所有加权输入加在一起,在加上一个偏差b; 3、导入一个激活函数,得到输出y。 通常来说,激活函数使用Sigmoid函数,也就是常说的S型函数,输入任意值(-∞,+∞),最后输出都能停留在0-1之间。 对此,他还举了一个简单的例子。 以激活函数是S型函数、2输入神经元为例,设置参数 w=[0,1] (w1=0,w2=1),b=4。 input:x=[2,3] output:y=0.999 这也就是最为朴素的神经网络——前馈神经网络。 对此,作者还用Python实现了整个过程。 import numpy as np def sigmoid(x): # Our activation function: f(x) = 1 / (1 + e^(-x)) return 1 / (1 + np.exp(-x)) class Neuron: def __init__(self, weights, bias): self.weights = weights self.bias = bias def feedforward(self, inputs): # Weight inputs, add bias, then use the activation function total = np.dot(self.weights, inputs) + self.bias return sigmoid(total) weights = np.array([0, 1]) # w1 = 0, w2 = 1 bias = 4 # b = 4 n = Neuron(weights, bias) x = np.array([2, 3]) # x1 = 2, x2 = 3 print(n.feedforward(x)) # 0.9990889488055994 构建神经网络 神经元连接在一起就是神经网络。 两个输入,一个含有两个神经元的隐藏层,一个含有1个神经元的输出层就构建了一个神经网络。 需要注意的是,可以用多层隐藏层。就比如,像这样: 我们仍以上个示例的条件为例。 一个神经网络可以包含任意数量的层和任意数量的神经元。 以Python代码示例如下: import numpy as np # ... code from previous section here class OurNeuralNetwork: ''' A neural network with: - 2 inputs - a hidden layer with 2 neurons (h1, h2) - an output layer with 1 neuron (o1) Each neuron has the same weights and bias: - w = [0, 1] - b = 0 ''' def __init__(self): weights = np.array([0, 1]) bias = 0 # The Neuron class here is from the previous section self.h1 = Neuron(weights, bias) self.h2 = Neuron(weights, bias) self.o1 = Neuron(weights, bias) def feedforward(self, x): out_h1 = self.h1.feedforward(x) out_h2 = self.h2.feedforward(x) # The inputs for o1 are the outputs from h1 and h2 out_o1 = self.o1.feedforward(np.array([out_h1, out_h2])) return out_o1 network = OurNeuralNetwork() x = np.array([2, 3]) print(network.feedforward(x)) # 0.7216325609518421 训练神经网路——计算损失函数 假设,我们正在处理以下这个项目。通过人员的体重和身高来判断性别。 以weight、height作为输入,以gender作为输出。 将Male设置为0,Female设置为1,还对其余数据进行了简化。 在训练神经网络之前,首先需要一个方法来量化它做得有多“好”,是否能够做得“更好”,那就是损失函数(loss)。 这里,我们将使用损失函数的一种——均方误差来计算。 预测结果越好,说明损失也就会越低。而训练神经网络的目的,就在于尽可能的减少损失。 如果我们确信所有的人都是Male,也就是说预测值为0,会出现什么样的结果? Python示例: (编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |