Serialize learner to string

Hey there,

Is there a way to dump a model/learner to string instead of a file? Similar to how pickle.dumps works. I think it might be possible to save to an io.BytesIO object but not sure how to do it.


1 Like

We use behind the scenes, so it would have to be possible on that level. They use pickle, I think, so maybe it’s doable. If you tell me how to to a string, I can add the option.

Taken from torch serialization (

x = torch.tensor([0, 1, 2, 3, 4])
# Save to io.BytesIO buffer
buffer = io.BytesIO(), buffer)

It seems that accepts a “maybe file” parameter. The thing is that the methods accepts a PathOrStr and relies on it inside.
I can make the change myself and PR it if you give me the guideline on how you’d like to see the interface of the and Learner.export methods (or alternatively if you think it should be a separate method).

It can be the same method. Just add a new argument buffer that defaults to None, and then have it point to path/fname if it’s none. That should do the trick.

You mean you want the signature to be like this?

def save(self, name:PathOrStr, return_path:bool=False, with_opt:bool=True, buffer:io.BytesIO=None)

If that is the case then you will have to, buffer=buffer) to save to a buffer.
How about putting it under the same parameter just like Torch does? That way we could just use (perhaps change the name of the variable to make it more readable). Inside the implementation we can check for the type and work accordingly (also raise an error if return_path=True and using a buffer).

I’m not too fond of replacing fname by a potential buffer and rename it, since it’s what most people use.
Maybe we can make fname default to None so that you can just type Then an assert will throw an error if name and buffer are None.

1 Like

Sounds good, thanks. Sometimes I wish Python supported function overloading :slight_smile: