我想根据this description实现平均感知器算法(第48页,完成psuedocode)。
我认为我非常接近,但是我无法弄清楚我需要计算每个特定索引在每次迭代期间计算的权重平均值的最后一步,然后将该值分配给a最终的权重数组。我该如何实现?
散列映射的结构是int
,它是迭代次数,然后是double[]
的数组,其中包含该迭代的权重。所以我猜输出会像
For all the hashmap keys
for the length of the hashmap value at this key index
...something
因此,如果每次迭代的第一个权重为2
,4
,3
,我想将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
}
答案 0 :(得分:1)
我想这样的事情会起作用:
//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 ];
}
但是,那么,必须用一些术语来划分最后的迭代次数
就像密钥位于密钥的末尾一样,没有更大的迭代,如果是这种情况,则除以它,就像那样。
也许这个?
//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;
}
}