gpt4 book ai didi

c - ANN : Learning Vector Quantization not working

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:01:25 26 4
gpt4 key购买 nike

我希望这里有人可以帮助我:我正在尝试实现一个神经网络来查找以二维集群形式呈现的数据集群。我尝试遵循 wikipedia 上描述的标准算法:我寻找每个数据点的最小距离,并更新该神经元对数据点的权重。当总距离足够小时,我停止这样做。

我的结果找到了大部分聚类,但在 View 上是错误的,虽然它计算了一个永久距离,但它不再收敛。我的错误在哪里?

typedef struct{
double x;
double y;
}Data;

typedef struct{
double x;
double y;
}Neuron;

typedef struct{
size_t numNeurons;
Neuron* neurons;
}Network;

int main(void){
srand(time(NULL));

Data trainingData[1000];
size_t sizeTrainingData = 0;
size_t sizeClasses = 0;
Network network;

getData(trainingData, &sizeTrainingData, &sizeClasses);

initializeNetwork(&network, sizeClasses);
normalizeData(trainingData, sizeTrainingData);
train(&network, trainingData, sizeTrainingData);

return 0;
}

void train(Network* network, Data trainingData[], size_t sizeTrainingData){
for(int epoch=0; epoch<TRAINING_EPOCHS; ++epoch){
double learningRate = getLearningRate(epoch);
double totalDistance = 0;
for(int i=0; i<sizeTrainingData; ++i){
Data currentData = trainingData[i];
int winningNeuron = 0;
totalDistance += findWinningNeuron(network, currentData, &winningNeuron);
//update weight
network->neurons[i].x += learningRate * (currentData.x - network->neurons[i].x);
network->neurons[i].y += learningRate * (currentData.y - network->neurons[i].y);
}
if(totalDistance<MIN_TOTAL_DISTANCE) break;
}
}

double getLearningRate(int epoch){
return LEARNING_RATE * exp(-log(LEARNING_RATE/LEARNING_RATE_MIN_VALUE)*((double)epoch/TRAINING_EPOCHS));
}

double findWinningNeuron(Network* network, Data data, int* winningNeuron){
double smallestDistance = 9999;
for(unsigned int currentNeuronIndex=0; currentNeuronIndex<network->numNeurons; ++currentNeuronIndex){
Neuron neuron = network->neurons[currentNeuronIndex];
double distance = sqrt(pow(data.x-neuron.x,2)+pow(data.y-neuron.y,2));
if(distance<smallestDistance){
smallestDistance = distance;
*winningNeuron = currentNeuronIndex;
}
}
return smallestDistance;
}

initializeNetwork(...) 以 -1 和 1 范围内的随机权重启动所有神经元。normalizeData(...) 以某种方式标准化,因此最大值为 1。

一个例子:如果我为网络提供大约 50 个(标准化的)数据点,这些数据点被分成 3 个集群,则剩余的 totaldistance 保持在大约 7.3。当我检查神经元的位置时,它应该代表集群的中心,两个是完美的,一个在集群的边界。算法不应该把它移到中心吗?我重复算法几次,输出总是相似的(在完全相同的错误点)

最佳答案

你的代码看起来不像 LVQ,特别是你从来没有使用获胜的神经元,而你应该只移动这个

void train(Network* network, Data trainingData[], size_t sizeTrainingData){
for(int epoch=0; epoch<TRAINING_EPOCHS; ++epoch){
double learningRate = getLearningRate(epoch);
double totalDistance = 0;
for(int i=0; i<sizeTrainingData; ++i){
Data currentData = trainingData[i];
int winningNeuron = 0;
totalDistance += findWinningNeuron(network, currentData, &winningNeuron);
//update weight
network->neurons[i].x += learningRate * (currentData.x - network->neurons[i].x);
network->neurons[i].y += learningRate * (currentData.y - network->neurons[i].y);
}
if(totalDistance<MIN_TOTAL_DISTANCE) break;
}
}

您要移动的神经元位于 winningNeuron 中,但您更新了 i 个神经元,其中 i 实际上迭代了训练样本,我很惊讶你没有忘记你的内存(网络->神经元应该小于 sizeTrainingData)。我猜你的意思是类似

void train(Network* network, Data trainingData[], size_t sizeTrainingData){
for(int epoch=0; epoch<TRAINING_EPOCHS; ++epoch){
double learningRate = getLearningRate(epoch);
double totalDistance = 0;
for(int i=0; i<sizeTrainingData; ++i){
Data currentData = trainingData[i];
int winningNeuron = 0;
totalDistance += findWinningNeuron(network, currentData, &winningNeuron);
//update weight
network->neurons[winningNeuron].x += learningRate * (currentData.x - network->neurons[winningNeuron].x);
network->neurons[winningNeuron].y += learningRate * (currentData.y - network->neurons[winningNeuron].y);
}
if(totalDistance<MIN_TOTAL_DISTANCE) break;
}
}

关于c - ANN : Learning Vector Quantization not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34415829/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com