Network类 —— __init__ 、sigmoid

41 阅读1分钟

1、__init__

代码:

    def __init_(self, sizes):
        self.num_layers = len(sizes)
        self.sizes = sizes
        self.biases = [ np.random.randn(y, 1) for y in sizes[1:] ]
        self.weights = [ np.random.randn(y, x) for x, y in zip(sizes[:-1], sizes[1:]) ] 

疑问:biasesweights 代码背后的生成过程

知识补充:np.random.randn(m, n)

  • in:
        a = np.random.randn(3, 1)
        print(a)
    
  • out:
        array([[-1.16708562],
               [-1.3747519 ],
               [ 1.53180853]])
    



解析:self.biases = [ np.random.randn(y, 1) for y in sizes[1:] ]

  • in
        net = [3, 3, 4, 1]
        a = [np.random.randn(y, 1) for y in net[1:]]
        print(a)
    
  • out:
        biases:
        [array([[-1.28584926],
                [-0.10648672],
                [ 0.96696939]]), 第二层
         array([[ 0.18739769],
                [ 0.31517292],
                [-0.93142655],
                [-1.34400024]]), 第三层
         array([[1.02909078]])]  第四层
    



解析:self.weights = [ np.random.randn(y, x) for x, y in zip(sizes[:-1], sizes[1:]) ]

  • 易知:

        zip(sizes[:-1], sizes[1:]) == [(3, 3), (3, 4), (4, 1)]
    
  • 结果应为:

        [
            np.random.randn(3, 3),
            np.random.randn(3, 4),
            np.random.randn(4, 1)
        ]
    
  • in

        a = [np.random.randn(y, x) for x, y in [(3, 3), (3, 4), (4, 1)]]
        a
    
  • out

        [array([[ 0.92428425,  1.30627121,  0.33581147],
                [-2.20627075, -0.93125026, -0.30980836],
                [ 1.39007824, -2.07998321, -0.25047032]]),
         array([[-0.31758724, -1.98424497,  0.02881466],
                [ 0.37809777, -0.32856453, -0.07872715],
                [ 0.76281222,  2.61967303, -0.34342589],
                [-0.99639051, -0.9156387 , -0.78856185]]),
         array([[-1.17977103, -1.35824605, -1.02660058, -0.52706551]])]
    






1、sigmoid

代码:

def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))

补充:np.exp()

  • in:
    a = np.exp(1)
    a
  • out:
2.718281828459045