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

model in rust, optimizer.step in python #862

Open
xnerhu opened this issue Apr 11, 2024 · 0 comments
Open

model in rust, optimizer.step in python #862

xnerhu opened this issue Apr 11, 2024 · 0 comments

Comments

@xnerhu
Copy link

xnerhu commented Apr 11, 2024

Hello.

Is it possible, to create a model in rust using tch-rs, export it using pyo3, so it is accessible from python side (forward function), then call it from python with some tensors and do optimization part on the python side too? I see that it may be impossible due to the usage of nn::Path / VarStore.

pub type Model = Box<dyn Fn(&Tensor) -> (Tensor)>;

#[pyfunction(name = "create_rs_model")]
pub fn model(p: &nn::Path) -> Model {
    let layers = nn::seq()
        .add(nn::linear(p / "layer1", 2, 16, Default::default()))
        .add_fn(|xs| xs.relu())
        .add(nn::linear(p / "layer2", 16, 1, Default::default()));
    return Box::new(move |xs: &Tensor| layers.forward(xs));
}
def class Encoder(torch.nn.Module)
...

encoder  = Encoder()
rs_model = create_rs_model()

encoded = encoder(inputs)
out = rs_model(encoded)

loss = F...(out, true)
loss.backward()
optimizer.step()
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