利用Miniflow创建一个深度神经网络

利用Miniflow创建一个深度神经网络

利用Miniflow可以更好的去理解Tensorflow的工作原理。

miniflow.py

import numpy as np
class Node(object):
    def __init__(self,inbound_nodes=[]):    #节点中包含输入节点、输出节点、值、梯度值以及前向传播和背向传播的方法
        self.inbound_nodes=inbound_nodes    #输入节点被存储为节点的列表
        self.value=None                     #节点取值为任意数据类型
        self.gradients={}                   #节点的梯度值为字典类型,键为每个输出节点,值为相应的输出节点关于该节点的偏微分乘以该节点相对于
                                            #输入节点的偏微分
        for n in self.inbound_nodes:        #输出节点被存储为节点的列表,由其传出节点进行赋值
            n.outbound_nodes.append(self)   

    def forward(self):
        raise NotImplementedError

    def backward(self):
        raise NotImplementedError    

Node类是一个母类,其子类有input,linear,sigmoid和MSE四个。Node类中包含两个列表,分别用来存储输入节点和输出节点,包含一个字典,用来存储该节点的梯度值,包含一个取值,以及两个方法,用来进行前向传播和背向传播。

class input(Node):                      #输入节点类型
    def __init__(self):
        Node.__init__(self)

    def forward(self):
        pass

    def backward(self):
        self.gradients={self:0}
        for n in self.outbound_nodes:
            grad_cost=n.gradients[self]
            self.gradients[self]+=grad_cost*1

input类是Node类的一个子类,可以用来概括深度神经网络中的所有输入值类型:X,W,b,y。

class Linear(Node):
    """
    Represents a node that performs a linear transform.
    """
    def __init__(self, X, W, b):
        # The base class (Node) constructor. Weights and bias
        # are treated like inbound nodes.
        Node.__init__(self, [X, W, b])

    def forward(self):
        """
        Performs the math behind a linear transform.
        """
        X = self.inbound_nodes[0].value
        W = self.inbound_nodes[1].value
        b = self.inbound_nodes[2].value
        self.value = np.dot(X, W) + b

    def backward(self):
        """
        Calculates the gradient based on the output values.
        """
        # Initialize a partial for each of the inbound_nodes.
        self.gradients = {n: np.zeros_like(n.value) for n in self.inbound_nodes}
        # Cycle through the outputs. The gradient will change depending
        # on each output, so the gradients are summed over all outputs.
        for n in self.outbound_nodes:
            # Get the partial of the cost with respect to this node.
            grad_cost = n.gradients[self]
            # Set the partial of the loss with resp
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值