Python中类的定义与调用

来源:互联网 发布:网络餐饮服务 编辑:程序博客网 时间:2024/05/23 10:46
#!/usr/bin/pythonimport numpy as np'''define'''class Network:    def __init__(self,sizes):        self.num_layers=len(sizes)        self.sizes=sizes        self.biases=[np.random.randn(y,1) for y in sizes[1:]]  # 3*1 and 1*1        self.weights=[np.random.randn(x,y) \                    for x,y in zip(sizes[1:],sizes[:-1])]      # 3*2 and 1*3    def feedforward(self,a):        for b,w in zip(self.biases,self.weights):            a=sigmoid(np.dot(w,a)+b)        return adef sigmoid(z):    return 1.0/(1.0+np.exp(-z))'''invoke'''net=Network([2,3,1])net.feedforward([[2],[3]])