# Switching javascript neural network to java

**URL:** <https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857>\
**Category:** Coding Questions\
**Created:** [July 17, 2018, 4:53pm UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857 "2018-07-17T16:53:56Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [July 17, 2018, 4:53pm UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/1 "2018-07-17T16:53:56Z")

</div>

OK, I’m trying to convert javascript code from coding train 10.13 neural network to java & having trouble.  
here my code in java

```auto
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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [July 17, 2018, 4:59pm UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/2 "2018-07-17T16:59:38Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [July 17, 2018, 5:04pm UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/3 "2018-07-17T17:04:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [July 17, 2018, 5:50pm UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/4 "2018-07-17T17:50:13Z")

</div>

Okay cool, so what error are you getting? Have you tried narrowing your problem down to a [MCVE](https://stackoverflow.com/help/mcve)?

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [July 17, 2018, 6:32pm UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/5 "2018-07-17T18:32:54Z")

</div>

I just told you where I was getting the error.

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [July 18, 2018, 5:38am UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/6 "2018-07-18T05:38:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [July 21, 2018, 7:29pm UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/7 "2018-07-21T19:29:07Z")

</div>

main page

```auto
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);
}

```

network

```auto
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

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [August 6, 2018, 6:50am UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/8 "2018-08-06T06:50:55Z")

</div>

> [@ctrembla](#):
>
> void feedforward(float input){ return guess; }

guess is never declared…

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [August 6, 2018, 9:16am UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/9 "2018-08-06T09:16:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [August 6, 2018, 9:45am UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/10 "2018-08-06T09:45:23Z")

</div>

```auto
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…

---

<div class="post-metadata">

**Author:** ![lqdev](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lqdev/32/29_2.png) [@lqdev](https://discourse.processing.org/u/lqdev)\
**Post date:** [August 6, 2018, 11:00am UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/11 "2018-08-06T11:00:02Z")

</div>

Also, here’s a little tip:  
Arrays in Java can be declared in a shorter syntax:

```java
// 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 };

```

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [August 6, 2018, 1:30pm UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/12 "2018-08-06T13:30:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lqdev](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lqdev/32/29_2.png) [@lqdev](https://discourse.processing.org/u/lqdev)\
**Post date:** [August 6, 2018, 3:22pm UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/13 "2018-08-06T15:22:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [August 6, 2018, 4:36pm UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/14 "2018-08-06T16:36:55Z")

</div>

> [@MxFxM](#):
>
> I doubt the processing compiler does it good

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.

---

<div class="post-metadata">

**Author:** ![lqdev](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lqdev/32/29_2.png) [@lqdev](https://discourse.processing.org/u/lqdev)\
**Post date:** [August 6, 2018, 5:28pm UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/15 "2018-08-06T17:28:46Z")

</div>

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 `color`s 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.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 6, 2018, 5:45pm UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/16 "2018-08-06T17:45:28Z")

</div>

> [@lqdev](#):
>
> … replaces color literals `#rrggbb` with (probably) the `color(r, g, b)` function,

It turns them into a hexadecimal literal: `#rrggbb` → `0xffrrggbb`. 🖌  
That is, the character `#` becomes `0xff`. 😉

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [August 6, 2018, 6:03pm UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/17 "2018-08-06T18:03:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 6, 2018, 6:17pm UTC](https://discourse.processing.org/t/switching-javascript-neural-network-to-java/1857/18 "2018-08-06T18:17:24Z")

</div>

> [@neilcsmith](#):
>
> … and no support for lambdas…

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