Help Needed: Trying to convert from Processing Script (JAVA) to C++

Status
Not open for further replies.

mjs513

Senior Member+
I am trying to convert a Neural Network sketch that was written for Processing, which is in JAVA. I go most of it converted using vectors instead of arraylists and it will compile and run on the Teensy 3.5. Unfortunately it then hangs on one construct. BTW: after trying to manually convert it I used a Java to C++ program which worked for some of the conversion but still had to make some changes. The construct that doesn't work is:
Code:
layers[0]->neurons[0]->getConnectionCount()
layers is in its own class as well as neurons. Tried a few variations but still having issues.

Program is too long to post so I am attaching the original Java file and the C++ file I am using:

Teensy Sketch: View attachment NN_AB.zip

Processing Sketch: View attachment sketch_180923a.zip

The problem child is in the neuralnetwork.cpp file.

Any help would be appreciated..
Thanks
Mike
 
Between working on other projects I think I might have isolated the problem, maybe.

The class NerualNetwork creates layers, connections and neurons. The class calls the class Layer it creates a vector of Layer class pointers using
Code:
 std::vector<Layer*> layers = {};
. The function then creates Neurons from the Neuron class which again creates a vector of Neuron pointers. Putting print statements in the Neuron class looks like it works correctly but when I try and use the neurons created in Layer it will not access the values and hangs if I try to access the neurons from the Layer class.

After doing some searching around and I think I found the issue, namely, when using the pointers the sketch is the Neuron vector of pointers contains a pointer to an object that doesn't exist anymore once it looses its original focus.

The solution recommended is to use "smart pointers" or a "copy constructor". Smart pointers recommend using "shared_ptr" with the vectors in questions. Still reading up on all this. Will keep you all posted.

EDIT: Just found an interesting tutorial on "Smart Pointers" if anyone is interested: https://thispointer.com/category/c/smart-pointers/
 
Last edited:
Thanks @WMXZ. Think you mean this post: https://forum.pjrc.com/threads/44509-Arduino-1-8-3?p=144839&viewfull=1#post144839. Know it was also discussed in KurtE's thread on teaching new tricks to old dogs. :)

Still reading on differences between smart pointers (unique_ptr, shared_ptr, make_unique, make_shared etc.), that's today's project.

Thanks for the references though.

Mike

UPDATE: 2;49 pm EST. Changed to shared_ptr/make_shared and it seemed to resolved the issue but have another issue that I have to figure out. :) Always something. Just wanted to post back.
 
Last edited:
Sorted out the last problem I think - I had to create a vector of vectors. Always something new which is good. I did get it finally running and its dumping results but the convergence times are minutes for a simple 2 layer 2 node neural network. Also the results are a lot different that what the processing code is showing for the same data. So there is probably something else wrong that I have not found yet. If I do I will post the code unless someone requests it.

To be honest the ANN library that I mentioned before converges faster.
 
Status
Not open for further replies.
Back
Top