MiniFlow -- 1.什么是神经网络

在这里插入图片描述

1. 什么是神经网络

如上图,这是一个简单的神经网络,神经网络就是一个包含数学运算的流程图(Graph),这些数学运算包括矩阵运算,还有激活函数。这个图表包括节点/神经元(neurons/nodes),链接/边界线(links/edges)。
每一层的节点的就是使用上一层的的输出作为本层的输入进行数学运算,当然不包括输入层次(input layer),因为输入层不进行运算
比如,每一个节点进行f(x,y)的运算,x和y都是将来自上一层的输出作为本层的输入。因此,每一层都会生成一个输出值,输出层除外(output layer)
在输入层和输出层之间的所有层都叫做隐藏层(Hidden layer)

2. 前向传播

从输入层开始,将一些数值经过每一层的数学运算,这个网络最后输出一个值,这个过程就叫做前向传播(Forward pass),如下这个简单的流程,最后输出30
在这里插入图片描述

3. Graphs

神经元(neurons/nodes)和链接(edges/links)构成了一个图结构(graph structure)。
定义一个Graphs一般需要两个步骤:

  1. Define the graphs of nodes and deges
  2. Propagate values through the graph

4. Neurons and MiniFlow

我们使用python 的class 作为MiniFlow的节点

class Neuron:
    def __init__(self):
        # Properties will go here!

我们知道,每一个节点都将上一层的输出作为输入,当然,每一个节点也都生成一个单独的输出,通过这个输入和输出来链接所有节点,我们需要添加两个lists,一个存储输入站点(inbound nodes)的引用关系,一个存储输出站点(outbound nodes)的引用

class Neuron:
    def __init__(self, inbound_neurons=[]):
        # Neuron from which this Neuron receives values
        self.inbound_neurons = inbound_neurons
        # Neuron to which this Neuron passes values
        self.outbound_neurons = []
        # Add this node as an outbound node on its inputs.
        for n in self.inbound_neurons:
            n.outbound_neurons.append(self)

每一个节点都要将输入的值根据本节点的数学关系,计算出一个值作为他的输出。现在我们初始化 value为None

class Neuron:
    def __init__(self, inbound_neurons=[]):
        # Neuron from which this Neuron receives values
        self.inbound_neurons = inbound_neurons
        # Neuron to which this Neuron passes values
        self.outbound_neurons = []
        # A calculated value
        self.value = None

每一个节点都需要完成前向运算和``反向传播(backpropagation),那么,我们需要添加两个方法

class Neuron:
    def __init__(self, inbound_neurons=[]):
        # Neuron from which this Neuron receives values
        self.inbound_neurons = inbound_neurons
        # Neuron to which this Neuron passes values
        self.outbound_neurons = []
        # A calculated value
        self.value = None
        # Add this node as an outbound node on its inputs.
        for n in self.inbound_neurons:
            n.outbound_neurons.append(self)

    def forward(self):
        """
        Forward propagation.

        Compute the output value based on `inbound_neurons` and
        store the result in self.value.
        """
        raise NotImplemented

    def backward(self):
        """
        Backward propagation.

        You'll compute this later.
        """
        raise NotImplemented

因为不同的节点执行不同的前向运算和反向传播,上面只是定义了一个父类,对于不同的节点我们还需要定义不同的子类来细化特定的运算,比如这里我们定义输入层

class Input(Neuron):
    def __init__(self):
        # An Input neuron has no inbound neurons,
        # so no need to pass anything to the Neuron instantiator.
        Neuron.__init__(self)

    # NOTE: Input neuron is the only node where the value
    # may be passed as an argument to forward().
    #
    # All other neuron implementations should get the value
    # of the previous neuron from self.inbound_neurons
    #
    # Example:
    # val0 = self.inbound_neurons[0].value
    def forward(self, value=None):
        # Overwrite the value if one is passed in.
        if value:
            self.value = value

Add 子类

class Add(Neuron):
    def __init__(self, x, y):
        Neuron.__init__(self, [x, y])

    def forward(self):
        """
        You'll be writing math here in the first quiz!
        """

4. 网络节点之间的拓扑关系

节点和节点之间通过拓扑逻辑关系链接
在这里插入图片描述
中间节点既要保存输入节点列表,也要保存输入关系列表,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值