[java] Vector problem

Started by
3 comments, last by Son of Cain 17 years ago
Hi, I'm trying to code a neural network for a pathfinding project. I'm passing the output (a Vector) from one layer to the next layer and when I try to access the vector I get an Index Out Of Range error message. This is the basics of the code (apologies about the formatting): public void Process(Vector<Double> Inputs) { ... ... int currWeight = 0; for (i = 0; i < numLayers; i++ ( // if beyond input layer if (i > 0) { // copy outputs from prev. layer into inputs for current layer vInputs = vOutputs; // clear previous outputs vOutputs.clear(); } ... ... // calculated weighted sum for each neuron for (j = 0; j < numNeurons; j++) { for (k = 0; k < numInputsToNeuron; k++) { // *THIS* is where the error occurs dWSum = vInputs.get(k) * vWeights(currWeight); // pass through sigmoid etc... ... ... vOutputs.add(dWSum); } } } } I'm really puzzled as to what is causing this error, I've checked the size and capacity of vInputs - both are what they should be - and I've compared the contents of vInputs with vOutputs after I've copied them for the next layer. I've also removed the vWeights(currWeight) part, so it's definitely the vInputs.get(k) call that is causing the problem. I'd really appreciate any help with this, Thanks! Cossie
Advertisement
0. Use [source][/source] tags around your code, and copy-paste relevant code portions instead of rewriting them.

1. What is the size of the vector and the value of k when the exception is thrown?

2. My java is a bit rusty, but doesn't vInputs = vOutputs; make vInputs reference vOutputs instead of creating a new vector?
Thanks for the source code tip, i'll remember it in future!

Quote:
2. My java is a bit rusty, but doesn't vInputs = vOutputs; make vInputs reference vOutputs instead of creating a new vector?



That's exactly what the problem was! After I read your post I created a local Vector to hold the inputs and used addAll() to copy from one Vector to another which has solved the problem.

Thanks a million ToohrVyk :)
Slightly off-topic, but you may also want to use a List (ArrayList) instead of a Vector there, due to serialization issues. Plus, it may be even better to use the Collection interface here, so that the algorithm will not care about which data structure you're using, leaving you the option of profiling your code with different implementations for better results.
a.k.a javabeats at yahoo.ca
Slightly off-topic, but you may also want to use a List (ArrayList) instead of a Vector there, due to serialization issues. Plus, it may be even better to use the Collection interface here, so that the algorithm will not care about which data structure you're using, leaving you the option of profiling your code with different implementations for better results.
a.k.a javabeats at yahoo.ca

This topic is closed to new replies.

Advertisement