利用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