hankcs/HanLP · error · ValueError
activation must be callable: type={}
Error message
activation must be callable: type={} What it means
The span-ranking SRL layer accepts an `activation` argument that must be callable (a function/nn.Module like torch.sigmoid or F.relu). Passing a string such as 'relu' or 'tanh' (a common convention in other libraries like sklearn/transformers) raises ValueError. None is allowed and means identity.
Source
Thrown at hanlp/components/srl/span_rank/layer.py:119
def forward(self, x):
if self.training:
return torch.mul(x, self.drop_mask.to(x.device))
else: # eval
return x * (1.0 - self.dropout_rate)
class NonLinear(nn.Module):
def __init__(self, input_size, hidden_size, activation=None):
super(NonLinear, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.linear = nn.Linear(in_features=input_size, out_features=hidden_size)
if activation is None:
self._activate = lambda x: x
else:
if not callable(activation):
raise ValueError("activation must be callable: type={}".format(type(activation)))
self._activate = activation
self.reset_parameters()
def forward(self, x):
y = self.linear(x)
return self._activate(y)
def reset_parameters(self):
nn.init.xavier_uniform_(self.linear.weight)
nn.init.zeros_(self.linear.bias)
class Biaffine(nn.Module):
def __init__(self, in1_features, in2_features, out_features,
bias=(True, True)):
super(Biaffine, self).__init__()
self.in1_features = in1_featuresView on GitHub (pinned to ddb1299bdd)
Solutions
- Pass a callable: activation=torch.relu (or F.relu, torch.sigmoid); import torch first
- For identity, pass activation=None
- If config uses strings, map them: {'relu': torch.relu, 'tanh': torch.tanh}.get(cfg)
Example fix
# before layer = Scorer(input_size=100, hidden_size=50, activation='relu') # after import torch layer = Scorer(input_size=100, hidden_size=50, activation=torch.relu)
Defensive patterns
Strategy: type-guard
Validate before calling
import torch
ACT = {'relu': torch.relu, 'tanh': torch.tanh, 'sigmoid': torch.sigmoid, 'identity': None}
activation = ACT[activation] if isinstance(activation, str) else activation
assert activation is None or callable(activation) Type guard
def is_callable_activation(a) -> bool:
return a is None or callable(a) Prevention
- Map config strings to callables at load time
- Add unit tests for layer construction from config
When it happens
Trigger: Constructing the SRL span-rank layer (or a config-driven model build) with activation='relu' or activation="tanh" instead of a callable; also passing a class (torch.nn.ReLU) without instantiating when the code expects an instance/function.
Common situations: Config files where activation is a string; porting hyperparameters from Keras/sklearn-style configs into HanLP training scripts.
Understand the failure class
Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.
Related errors
- Got average f{average}, expected one of None, 'token', or 'b
- invalid number of tags: {num_tags}
- invalid reduction: {reduction}
- alpha must be float, list of float, or torch.FloatTensor, {}
- Only supports floating point dtypes.
AI-assisted analysis of hankcs/HanLP@ddb1299bdd (2026-08-27).
Data as JSON: /api/errors/eb0a0b1829618d8c.
Report an issue: GitHub.