c++parameterspytorchlayertorchscript

Torchscript/C++ jit::trace model - Accessing layers parameters


I have a model I trained in python, traced using torch.jit.trace, and load into C++ using torch::jit::load.

Is there a way to access the last layer to pull the value for the models required output depth (for example, if it is a Conv2D layer going from 16 -> 2, I want to predefine a tensor for a depth [b,d->2,x,y] of 2)?


Solution

  • Not the most elegant way of solving this, but the most straightforward was just passing a dummy tensor through and accessing the shape. Another way I did try was accessing the parameter list and looking for "softmax", unfortunately I couldn't guarantee everyones model will spell it the same way when searching for this. If someone else has a good answer for this feel free to share, but this will have to do for now.