|
|
|
@ -2,7 +2,6 @@ package org.wlld.imageRecognition.border;
|
|
|
|
|
|
|
|
|
|
import org.wlld.MatrixTools.Matrix;
|
|
|
|
|
import org.wlld.MatrixTools.MatrixOperation;
|
|
|
|
|
import org.wlld.tools.ArithUtil;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
@ -15,16 +14,27 @@ import java.util.Random;
|
|
|
|
|
*/
|
|
|
|
|
public class LVQ {
|
|
|
|
|
private int typeNub;//原型聚类个数,即分类个数
|
|
|
|
|
private MatrixBody[] model = new MatrixBody[typeNub];//原型向量
|
|
|
|
|
private MatrixBody[] model;//原型向量
|
|
|
|
|
private List<MatrixBody> matrixList = new ArrayList<>();
|
|
|
|
|
private double studyPoint = 0.1;//量化学习率
|
|
|
|
|
private int length;//向量长度
|
|
|
|
|
private boolean isReady = false;
|
|
|
|
|
private int lvqNub = 50;
|
|
|
|
|
|
|
|
|
|
public LVQ(int typeNub) {
|
|
|
|
|
public boolean isReady() {
|
|
|
|
|
return isReady;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LVQ(int typeNub, int lvqNub) {
|
|
|
|
|
this.typeNub = typeNub;
|
|
|
|
|
this.lvqNub = lvqNub;
|
|
|
|
|
model = new MatrixBody[typeNub];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MatrixBody[] getModel() {
|
|
|
|
|
public MatrixBody[] getModel() throws Exception {
|
|
|
|
|
if (!isReady) {
|
|
|
|
|
throw new Exception("not study");
|
|
|
|
|
}
|
|
|
|
|
return model;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -46,31 +56,32 @@ public class LVQ {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private double study() throws Exception {
|
|
|
|
|
double error = 0;
|
|
|
|
|
private void study() throws Exception {
|
|
|
|
|
for (MatrixBody matrixBody : matrixList) {
|
|
|
|
|
Matrix matrix = matrixBody.getMatrix();//特征向量
|
|
|
|
|
long type = matrixBody.getId();//类别
|
|
|
|
|
double distEnd = 0;
|
|
|
|
|
int id = 0;
|
|
|
|
|
for (int i = 0; i < typeNub; i++) {
|
|
|
|
|
MatrixBody modelBody = model[i];
|
|
|
|
|
Matrix modelMatrix = modelBody.getMatrix();
|
|
|
|
|
long id = modelBody.getId();
|
|
|
|
|
boolean isRight = id == type;//类别是否相同
|
|
|
|
|
//对矩阵进行修正
|
|
|
|
|
Matrix matrix1 = op(matrix, modelMatrix, isRight);
|
|
|
|
|
//修正矩阵与原矩阵的范数差
|
|
|
|
|
double dist = vectorEqual(modelMatrix, matrix1);
|
|
|
|
|
//将修正后的向量进行赋值
|
|
|
|
|
modelBody.setMatrix(matrix1);
|
|
|
|
|
//统计变化值
|
|
|
|
|
error = ArithUtil.add(error, dist);
|
|
|
|
|
double dist = vectorEqual(modelMatrix, matrix);
|
|
|
|
|
if (distEnd == 0 || dist < distEnd) {
|
|
|
|
|
id = matrixBody.getId();
|
|
|
|
|
distEnd = dist;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
MatrixBody modelBody = model[id];
|
|
|
|
|
Matrix modelMatrix = modelBody.getMatrix();
|
|
|
|
|
boolean isRight = id == type;
|
|
|
|
|
Matrix matrix1 = op(matrix, modelMatrix, isRight);
|
|
|
|
|
modelBody.setMatrix(matrix1);
|
|
|
|
|
}
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//比较两个向量之间的范数差
|
|
|
|
|
private double vectorEqual(Matrix matrix1, Matrix matrix2) throws Exception {
|
|
|
|
|
public double vectorEqual(Matrix matrix1, Matrix matrix2) throws Exception {
|
|
|
|
|
Matrix matrix = MatrixOperation.sub(matrix1, matrix2);
|
|
|
|
|
return MatrixOperation.getNorm(matrix);
|
|
|
|
|
}
|
|
|
|
@ -100,9 +111,9 @@ public class LVQ {
|
|
|
|
|
model[i] = matrixBody;
|
|
|
|
|
}
|
|
|
|
|
//初始化完成
|
|
|
|
|
for (int k = 0; k < 1000; k++) {
|
|
|
|
|
double error = study();
|
|
|
|
|
System.out.println("error==" + error);
|
|
|
|
|
for (int i = 0; i < lvqNub; i++) {
|
|
|
|
|
study();
|
|
|
|
|
}
|
|
|
|
|
isReady = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|