DiffEqML / torchdyn

A PyTorch library entirely dedicated to neural differential equations, implicit models and related numerical methods
https://torchdyn.org
Apache License 2.0
1.4k stars 130 forks source link

Error with lower dimensional mappings within `f` #125

Open nprasadmm opened 2 years ago

nprasadmm commented 2 years ago

I'm not sure if this is a bug, an error in my code, or by design, but my ODE errors out unless input_dim == output_dim at all steps within the f function. For example, if I were to apply the layer nn.Linear(18, 2), I would get the following error:

             File "/home/ubuntu/anaconda3/envs/storage-env/lib/python3.9/site-packages/torchdyn/core/neuralde.py", line 93, in forward
t_eval, sol =  super().forward(x, t_span)
             File "/home/ubuntu/anaconda3/envs/storage-env/lib/python3.9/site-packages/torchdyn/core/problems.py", line 79, in forward
return self.odeint(x, t_span)
             File "/home/ubuntu/anaconda3/envs/storage-env/lib/python3.9/site-packages/torchdyn/core/problems.py", line 75, in odeint
return self.autograd_function(self.vf_params, x, t_span)
             File "/home/ubuntu/anaconda3/envs/storage-env/lib/python3.9/site-packages/torchdyn/numerics/sensitivity.py", line 37, in forward
t_sol, sol = generic_odeint(problem_type, vf, x, t_span, solver, atol, rtol, interpolator, B, 
             File "/home/ubuntu/anaconda3/envs/storage-env/lib/python3.9/site-packages/torchdyn/numerics/sensitivity.py", line 24, in generic_odeint
return odeint(vf, x, t_span, solver, atol=atol, rtol=rtol, interpolator=interpolator, return_all_eval=return_all_eval)
            File "/home/ubuntu/anaconda3/envs/storage-env/lib/python3.9/site-packages/torchdyn/numerics/odeint.py", line 83, in odeint
return _fixed_odeint(f_, x, t_span, solver) 
           File "/home/ubuntu/anaconda3/envs/storage-env/lib/python3.9/site-packages/torchdyn/numerics/odeint.py", line 412, in _fixed_odeint
_, x, _ = solver.step(f, x, t, dt)
           File "/home/ubuntu/anaconda3/envs/storage-env/lib/python3.9/site-packages/torchdyn/numerics/solvers.py", line 89, in step
k2 = f(t + c[0] * dt, x + dt * (a[0] * k1))
           RuntimeError: The size of tensor a (18) must match the size of tensor b (2) at non-singleton dimension 1

This also happens if other dimensions change through pooling operations. I would appreciate any help in resolving this.

Zymrael commented 2 years ago

You are explicitly parametrizing the derivative (i.e. vector field) with your neural network, which has to have the same dimension as the state.

nprasadmm commented 2 years ago

I see. So assuming my neural network operates on a batch of 18-dimensional vectors, how would I plot the flows for this ode? Would I have to run t-SNE on each step of the trajectory to reduce to 2D vectors?

1209098482 commented 2 years ago

nice,i have same problem,