dotnet / TorchSharp

A .NET library that provides access to the library that powers PyTorch.
MIT License
1.36k stars 177 forks source link

Having trouble accessing training property of module #1267

Open asieradzk opened 6 months ago

asieradzk commented 6 months ago

I tried to implement my own module and when attempting to read training property I get access violation error

from torchsharp

public virtual bool training
{
    get
    {
        bool result = NativeMethods.THSNN_Module_is_training(handle);
        CheckForErrors();
        return result;
    }
}

From my module:

        public override Tensor forward(Tensor input)
        {    
            Tensor weight = training ? _weightMu + _weightSigma * _weightEpsilon : _weightMu;
            Tensor bias = _biasMu is not null ? (training ? _biasMu + _biasSigma * _biasEpsilon : _biasMu) : null;
            return torch.nn.functional.linear(input, weight, bias);
        }
    I wanted to inherit linear but someone made it sealed for some reason but thats another issue. 
yueyinqiu commented 6 months ago

failed to reproduce?

image

NiklasGustafsson commented 3 months ago

@asieradzk The access violation would suggest that the 'handle' field is invalid.