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

Bug in ide.py when handling a list of losses #1907

Open
isaingit opened this issue Dec 5, 2024 · 0 comments
Open

Bug in ide.py when handling a list of losses #1907

isaingit opened this issue Dec 5, 2024 · 0 comments

Comments

@isaingit
Copy link

isaingit commented Dec 5, 2024

Dear developers,

I found a bug in the code of ide.py, because it triggered an error like "list object is not callable" when an argument of type "loss = ['MSE', 'MSE'] is passed to the compile method.

Find below a proposal to overcome the issue:

class IDE(PDE):
     
    (...)

    def losses_train(self, targets, outputs, loss_fn, inputs, model, aux=None):           
        bcs_start = np.cumsum([0] + self.num_bcs)
        int_mat = self.get_int_matrix(True)
        f = self.pde(inputs, outputs, int_mat)
        if not isinstance(f, (list, tuple)):
            f = [f]
        f = [fi[bcs_start[-1] :] for fi in f]
        
        if not isinstance(loss_fn, (list, tuple)):
            loss_fn = [loss_fn] * (len(f) + len(self.bcs))
        elif len(loss_fn) != len(f) + len(self.bcs):
            raise ValueError(
                "There are {} errors, but only {} losses.".format(
                    len(f) + len(self.bcs), len(loss_fn)
                )
            )
            
        # losses = [loss_fn(bkd.zeros_like(fi), fi) for fi in f]
        losses = [loss_fn[i](bkd.zeros_like(fi), fi) for i, fi in enumerate(f)]
        
        for i, bc in enumerate(self.bcs):
            beg, end = bcs_start[i], bcs_start[i + 1]
            error = bc.error(self.train_x, inputs, outputs, beg, end)
            # losses.append(loss_fn(bkd.zeros_like(error), error))
            losses.append(loss_fn[len(f) + i](bkd.zeros_like(error), error))
        return losses

    def losses_test(self, targets, outputs, loss_fn, inputs, model, aux=None):
        int_mat = self.get_int_matrix(False)
        f = self.pde(inputs, outputs, int_mat)
        if not isinstance(f, (list, tuple)):
            f = [f]
        # return [
        #     loss_fn(bkd.zeros_like(fi), fi) for fi in f
        # ] + [bkd.as_tensor(0, dtype=config.real(bkd.lib)) for _ in self.bcs]
        return [
            loss_fn[i](bkd.zeros_like(fi), fi) for i, fi in enumerate(f)
        ] + [bkd.as_tensor(0, dtype=config.real(bkd.lib)) for _ in self.bcs]

Would that be OK? Thanks in advance for the attention :)

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

No branches or pull requests

1 participant