Main Content


LVQ2.1 weight learning function


[dW,LS] = learnlv2(W,P,Z,N,A,T,E,gW,gA,D,LP,LS)
info = learnlv2('code')


learnlv2 is the LVQ2 weight learning function.

[dW,LS] = learnlv2(W,P,Z,N,A,T,E,gW,gA,D,LP,LS) takes several inputs,


S-by-R weight matrix (or S-by-1 bias vector)


R-by-Q input vectors (or ones(1,Q))


S-by-Q weighted input vectors


S-by-Q net input vectors


S-by-Q output vectors


S-by-Q layer target vectors


S-by-Q layer error vectors


S-by-R weight gradient with respect to performance


S-by-Q output gradient with respect to performance


S-by-S neuron distances


Learning parameters, none, LP = []


Learning state, initially should be = []

and returns


S-by-R weight (or bias) change matrix


New learning state

Learning occurs according to learnlv2’s learning parameter, shown here with its default value. - 0.01

Learning rate

LP.window - 0.25

Window size (0 to 1, typically 0.2 to 0.3)

info = learnlv2('code') returns useful information for each code character vector:


Names of learning parameters


Default learning parameters


Returns 1 if this function uses gW or gA


Here you define a sample input P, output A, weight matrix W, and output gradient gA for a layer with a two-element input and three neurons. Also define the learning rate LR.

p = rand(2,1);
w = rand(3,2);
n = negdist(w,p);
a = compet(n);
gA = [-1;1; 1]; = 0.5;

Because learnlv2 only needs these values to calculate a weight change (see “Algorithm” below), use them to do so.

dW = learnlv2(w,p,[],n,a,[],[],[],gA,[],lp,[])

Network Use

You can create a standard network that uses learnlv2 with lvqnet.

To prepare the weights of layer i of a custom network to learn with learnlv2,

  1. Set net.trainFcn to 'trainr'. (net.trainParam automatically becomes trainr’s default parameters.)

  2. Set net.adaptFcn to 'trains'. (net.adaptParam automatically becomes trains’s default parameters.)

  3. Set each net.inputWeights{i,j}.learnFcn to 'learnlv2'.

  4. Set each net.layerWeights{i,j}.learnFcn to 'learnlv2'. (Each weight learning parameter property is automatically set to learnlv2’s default parameters.)

To train the network (or enable it to adapt),

  1. Set net.trainParam (or net.adaptParam) properties as desired.

  2. Call train (or adapt).


learnlv2 implements Learning Vector Quantization 2.1, which works as follows:

For each presentation, if the winning neuron i should not have won, and the runnerup j should have, and the distance di between the winning neuron and the input p is roughly equal to the distance dj from the runnerup neuron to the input p according to the given window,

min(di/dj, dj/di) > (1-window)/(1+window)

then move the winning neuron i weights away from the input vector, and move the runnerup neuron j weights toward the input according to

dw(i,:) = -*(p'-w(i,:))
dw(j,:) = +*(p'-w(j,:))

Version History

Introduced before R2006a

See Also

| |