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, 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. 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([2, 3]) # [[-0.8018668 0.8736385 -0.9064771 ] # random # [-0.10228515 -0.87188244 -0.8783718 ]] # random label = paddle.to_tensor([0, 1, 4, 5]) num_classes = 5 weight=paddle.uniform([num_classes-1, 3]) # [[-0.24148715 0.8449961 -0.7399121 ] # random # [-0.9800559 0.43509364 0.9091208 ] # random # [ 0.60194826 0.10430074 -0.4521166 ] # random # [-0.4469818 -0.01536179 -0.604454 ]] # random out=F.hsigmoid_loss(input, label, num_classes, weight) # [[3.0159328] # [2.2407534]]