Skip to content

Brain hparams can only be accessed with dot notation, not by name #2898

Description

@tomaz-suller

Describe the bug

hparams from load_hyperpyyaml returns a Python dict, yet internally Brain converts it into a SimpleNamespace, which only allows attributes to be accessed via dot notation and no longer from their names.

# Make hyperparams available with dot notation too
if hparams is not None:
self.hparams = SimpleNamespace(**hparams)

When trying to access Brain.hparams by name, you get the following exception: TypeError: 'types.SimpleNamespace' object is not subscriptable

This makes scripting access to hparams -- in particular for parameter transfer applications -- a bit annoying and not at all idiomatic.

Expected behaviour

The described behaviour is unintuitive since I expected to be able to access the parameters in the same way as the hparams I passed originally, that is, by name.

So I would expect

import speechbrain as sb

model = sb.Brain(...)
assert model.hparams["foo"] is model.hparams.foo

To Reproduce

model = EncoderDecoderASR.from_hparams(source="speechbrain/asr-crdnn-rnnlm-librispeech")
model.hparams["asr"]

Environment Details

Python==3.10.16
speechbrain==1.0.3

(other environment details shouldn't be relevant)

Relevant Log Output

Additional Context

An obvious workaround is using getattr (in my example, getattr(model, "asr"). I'm raising the issue mainly as a feature request for native support, but understand if it's not a use case you'd like to support.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions