Switching javascript neural network to java

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.

Okay cool, so what error are you getting? Have you tried narrowing your problem down to a MCVE?

I just told you where I was getting the 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.

main page

NeuralNetwork nn;
void setup(){
  nn = new NeuralNetwork(2,2,1);
  float [] input = new float[2];
  input[0] = 1;
  input[1] = 0;
  float output = nn.feedforward(input);


class NeuralNetwork{
  float input, hidden, output;
  NeuralNetwork(float input, float hidden, float output){
    this.input = input;
    this.hidden = hidden;
    this.output = output;
    Matrix weightsih = new Matrix(input);
  void feedforward(float input){
    return guess;

here where I got so far

guess is never declared…

And you can’t have a void method return anything either!

float feedforward(float input) {
  return 0;

This would work but it won’t do anything.
Also you don’t want a float, you want a float array…

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 };
1 Like

A good compiler should make no difference out of those 3 but I doubt the processing compiler does it good. I have no idea how it’s handled though.

@MxFxM I’m not saying the compiler does it bad, I’m just saying that there’s a shorter syntax.

There is no “Processing compiler”, but you keep hating on it! :wink: 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.

1 Like

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.

1 Like

It turns them into a hexadecimal literal: #rrggbb -> 0xffrrggbb. :paintbrush:
That is, the character # becomes 0xff. :wink:

1 Like

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.

Actually, we can’t use any Java 8’s new features even in “.java” tabs, where the pre-processor isn’t used!