Code Monkey home page Code Monkey logo

Comments (3)

agoscinski avatar agoscinski commented on August 29, 2024

I will make an attempt using rascalines test python/rascaline-torch/tests/autograd.py as base. Probably for the first attempt I only write the tests for torch array backend, because I am not sure how to do these tests for numpy-backend.

from metatensor.

Luthaf avatar Luthaf commented on August 29, 2024

I don't think the autograd checks in rascaline will help here. The idea is to check explicit forward gradients stored in ˋblock.gradient(...)`, which is not integrated with torch autograd.

A better example would be in rascaline ˋtest_utils rust module in ˋcalculators, where we do the kind of explicit finite difference that is required here.

from metatensor.

agoscinski avatar agoscinski commented on August 29, 2024

Yes realized also that the gradcheck function is more for checking backward function implementations and cannot be reused for forward gradients (at least from what I understood). I was trying to use torch.autograd.grad

import torch
import metatensor
from metatensor import Labels, TensorMap, TensorBlock

g = torch.Generator()
g.manual_seed(0)
input_values1 = torch.rand(3, 5, generator=g, requires_grad=True)
values1 = input_values1**2 # some operation that makes ϑf(values)/ϑvalues not equal one
values1_grad = torch.autograd.grad(inputs=input_values1, outputs=values1,
        grad_outputs=torch.ones_like(values1), retain_graph=True)[0]

block1 = metatensor.block_from_array(values1.detach())
block1.add_gradient(
    parameter="g",
    gradient=TensorBlock(
        values=values1_grad.detach(),
        samples=Labels.range("sample", len(values1)),
        components=[],
        properties=block1.properties,
    ),
)
tensor1 = TensorMap(Labels.range("_", 1), [block1])

output_tensor = metatensor.add(tensor1, tensor1)
output_values = values1 + values1
grad_autograd = torch.autograd.grad(
        inputs=input_values1,
        outputs=output_values,
        grad_outputs=torch.ones_like(output_values),
        retain_graph=False)[0]
grad_metatensor = output_tensor[0].gradient("g").values
print(torch.allclose(grad_autograd, grad_metatensor))

The hope was by using this util that we do not require our custom finite difference implementation, but since we want that the tests work also for numpy, it does not seem like a good approach.

from metatensor.

Related Issues (20)

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.