hankcs/HanLP · error · ValueError

Unrecognized mapper type {mapper}

Error message

Unrecognized mapper type {mapper}

What it means

Feedforward normalizes activations into a list of length num_layers (a single activation is broadcast), but an explicitly passed list must match num_layers exactly. A mismatched activations list raises this ValueError in __init__.

Source

Thrown at hanlp/common/transform.py:494

    def convert(y: str):
        if y.startswith('M-'):
            return 'I-'
        return y


class NormalizeToken(ConfigurableNamedTransform):

    def __init__(self, mapper: Union[str, dict], src: str, dst: str = None) -> None:
        super().__init__(src, dst)
        self.mapper = mapper
        if isinstance(mapper, str):
            mapper = get_resource(mapper)
        if isinstance(mapper, str):
            self._table = load_json(mapper)
        elif isinstance(mapper, dict):
            self._table = mapper
        else:
            raise ValueError(f'Unrecognized mapper type {mapper}')

    def __call__(self, sample: dict) -> dict:
        src = sample[self.src]
        if self.src == self.dst:
            sample[f'{self.src}_'] = src
        if isinstance(src, str):
            src = self.convert(src)
        else:
            src = [self.convert(x) for x in src]
        sample[self.dst] = src
        return sample

    def convert(self, token) -> str:
        return self._table.get(token, token)


class PunctuationMask(ConfigurableNamedTransform):
    def __init__(self, src: str, dst: str = None) -> None:

View on GitHub (pinned to ddb1299bdd)

Solutions

  1. Pass a single activation (e.g. activations='relu') to have it broadcast to all layers
  2. Make len(activations) == num_layers
  3. Double-check config files after changing layer counts

Example fix

# before
Feedforward(input_dim=300, num_layers=3, hidden_dims=[128]*3, activations=['relu','tanh'])
# after
Feedforward(input_dim=300, num_layers=3, hidden_dims=[128]*3, activations='relu')
Defensive patterns

Strategy: validation

Validate before calling

assert isinstance(activations, str) or len(activations) == num_layers

Try / catch

try:
    ff = Feedforward(..., activations=activations)
except ValueError:
    activations = 'relu'  # fall back to broadcast scalar
    ff = Feedforward(..., activations=activations)

Prevention

When it happens

Trigger: Passing activations=['relu','tanh'] with num_layers=3, or any list of activation names/objects whose length differs from num_layers.

Common situations: Editing an existing FFN config and changing num_layers without updating activations; mixing scalar and list conventions in YAML/JSON configs.

Understand the failure class

Background: Config validation failed: what "invalid value for {key}" and settings-rejection errors mean across 19 open-source libraries — this error's family across 19 libraries.

Related errors


AI-assisted analysis of hankcs/HanLP@ddb1299bdd (2026-08-27). Data as JSON: /api/errors/58840fa85836144e. Report an issue: GitHub.