Jump to content
  • Advertisement
Sign in to follow this  
cossie

[java] Vector problem

This topic is 4171 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!