# hsigmoid¶

api_attr

declarative programming (static graph)

`paddle.fluid.layers.``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)[source]

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_custom` to True, and do the following steps (take the language model as an example):

1. Using a custom word dict to build a binary tree, each leaf node should be an word in the word dict.

2. Creating a dict map word_id -> path that from the word to the root node, we call it path_table.

3. 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.

4. 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.

Parameters
• 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_custom` is set to False), `num_classes` should not be None. If the custom tree is used (`is_custom` is set to True), `num_classes` should 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, `path_table`, `path_code` and `num_classes` should be set, otherwise `num_classes` should 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.

Returns

A tensor with the cost of hierarchical sigmoid, its shape is [N, 1] and data type is the same as `input`.

Return type

Variable

Examples

```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]]
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.7766397,"math_prob":0.92507434,"size":4759,"snap":"2020-34-2020-40","text_gpt3_token_len":1222,"char_repetition_ratio":0.104942165,"word_repetition_ratio":0.1594203,"special_character_ratio":0.263921,"punctuation_ratio":0.17652959,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9842496,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-26T12:24:48Z\",\"WARC-Record-ID\":\"<urn:uuid:81563afd-9aba-441c-840a-dfba51eedd42>\",\"Content-Length\":\"98651\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:11ac1a6b-3d49-4799-9b4f-98ac94163402>\",\"WARC-Concurrent-To\":\"<urn:uuid:cc5ecc77-b78c-4b0b-96a9-4b7d280c826b>\",\"WARC-IP-Address\":\"180.76.58.84\",\"WARC-Target-URI\":\"https://www.paddlepaddle.org.cn/documentation/docs/en/api/layers/hsigmoid.html\",\"WARC-Payload-Digest\":\"sha1:2RMJTF72USG5JOB4LJPGVBZQGKGPC657\",\"WARC-Block-Digest\":\"sha1:AICBO36XAD3R2GK7A3S2TFVGYE7GHT6E\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400241093.64_warc_CC-MAIN-20200926102645-20200926132645-00393.warc.gz\"}"}