- class paddle.nn. HSigmoidLoss ( feature_size, num_classes, weight_attr=None, bias_attr=None, is_custom=False, is_sparse=False, name=None )
Hierarchical Sigmoid Layer.
The hierarchical sigmoid organizes the classes into a complete binary tree to reduce the computational complexity and speed up the model training, especially the training of language model. Each leaf node of the complete binary tree represents a class(word) and each non-leaf node acts as a binary classifier. For each class(word), there’s a unique path from root to itself, hsigmoid calculate the cost for each non-leaf node on the path, and sum them to get a total cost. Comparing to softmax, the OP can reduce the computational complexity from \(O(N)\) to \(O(logN)\), where \(N\) represents the number of classes or the size of word dict.
The OP supports default tree and custom tree. For the default tree, you can refer to Hierarchical Probabilistic Neural Network Language Model <http://www.iro.umontreal.ca/~lisa/pointeurs/hierarchical-nnlm-aistats05.pdf>_. For the custom tree, you need to set
is_customto True, and do the following steps (take the language model as an example):
Using a custom word dict to build a binary tree, each leaf node should be an word in the word dict.
Creating a dict map word_id -> path that from the word to the root node, we call it path_table.
Creating a dict map word_id -> code of path that from the word to the root node, we call it path_code. Code means the label of each binary classifier, 1 indicate true, 0 indicate false.
Now, each word should has its path and code along the path, you can pass a batch of path and code related to the same batch of inputs.
feature_size (int) – The number of features.
num_classes (int) – The number of classes or the size of word dict, must be greater than 2. If the default tree is used (
is_customis set to False),
num_classesshould not be None. If the custom tree is used (
is_customis set to True),
num_classesshould be the number of non-leaf nodes, which indicates the num of classes using by the binary classifier.
weight_attr (ParamAttr, optional) – The parameter attribute for the learnable weights of hsigmoid. If it is set to None or one attribute of ParamAttr, hsigmoid will create a ParamAttr as param_attr. If the Initializer of the param_attr is not set, the parameter is initialized with Xavier. Default is None.
bias_attr (ParamAttr|bool, optional) – The parameter attribute for the bias of hsigmoid. If it is set to False, no bias will be added. If it is set to None or one attribute of ParamAttr, hsigmoid will create a ParamAttr as bias_attr. If the Initializer of the bias_attr is not set, the bias is initialized zero. Default is None.
is_custom (bool, optional) – Whether use custom binary tree. If it’s True, path_table and path_code should be passed to its forward method, otherwise path_table and path_code should not be passed to its forward method. Default is False.
is_sparse (bool, optional) – Whether use sparse updating instead of dense updating, if it’s True, the gradient of weight and input will be sparse. Default is False.
name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.
input (Tensor): The input tensor. The shapes is [N, D], where N is batch size and D is feature size. It’s data type should be float32, float64. label (Tensor): It’s shapes is [N, 1]. It’s data type should be int64. output (Tensor): The HSigmoid Loss of
label. Shape is [N, 1]
import paddle paddle.set_device('cpu') input = paddle.uniform([4, 3]) # [[0.56194401 -0.22450298 -0.10741806] # random # [0.36136317 0.23556745 0.88748658] # random # [0.18151939 0.80947340 -0.31078976] # random # [0.68886101 -0.14239830 -0.41297770]] # random label = paddle.to_tensor([0, 1, 4, 5]) m = paddle.nn.HSigmoidLoss(3, 5) out = m(input, label) # [[2.42524505] # [1.74917245] # [3.14571381] # [2.34564662]]
Defines the computation performed at every call. Should be overridden by all subclasses.
*inputs (tuple) – unpacked tuple arguments
**kwargs (dict) – unpacked dict arguments