NeuroDiffGym / neurodiffeq

A library for solving differential equations using neural networks based on PyTorch, used by multiple research groups around the world, including at Harvard IACS.
http://pypi.org/project/neurodiffeq/
MIT License
680 stars 89 forks source link

Using Special type activation function #162

Closed SK-Math closed 2 years ago

SK-Math commented 2 years ago

Hi,

I am pretty new to neurodiffeq, thank you very much for an excellent library. I want to use special type activation function in particular f(x)= cos(1.75 x)* exp (-x**2/2) so how I will define the class for using this activation function? and how to call this function in this line fcnn=FCNN(hidden_units=(50,50, ), actv=nn.Tanh) Thank you in advance

shuheng-liu commented 2 years ago

Hi @SK-Math , you can easily achieve this by implementing your own torch module.

import torch
from neurodiffeq.networks import FCNN

class MyActivation(torch.nn.Module):
    def forward(self, x):
        return torch.cos(1.75 * x) * torch.exp(-x**2 / 2)

fcnn = FCNN(hidden_units=(50, 50), actv=MyActivation)

Let me know if there are further questions.

SK-Math commented 2 years ago

Thnka you @shuheng-liu for your quick responce

SK-Math commented 2 years ago
def forward(self, x):
        return  x*torch.exp(0.5)

when calling in solver getting error like TypeError: exp(): argument 'input' (position 1) must be Tensor, not float how to fix it

shuheng-liu commented 2 years ago

exp(0.5) is just a constant. You can use np.exp or math.exp instead of torch.exp, which only works for pytorch tensors (similar to numpy arrays)

SK-Math commented 2 years ago

Hi @shuheng-liu How can I use Butterfly Optimization Algorithm instead of default ADAM optimization? As I see torch.optim. has not Butterfly Optimization . How to plot Loss and validation graph during training ? Thank you

shuheng-liu commented 2 years ago

I haven't heard of the butterfly optimization algorithm. If you really want to try it out, you probably have to implement your own Optimizer. Here is an article.

Can you be a little bit more specific as to "Loss and validation graph"? In general, you can inject behavior, including custom plotting and logging using the callback feature.

SK-Math commented 2 years ago
  1. Hi, Can I write ode= 0.5*u''(x) + u'(0.5x) + u(x)* like `ode= lambda u, t: [ 0.5 diff(u, t, order=2) + diff(u, t/2) + u]` Just want to confirm writing of this term u'(0.5x)** please check.

  2. Is neurodiffeq support fractional order diff. eq?

Thank you

SK-Math commented 2 years ago

Hi,

  1. Can I write ode= 0.5u''(x) + u'(0.5x) + u(x) like ode= lambda u, t: [ 0.5 diff(u, t, order=2) + diff(u, t/2) + u] Just want to confirm writing of this term u'(0.5x) please check.

  2. Is neurodiffeq support fractional order diff. eq?

Thank you

shuheng-liu commented 2 years ago

Hi, the first equation is, by definition, not a differential equation. It is a generic functional equation with differential operators. Hence, NeuroDiffEq cannot be used to solve this equation out of the box. But you can still solve it with neural networks, you just have to write some codes by yourself.

At the moment, NeuroDiffEq doesn't support fractional derivatives.