hsigmoid( input, label, num_classes, param_attr=None, bias_attr=None, name=None, path_table=None, path_code=None, is_custom=False, is_sparse=False )
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.
input (Variable) – 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 and float64.
label (Variable) – 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 (
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.
param_attr (ParamAttr, optional) – The parameter attribute for the learnable parameters/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: 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: None.
name (str, optional) – Normally there is no need for user to set this property. For more information, please refer to Name. Default: None.
path_table (Variable, 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. Default: None.
path_code (Variable, 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. Default: None.
is_custom (bool, optional) – Whether use custom binary tree. If it’s True,
num_classesshould be set, otherwise
num_classesshould be set. Default: False.
is_sparse (bool, optional) – Whether use sparse updating instead of dense updating, if it’s True, the gradient of W and input will be sparse. Default: False.
A tensor with the cost of hierarchical sigmoid, its shape is [N, 1] and data type is the same as
- Return type
import paddle.fluid as fluid x = fluid.layers.fill_constant(shape=[4, 3], value=0.9, dtype='float32') # x = [[0.9, 0.9, 0.9], [0.9, 0.9, 0.9], [0.9, 0.9, 0.9], [0.9, 0.9, 0.9]] y = fluid.layers.fill_constant( shape=[4, 1], value=1, dtype='int64') # y = [, , , ] out = fluid.layers.hsigmoid(input=x, label=y, num_classes=2, param_attr=fluid.initializer.Constant( value=0.05), bias_attr=fluid.initializer.Constant(value=.0)) # out = [[0.62792355], [0.62792355], [0.62792355], [0.62792355]]