def newlvq(minmax, cn0, pc):
"""
Create a learning vector quantization (LVQ) network
:Parameters:
minmax: list of list, the outer list is the number of input neurons,
inner lists must contain 2 elements: min and max
Range of input value
cn0: int
Number of neurons in input layer
pc: list
List of percent, sum(pc) == 1
:Returns:
net: Net
:Example:
>>> # create network with 2 inputs,
>>> # 2 layers and 10 neurons in each layer
>>> net = newlvq([[-1, 1], [-1, 1]], 10, [0.6, 0.4])
"""
pc = np.asfarray(pc)
assert sum(pc) == 1
ci = len(minmax)
cn1 = len(pc)
assert cn0 > cn1
layer_inp = layer.Competitive(ci, cn0)
layer_out = layer.Perceptron(cn0, cn1, trans.PureLin())
layer_out.initf = None
layer_out.np['b'].fill(0.0)
layer_out.np['w'].fill(0.0)
inx = np.floor(cn0 * pc.cumsum())
for n, i in enumerate(inx):
st = 0 if n == 0 else inx[n - 1]
layer_out.np['w'][n][st:i].fill(1.0)
net = Net(minmax, cn1, [layer_inp, layer_out],
[[-1], [0], [1]], train.train_lvq, error.MSE())
return net
评论列表
文章目录