Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NeuroEvolution #31

Merged
merged 19 commits into from
Sep 1, 2023
Merged

NeuroEvolution #31

merged 19 commits into from
Sep 1, 2023

Conversation

shiffman
Copy link
Member

I'm continuing from where I left off with #23 and moving onto neuro-evolution. One API note, I've changed the noTraining option to neuroEvolution, so now creating the empty model looks like:

let model = ml5.neuralNetwork({
  inputs: 5,
  outputs: ['flap', 'no flap'],
  task: 'classification',
  neuroEvolution: true,
});

I thought about neuroEvolution being a "task", but specifying "classification" or "regression" is still needed.I could adapt task to accept an array of options, but I think that overcomplicates things. This flag is necessary in order for the model to be initialized properly for use with some of the neuroevolution functionality. Thoughts welcome!

Also, I noticed that my VSCode settings made quite a few changes to whitespace, quote style, etc. @ziyuan-linn I followed the CONTRIBUTING.md instructions, did I miss any steps perhaps? I noticed the .prettierrc.json file is empty, do we need to add options to it?

@ziyuan-linn
Copy link
Member

Hi @shiffman , the .prettierrc file is empty so it would override any user settings and use Prettier's default settings for this workspace. I looked at your commit, and it seems like Prettier is formatting correctly. I think the problem is that the original files have never been formatted by Prettier. I will make a PR formatting all the existing files to resolve this issue.

@shiffman
Copy link
Member Author

@ziyuan-linn I can't explain it, but for some reason the Pose Detection model caused an error in ml5.neuralNetwork(). I think maybe it may have required updating some dependencies which caused something to break. See this commit de87879 where I temporarily disabled the pose model. After this commit, when I run yarn install and yarn run start the neural network examples work again. If I add pose detection back in, run yarn install they break.

I think it may be due to a tfjs-data update but I haven't had time to diagnose this. Do you have any idea or would you be able to look into this? cc @gohai as another pair of eyes.

@ziyuan-linn
Copy link
Member

@shiffman That's strange. I can take a look today!

@ziyuan-linn
Copy link
Member

@shiffmanThe error should go away after merging #34 :)

@shiffman
Copy link
Member Author

Thank you @ziyuan-linn, what's weird is that I am explicitly using ml5.tf.setBackend("cpu"); for my neuroEvolution examples which is actually much higher performance for tiny models. And I still got the error! I wrote a short explanation in the nature of code book:

Screen Shot 2023-08-14 at 6 04 05 PM

Let me try merging #34, updating this branch, and see where it leaves us!

@shiffman
Copy link
Member Author

Ah, ok, so the issue is resolved but I am not sure why my call to ml5.tf.setBackend("cpu"); was not working previously. Now with the forced tf.setBackend("webgl"); in the init() method, I am not able to properly override the backend. I'm hard-coding it to CPU for now in b460d80 just so I can keep working on my examples. Maybe we can look at a better way to handle this as you suggested in #34!

Is the backend a global setting for any given webpage? If so then I don't think it makes sense to add an option to ml5.neuralNetwork() since it would give the appearance of different backends for different models? We could also just wrap tf.setBackend() and add a utility method:

ml5.setBackend("cpu"):

This would be my preference! What do you think @ziyuan-linn?

@ziyuan-linn
Copy link
Member

@shiffman Ohh I think calling ml5.tf.setBackend("cpu") before initializing Bird might have worked previously before the forced called was added to init().

function setup() {
  createCanvas(640, 240);
  ml5.tf.setBackend("cpu");//<------- moved to before Bird constructor call
  for (let i = 0; i < 200; i++) {
    birds[i] = new Bird();
  }
  pipes.push(new Pipe());
  
}

My guess is that if the Bird constructor is called and subsequently calls ml5.nerualNetwork() before setBackend("cpu"), the program would run with the default webgpu backend and crash before the backend could be changed.

I think tf.setBackend is a global setting, and I agree that a ml5 utility method would make more sense. I would also be happy to implement it, which shouldn't be too hard.

@shiffman shiffman changed the title [WIP] NeuroEvolution NeuroEvolution Aug 19, 2023
@shiffman shiffman requested review from ziyuan-linn and gohai August 19, 2023 13:26
@shiffman shiffman marked this pull request as ready for review August 19, 2023 13:26
@shiffman
Copy link
Member Author

This is just about ready but I'd like to resolve and merge #36 first. Also, workflow-wise, I wonder if we should be using "squash and merge" since our development branches involve a lot of small steps and changes? This could apply to #26 as well? @ziyuan-linn @gohai

@ziyuan-linn
Copy link
Member

@shiffman I think squash and merge can be a good idea for the big development branches! I don't really have a strong opinion either way.

@shiffman
Copy link
Member Author

These examples along with a completed draft of Chapter 10: Nature of Code are ready to go! @gohai and @ziyuan-linn any comments before I squash and merge?

The examples could probably use some cleanup / comments, I will continue to refine as I work on the chapter.

Copy link
Member

@ziyuan-linn ziyuan-linn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I briefly read through chapter 10 of The Nature of Code. I was only vaguely familiar with the concept of reinforcement learning, but I think the book explained the concept very well through the step-by-step flappy bird example! The Neuroevolution Ecosystem example is also fascinating and makes me want to try it on my own.

The examples code seems very clear to me and I was able to run them without any problem.

@shiffman
Copy link
Member Author

shiffman commented Sep 1, 2023

I'm working on reviewing pull requests today! I think I'll go ahead and merge this one first!

@shiffman shiffman merged commit b445e55 into main Sep 1, 2023
@gohai gohai deleted the neuroevolution branch September 16, 2023 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants