Skip to content

How to use Neptune with argparse#

You can use Neptune to log parameters defined with argparse .

Before you start#

  • Sign up at neptune.ai/register.
  • Create a project for storing your metadata.
  • Have argparse and Neptune installed:

    pip install -U argparse neptune
    
    conda install -c conda-forge argparse neptune
    

Logging parameters with argparse#

  1. Import Neptune and start a run:

    import neptune
    
    run = neptune.init_run() # (1)!
    
    1. If you haven't set up your credentials, you can log anonymously:

      neptune.init_run(
          api_token=neptune.ANONYMOUS_API_TOKEN,
          project="common/quickstarts",
      )
      
  2. Specify and parse the arguments.

    import argparse
    
    argparser = argparse.ArgumentParser()
    argparser.add_argument("--lr", default=0.01)
    argparser.add_argument("--batch", default=32)
    argparser.add_argument("--activation", default="ReLU")
    
    args = argparser.parse_args()
    
  3. Log the parsed arguments to a namespace in the run:

    run["parameters"] = args
    

    Inside the namespace, Neptune creates a field for each argument.

  4. To stop the connection to Neptune and sync all data, call the stop() method:

    run.stop()
    
  5. Run your script as you normally would.

Sample output

[neptune] [info ] Neptune initialized. Open in the app: https://app.neptune.ai/workspace/project/e/RUN-1

In the above example, the run ID is RUN-1.

If Neptune can't find your project name or API token

As a best practice, you should save your Neptune API token and project name as environment variables:

export NEPTUNE_API_TOKEN="h0dHBzOi8aHR0cHM6Lkc78ghs74kl0jv...Yh3Kb8"
export NEPTUNE_PROJECT="ml-team/classification"

Alternatively, you can pass the information when using a function that takes api_token and project as arguments:

run = neptune.init_run( # (1)!
    api_token="h0dHBzOi8aHR0cHM6Lkc78ghs74kl0jv...Yh3Kb8",  # your token here
    project="ml-team/classification",  # your full project name here
)
  1. Also works for init_model(), init_model_version(), init_project(), and integrations that create Neptune runs underneath the hood, such as NeptuneLogger or NeptuneCallback.

  2. API token: In the bottom-left corner, expand the user menu and select Get my API token.

  3. Project name: You can copy the path from the project details ( Edit project details).

If you haven't registered, you can log anonymously to a public project:

api_token=neptune.ANONYMOUS_API_TOKEN
project="common/quickstarts"

Make sure not to publish sensitive data through your code!

Follow the run link and explore the logged parameters in the All metadata dashboard.

Logging with Namespace()#

You can also use Namespace() to define the args:

from argparse import Namespace

args = Namespace(
    lr=0.01,
    batch=32,
    activation="ReLU",
)

args = argparser.parse_args()
run["parameters"] = args