OK, I’m trying to convert javascript code from coding train 10.13 neural network to java & having trouble.
here my code in java
NeuralNetwork nn;
void setup(){
nn = new NeuralNetwork(2,2,1);
float [] input = new float[2];// here I need to create the inputs to send to he outputs
input[0] = 1;
input[1] = 0;
float output = nn.feedforward(input);
}
here the js version what I’m trying to convert
function setup(){
let nn = new NeuralNetwork(2, 2, 1);
let input = [1, 0];
let output = nn.feedforward(input);
}
Can anyone tell me if I have converted the code so far properly? If not I would love help been trying to do this for a while now.
Generally speaking, you shouldn’t try to convert code line-by-line. Instead, you should take a step back and think about what the program is doing in the first language, and then implement that in the target language. It’s a bit like translating literature: you don’t just go word-by-word. Instead, you take the overall feel of the entire thing and try to approximate that the best you can in the target language.
But for this question, can you be more specific about what you’re asking? Does your code compile? Does it do what you expected?
I find js hard to understand since I use java. This code is suppose to get 3 inputs the number of inputs, the number of hidden and the number of outputs then assign the inputs, which I think I’ve done & then place them in the feedforward function which comes to an error.
Do you have a class NeuralNetrok yet? And if so, please post it because there feedforward () should be located and that’s where the problem seems to be.
Also, here’s a little tip:
Arrays in Java can be declared in a shorter syntax:
// so instead of this
float[] longest = new float[2];
longest[0] = 1;
longest[1] = 2;
// do this for arrays in method arguments
new float[] { 1, 2 }
// or this for initializing an array
float[] shortest = { 1, 2 };
There is no “Processing compiler”, but you keep hating on it! Of the two compilers actually involved, the Java JIT one is the important one here and that’s world class.
@lqdev is right, it’s a shorter (and less error prone) syntax.
I agree here, there is no Processing compiler. However, there is a Processing preprocessor. This is a big difference, because what actually happens is that Processing uses ANTLR to translate your sketch into a Java class, so it joins all of your tabs together, adds a main method, adds all the necessary imports (and moves them out of the class so that the Java compiler won’t spit out any errors), changes colors to ints, replaces color literals (#rrggbb) with (probably) the color(r, g, b) function, and so much more I’m not able to describe here.
Yes, there’s a preprocessor, which puts the code in context (great) and makes pointless minor syntax / language changes (buggy PITA and no support for lambdas). But it has no effect on how the code above actually compiles, either to bytecode or at runtime.