nsml.bind

nsml.bind(save=None, load=None, **kwargs)

It binds functions that are used internally in NSML.

Parameters

Example

def load(dir_name):
    torch.load(os.path.join(dir_name,'model.pth'))
    ...
def save(dir_name, **kwargs):
    if kwargs['real']:
        torch.save(object,os.path.join(dir_name, 'model.pth'))
    ...
# kwargs got a ‘real=True’.
nsml.bind(save=save, load=load, real=True)

nsml.bind-save

nsml.bind() passed to nsml.bind() is a function that stores python objects such as models and optimizers.

User saves one or more files in the input dir_path.

nsml fork , nsml infer , nsml submit commands that use an already created session use objects saved through save function.

param str dir_path

The folder path to the location to save.

An example of saving directly to a file.

Example

def nsml_save(dir_path):
    state = {
    'model': model.state_dict(),
    'optimizer': optimizer.state_dict()
    }
    filename = os.path.join(dir_path, 'model.pth')
nsml.bind(save=nsml_save)

nsml.bind-load

Bind the load function that load the saved model.

load function should receive first argument as saved directory path.

It calls load function on a command that loads a model such as nsml fork , nsml infer , nsml submit.

param str dir_path

The path to the saved folder.

This is an example to load when only one file is saved in bind-save .

Example

def nsml_load(dir_path):
    checkpoint = torch.load(os.path.join(dir_path, 'model.pth'))
    ...

nsml.bind(load=nsml_load)