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

Support tracing Variable during computation and compilation by using tracing_variable() function #472

Merged
merged 11 commits into from
Sep 9, 2023

Conversation

chaoming0625
Copy link
Collaborator

@chaoming0625 chaoming0625 commented Sep 9, 2023

Flax and other libraries support object-oriented (OO) programming by providing variable tracing during computation and transformations. On the contrary, BrainPy is designed to be Pythonic, and its OO interface supports tracing variables before transformations. Now, we are excited that BrainPy also supports tracing variables during transformations.

Particularly, we provide .tracing_variable().

Here is the usage example::

      class Example(bm.BrainPyObject):
         def fun(self):
           # The first time of calling `.fun()`, this line will create a Variable instance.
           # If users repeatedly call `.fun()` function, this line will not initialize variables again.
           # Instead, it will return the variable has been created.
           self.tracing_variable('a', bm.zeros, (10,))

           # The created variable can be accessed with self.xxx
           self.a.value = bm.ones(10)

           # Calling this function again will not reinitialize the
           # variable again, Instead, it will return the variable
           # that has been created.
           a = self.tracing_variable('a', bm.zeros, (10,))

@chaoming0625
Copy link
Collaborator Author

@ztqakita This functionality will resolve the problem in STDP models.

@chaoming0625
Copy link
Collaborator Author

To solve issue #471

Copy link
Collaborator

@ztqakita ztqakita left a comment

Choose a reason for hiding this comment

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

That's a wonderful addition! Now I can use this new function in update_weight method!

@chaoming0625 chaoming0625 merged commit d83caa4 into brainpy:master Sep 9, 2023
19 checks passed
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