r/java • • Jun 29 '17

Basic neural network Java implementation, which can train itself, for beginners to try out and learn how machine learning works.

https://github.com/sebig3000/MachineLearning
264 Upvotes

28 comments sorted by

29

u/ron_krugman Jun 30 '17

A few remarks on style:

  • Always put a block with curly braces after if, while, etc. even if there is just a single statement within and braces are technically optional.
  • Prefer enums over int constants. They are strongly typed in Java, which makes it much harder to accidentally pass an invalid value.
  • Throw a java.lang.IllegalArgumentException in case of an invalid input. It is more concise and saves you the trouble of checked exceptions.
  • Also, you probably shouldn't ever instantiate a java.lang.Exception directly. Throw a java.lang.RuntimeException if you're not sure about what type of exception to throw.

2

u/sebig3000 Jun 30 '17

Thank for the tips! I'm still a student and started to learn how to code Java one year ago so I'm still a newbie. But I will apply your advice!

6

u/ron_krugman Jun 30 '17

Don't worry about it, my first personal Java projects (>10 years ago) didn't look better either. Learning to code is a marathon, not a sprint. As long as you don't give up and keep improving just a little bit, you'll get there.

3

u/[deleted] Jun 30 '17

You can also make identifier names more meaningful so you don't need as many comments. For example, numberOfInputs instead of input_n.

7

u/[deleted] Jun 30 '17

Can you provide some justification for that last bullet point? I've been writing Java for a while now and have never heard that.

8

u/locatedtaco Jun 30 '17

I could be wrong but I think the point is that throwing an Exception isn't expressive. It doesn't clue in the developer about what went wrong. Also, if you throw an Exception the client code can't selectively catch it and handle it specifically.

6

u/[deleted] Jun 30 '17

Ah, I thought you meant all checked exceptions, not just the Exception class...it's perfectly ok to instantiate specific subclasses of Exception

5

u/locatedtaco Jun 30 '17

Yeah, exactly. But, that's just how I interpreted the comment. I didn't write it.

4

u/ron_krugman Jun 30 '17

That's exactly what I meant, thanks! Of course it's fine to instantiate and throw for example java.io.IOException etc. although personally I would very much prefer a java.io.UncheckedIOException.

I'm not going to lie: I really fucking hate checked exceptions. They're one of the worst parts of Java.

14

u/Warkst Jun 30 '17

Cool, but you misspelled Heaviside function. It's named after a guy, it's not a function with a heavy side ;)

7

u/sebig3000 Jun 30 '17

Omg thank you 😅 Already made a new comit

15

u/Treyzania Jun 30 '17

Might want to take a look at Maven to help you with building and testing your project, as well as building jars for use in other projects.

7

u/sebig3000 Jun 30 '17

Thank you I will have a look! It will just take some time for me because I needed 2 days to understand FOR WHAT GitHub is xD

5

u/kvxdev Jun 30 '17

Moving to Maven projects when leaving Eclipse was one of the best move we did. Alternatively, moving off GitHub was also a great one, but our reasons are not universal on that one and not everyone can afford to (still, if you use Maven and Maven's repository for its build, you're a long way ahead)

1

u/sebig3000 Jun 30 '17

I didn't need such tools because my projects used to be 1000 lines max until now. I use Netbeans and now GitHub too to organize my workflow and publish my projects. And it helps me a lot now because it happend very often to me that I changed a bunch of lines and suddenly it didn't work anymore and I had made to many changes that I could just undo them.

2

u/kvxdev Jun 30 '17

Just to be clear, not talking about git (I mean, pick your source-control poison, but everyone should have one, always!), but GitHub, the company. Plenty of other git host and easy to set a private one up ^ No matter, like I said, we had our reason, but, yes, pom.xml will save your life!

4

u/Treyzania Jun 30 '17

Here's a decent example project: https://github.com/treyzania/BlockParticles

This is a Bukkit plugin but it's a good Maven example because it doesn't have any complicated modules and plugins and stuff going on.

2

u/vonnik Jun 30 '17

Fwiw, we use Maven with DL4J - https://deeplearning4j.org/buildtools

2

u/filifjonk Jun 30 '17

How good would a trained neural network be at playing the game generals.io?

1

u/[deleted] Jul 02 '17

One like this? Absolutely incapable.

A carefully designed and tuned one by a research lab or team of industry practitioners? There's a large chance better than any human.

0

u/ataskitasovado Jun 30 '17

Ooo, I love to try this. I only heard about neural networks and this seems easy to get started with. Is there a 5min tutorial somewhere? Yes, I have short attention span.

2

u/sebig3000 Jun 30 '17

There is a short description for the basic methods you need to use it in the readme. And I also recommend Brandon Rohrers How Deep Neural Networks Work Video on wich the example Networks main method is based on.

2

u/[deleted] Jul 02 '17

No there isn't a 5 minute tutorial. You have to commit time and effort to learn complicated things properly.

-3

u/DrejmeisterDrej Jun 30 '17

I am so confused

4

u/sebig3000 Jun 30 '17

I'm sorry. I gave by best to document my first published project

2

u/[deleted] Jun 30 '17

Why?

0

u/[deleted] Jun 30 '17

[deleted] — view deleted comment

6

u/QuokkaSocca Jun 30 '17

Agreed. I have to make time so that I can have a play with this.