gpt4 book ai didi

java - 取 double[] HashMap 中每个索引的平均值并将其分配给输出 double[]

转载 作者:行者123 更新时间:2023-12-01 11:52:32 25 4
gpt4 key购买 nike

我想实现平均感知器算法,按照this description (第 48 页,附有伪代码)。

我认为我已经非常接近了,但是我在试图找出最后一步时遇到了麻烦,其中我需要计算每个特定索引的每次迭代期间计算的权重的平均值,然后将该值分配给最终的权重数组。我将如何实现?

hashmap 的结构是 int,它是迭代次数,然后是一个包含该迭代权重的 double[] 数组。所以我猜输出会是这样的

For all the hashmap keys
for the length of the hashmap value at this key index
...something

因此,如果每次迭代的第一个权重是 243,我想分配 3 的权重 到该索引的最终 double[] 数组,对于所有实例依此类推。

下面是相关代码。完整代码为here on my GitHub如果您想查看的话。

   //store weights to be averaged. 
Map<Integer,double[]> cached_weights = new HashMap<Integer,double[]>();


final int globoDictSize = globoDict.size(); // number of features

// weights total 32 (31 for input variables and one for bias)
double[] weights = new double[globoDictSize + 1];
for (int i = 0; i < weights.length; i++)
{
//weights[i] = Math.floor(Math.random() * 10000) / 10000;
//weights[i] = randomNumber(0,1);
weights[i] = 0.0;
}


int inputSize = trainingPerceptronInput.size();
double[] outputs = new double[inputSize];
final double[][] a = Prcptrn_InitOutpt.initializeOutput(trainingPerceptronInput, globoDictSize, outputs, LABEL);


double globalError;
int iteration = 0;
do
{
iteration++;
globalError = 0;
// loop through all instances (complete one epoch)
for (int p = 0; p < inputSize; p++)
{
// calculate predicted class
double output = Prcptrn_CalcOutpt.calculateOutput(THETA, weights, a, p);
// difference between predicted and actual class values
//always either zero or one
double localError = outputs[p] - output;

int i;
for (i = 0; i < a.length; i++)
{
weights[i] += LEARNING_RATE * localError * a[i][p];
}
weights[i] += LEARNING_RATE * localError;

// summation of squared error (error value for all instances)
globalError += localError * localError;
}

这是我上面提到的部分

       //calc averages
for (Entry<Integer, double[]> entry : cached_weights.entrySet())
{
int key = entry.getKey();
double[] value = entry.getValue();
// ...
}

/* Root Mean Squared Error */
//System.out.println("Iteration " + iteration + " : RMSE = " + Math.sqrt(globalError / inputSize));
}
while (globalError != 0 && iteration <= MAX_ITER);


//calc averages
Iterator it = cached_weights.entrySet().iterator();
while( it.hasNext() )
{
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());

it.remove(); // avoids a ConcurrentModificationException
}

最佳答案

我想这样的事情会起作用:

   //calc averages
for (Entry<Integer, double[]> entry : cached_weights.entrySet())
{
int key = entry.getKey();
double[] value = entry.getValue();
AVERAGED_WEIGHTS[ key - 1 ] += value[ key - 1 ];
}

但是,我猜最后必须用一些术语来除以迭代次数

就像如果 key 位于 key 的末尾,则不再有更大的迭代,如果是这种情况,则除以它,类似的事情。

也许是这个?

   //calc averages
for (Entry<Integer, double[]> entry : cached_weights.entrySet())
{
int key = entry.getKey();
double[] value = entry.getValue();
AVERAGED_WEIGHTS[ key - 1 ] += value[ key - 1 ];

if (key == iteration)
{
AVERAGED_WEIGHTS[ key - 1 ] /= key;
}
}

关于java - 取 double[] HashMap 中每个索引的平均值并将其分配给输出 double[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28688007/

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