GithubHelp home page GithubHelp logo

Comments (3)

Nerrror avatar Nerrror commented on September 26, 2024 2

tmm_fast_torch is deprecated, I'll remove it with the next release. The Pytorch functionality is directly integrated into the coh_vec_tmm_disp_mstack function (terrible name, will change that in a future release, too). You can just pass either a torch.Tensor or a np.array to the function and it will compute with both.

from tmm_fast.

driofernandes avatar driofernandes commented on September 26, 2024

thanks! essentially I'm trying to reproduce the example from the Appendix 3 of your paper. I tried to replace tmm_fast_torch by coh_vec_tmm_disp_mstack, but I get an error (see below)

import numpy as np
import torch
from tmm_fast.vectorized_tmm_dispersive_multistack import coh_vec_tmm_disp_mstack as tmm

wl = np.linspace(500, 900, 301)*1e-9
theta = np.deg2rad(np.linspace(0, 90, 301))

n_layers = 12
stack_layers = np.random.uniform(20, 150, n_layers)*1e-9

stack_layers[0] = stack_layers[-1] = np.inf
optical_index = torch.tensor(np.random.uniform(1.2, 5, n_layers*len(wl)).reshape(1,n_layers,len(wl)))
optical_index[0,-1,0] = 1

stack_layers = torch.tensor(stack_layers.reshape(1,n_layers), requires_grad=True)

wl = torch.tensor(wl)
theta = torch.tensor(theta)
result = tmm('s', optical_index, stack_layers, theta, wl)['R']

mse = torch.nn.MSELoss()
error = mse(result, torch.zeros_like(result))
error.backward()

Nb: different from the function in the paper, the coh_vec_tmm_disp_mstack function accepts either numpy arrays or torch tensors. that's the reason for the conversion in my version of the code

this is the error I get:

Exception has occurred: RuntimeError
one of the variables needed for gradient computation has been modified by an inplace operation: [torch.DoubleTensor [1, 301, 301, 12]], which is output 0 of AsStridedBackward0, is at version 1; expected version 0 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!
  File "G:\My Drive\05_Other_projects\04_filters\test2.py", line 29, in <module>
    error.backward()
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.DoubleTensor [1, 301, 301, 12]], which is output 0 of AsStridedBackward0, is at version 1; expected version 0 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

thanks for your help!

from tmm_fast.

Nerrror avatar Nerrror commented on September 26, 2024

Hey, I think the problem was in how the clamping was done with the delta function. I published a new release an PyPi (v0.2.1) with some minor bugfixes, this should also fix the problem you're encountering (It worked for me at least).

Btw, you can now directly do

from tmm_fast import coh_tmm

instead of the unwieldy vectorized_tmm_dispersive_multistack

from tmm_fast.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.