Python中类的定义与调用
#!/usr/bin/pythonimport numpy as np'''define'''class Network:def __init__(self,sizes):self.num_layers=len(sizes)self.sizes=sizesself.biases=[np.random.randn(y,1) for y
·
#!/usr/bin/python
import 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 a
def sigmoid(z):
return 1.0/(1.0+np.exp(-z))
'''invoke'''
net=Network([2,3,1])
net.feedforward([[2],[3]])
更多推荐
已为社区贡献9条内容
所有评论(0)