question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I wanted to pickle the LogisticCalibration() class after I had fit it (for later re-use), but I was getting an error related to can't pickle _thread.Rlock objects.

I was able to find a work-around by setting logger in the class no None. Seems a bit hacky, but it did work. Might be something to think about in future releases

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
fabiankuepperscommented, Dec 9, 2022

Hi @VCBE123 thanks for pointing out. Indeed, this is a point I’ve missed to document in the README. You can simply store and reload a model by

lr = LogisticCalibration()

# store on disk
lr.save_model("foo/bar/model.pkl")

# load from disk
lr.load_model("foo/bar/model.pkl")

Hope this helps!

0reactions
VCBE123commented, Dec 16, 2022

Hi @fabiankueppers thank you for your clean demo. I have another question. Since I can save the trained model to a .pkl file, is there any advice for reconstructing the model in C/C++ for development usage?

Finally, I dive into the code and reconstruct the model.

from netcal.scaling import LogisticCalibration
import torch
import numpy as np

lr = LogisticCalibration(detection=True, use_cuda=False)
lr.load_model("lr_5.pkl")

inputs = np.random.randn(10, 5)
out = lr.transform(inputs)


model_weight = torch.load("lr_5.pkl")["_sites"]
inputs = torch.from_numpy(inputs)
weight = torch.from_numpy(model_weight["weights"]['values']).reshape(-1, 1)
bias = torch.from_numpy(model_weight["bias"]['values']).reshape(-1, 1)


def inferse_sigmoid(confidence):
    epsilon = float(torch.finfo(confidence.dtype).eps)
    clipped = torch.clamp(confidence, epsilon, 1. - epsilon)
    inv_clipped = torch.clamp(1. - confidence, epsilon, 1. - epsilon)

    logit = torch.log(clipped) - torch.log(inv_clipped)
    return logit


inputs[:, 0] = inferse_sigmoid(inputs[:, 0])
out_r = torch.sigmoid(torch.mm(inputs, weight) + bias).squeeze_(1).numpy()

assert np.allclose(out, out_r)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Pickling Objects in Python - ThePythonGuru.com
The pickle module is Python-specific which means that once the object is serialized you can't deserialize it using another language like PHP, Java,...
Read more >
The Python pickle Module: How to Persist Objects in Python
The Python pickle module is another way to serialize and deserialize objects in Python. It differs from the json module in that it...
Read more >
Understanding Python Pickling with example - GeeksforGeeks
Python pickle module is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it ......
Read more >
How to Pickle and Unpickle Objects in Python - Stack Abuse
Pickling and unpickling in Python is the process that is used to describe the conversion of objects into byte streams and vice versa ......
Read more >
Python Pickle Module for saving Objects by serialization
Pickling is the serializing and de-serializing of python objects to a byte stream. Unpicking is the opposite. You may hear this methodology called...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found