- paddle.nn.functional. hsigmoid_loss ( input, label, num_classes, weight, bias=None, path_table=None, path_code=None, is_sparse=False, name=None )
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, hsigmoid 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 API supports default tree and custom tree. For the default tree, you can refer to Hierarchical Probabilistic Neural Network Language Model.
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.
input (Tensor) – A tensor with the shape [N, D], where N is the size of mini-batch, and D is the feature size. Its data type supports float32 or float64.
label (Tensor) – A tensor contains the labels of training data. Its shape is [N, 1] and data type is int64.
num_classes (int) – The number of classes or the size of word dict, must be greater than 2. If the default tree is used (path_code and path_table is None are None), num_classes should not be None. If the custom tree is used (path_code and path_table is None are not None), num_classes should be the number of non-leaf nodes, which indicates the num of classes using by the binary classifier.
weight (Tensor) – A tensor with shape (num_classes - 1, D), with the same data type as input.
bias (Tensor, optional) – A tensor with shape (num_classes - 1, 1), with the same data type as input. If bias is None, no bias will be add. Default is None.
path_table (Tensor, optional) – A tensor that stores each batch of samples’ path from leaf to root node, its shape is [N, L] and data type is int64, where L is the length of path. For each sample i, path_table[i] is a np.array like structure and each element in this array is the indexes in parent nodes’ weight matrix. If path_table and path_code are None, the default tree will be used. Default is None.
path_code (Tensor, optional) – A tensor that stores each batch of samples’ code of path from leaf to root node, its shape is [N, L] and data type is int64, which is the same as
path_table. Each code of path is consisted with the code of nodes from leaf to root node. If path_table and path_code are None, the default tree will be used. Default is None.
is_sparse (bool, optional) – Whether use sparse updating instead of dense updating. If is_sparse is 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.
A tensor with the cost of hierarchical sigmoid, its shape is [N, 1] and data type is the same as input.
import paddle import paddle.nn.functional as F paddle.set_device('cpu') input = paddle.uniform([4, 3]) # [[0.45424712 -0.77296764 0.82943869] # random # [0.85062802 0.63303483 0.35312140] # random # [0.57170701 0.16627562 0.21588242] # random # [0.27610803 -0.99303514 -0.17114788]] # random label = paddle.to_tensor([0, 1, 4, 5]) num_classes = 5 weight=paddle.uniform([num_classes-1, 3]) # [[-0.64477652 0.24821866 -0.17456549] # random # [-0.04635394 0.07473493 -0.25081766] # random # [ 0.05986035 -0.12185556 0.45153677] # random # [-0.66236806 0.91271877 -0.88088769]] # random out=F.hsigmoid_loss(input, label, num_classes, weight) # [[1.96709502] # [2.40019274] # [2.11009121] # [1.92374969]]