/usr/local/lib64/python3.6/site-packages/torch/nn/modules/__pycache__
NameSizeModeActions
activation.cpython-36.pyc476350644editdlrm
adaptive.cpython-36.pyc97250644editdlrm
batchnorm.cpython-36.pyc310390644editdlrm
channelshuffle.cpython-36.pyc19010644editdlrm
container.cpython-36.pyc279700644editdlrm
conv.cpython-36.pyc577890644editdlrm
distance.cpython-36.pyc36840644editdlrm
dropout.cpython-36.pyc103100644editdlrm
flatten.cpython-36.pyc58130644editdlrm
fold.cpython-36.pyc128070644editdlrm
instancenorm.cpython-36.pyc188080644editdlrm
lazy.cpython-36.pyc116900644editdlrm
linear.cpython-36.pyc101880644editdlrm
loss.cpython-36.pyc914650644editdlrm
module.cpython-36.pyc665300644editdlrm
normalization.cpython-36.pyc113600644editdlrm
padding.cpython-36.pyc222170644editdlrm
pixelshuffle.cpython-36.pyc43980644editdlrm
pooling.cpython-36.pyc532030644editdlrm
rnn.cpython-36.pyc446530644editdlrm
sparse.cpython-36.pyc209560644editdlrm
transformer.cpython-36.pyc207370644editdlrm
upsampling.cpython-36.pyc107800644editdlrm
utils.cpython-36.pyc25280644editdlrm
_functions.cpython-36.pyc54410644editdlrm
__init__.cpython-36.pyc52360644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/torch/nn/modules/__pycache__/adaptive.cpython-36.pyc (9725B)
3 Eg+@s~ddlmZddlZddlmZddlmZmZddlmZm Z m Z ddl m Z dd l mZed d d gZGd dde ZdS)) namedtupleN)Tensor)ListSequence) Sequential ModuleListLinear)Module) log_softmax _ASMoutputoutputlosscseZdZUdZeeeee  e  e edeeeee e ddfdd Zddd d Zeeed d d ZddZeedddZeedddZZS)AdaptiveLogSoftmaxWithLossuEfficient softmax approximation as described in `Efficient softmax approximation for GPUs by Edouard Grave, Armand Joulin, Moustapha Cissé, David Grangier, and Hervé Jégou `__. Adaptive softmax is an approximate strategy for training models with large output spaces. It is most effective when the label distribution is highly imbalanced, for example in natural language modelling, where the word frequency distribution approximately follows the `Zipf's law`_. Adaptive softmax partitions the labels into several clusters, according to their frequency. These clusters may contain different number of targets each. Additionally, clusters containing less frequent labels assign lower dimensional embeddings to those labels, which speeds up the computation. For each minibatch, only clusters for which at least one target is present are evaluated. The idea is that the clusters which are accessed frequently (like the first one, containing most frequent labels), should also be cheap to compute -- that is, contain a small number of assigned labels. We highly recommend taking a look at the original paper for more details. * :attr:`cutoffs` should be an ordered Sequence of integers sorted in the increasing order. It controls number of clusters and the partitioning of targets into clusters. For example setting ``cutoffs = [10, 100, 1000]`` means that first `10` targets will be assigned to the 'head' of the adaptive softmax, targets `11, 12, ..., 100` will be assigned to the first cluster, and targets `101, 102, ..., 1000` will be assigned to the second cluster, while targets `1001, 1002, ..., n_classes - 1` will be assigned to the last, third cluster. * :attr:`div_value` is used to compute the size of each additional cluster, which is given as :math:`\left\lfloor\frac{\texttt{in\_features}}{\texttt{div\_value}^{idx}}\right\rfloor`, where :math:`idx` is the cluster index (with clusters for less frequent words having larger indices, and indices starting from :math:`1`). * :attr:`head_bias` if set to True, adds a bias term to the 'head' of the adaptive softmax. See paper for details. Set to False in the official implementation. .. warning:: Labels passed as inputs to this module should be sorted according to their frequency. This means that the most frequent label should be represented by the index `0`, and the least frequent label should be represented by the index `n_classes - 1`. .. note:: This module returns a ``NamedTuple`` with ``output`` and ``loss`` fields. See further documentation for details. .. note:: To compute log-probabilities for all classes, the ``log_prob`` method can be used. Args: in_features (int): Number of features in the input tensor n_classes (int): Number of classes in the dataset cutoffs (Sequence): Cutoffs used to assign targets to their buckets div_value (float, optional): value used as an exponent to compute sizes of the clusters. Default: 4.0 head_bias (bool, optional): If ``True``, adds a bias term to the 'head' of the adaptive softmax. Default: ``False`` Returns: ``NamedTuple`` with ``output`` and ``loss`` fields: * **output** is a Tensor of size ``N`` containing computed target log probabilities for each example * **loss** is a Scalar representing the computed negative log likelihood loss Shape: - input: :math:`(N, \texttt{in\_features})` - target: :math:`(N)` where each value satisfies :math:`0 <= \texttt{target[i]} <= \texttt{n\_classes}` - output1: :math:`(N)` - output2: ``Scalar`` .. _Zipf's law: https://en.wikipedia.org/wiki/Zipf%27s_law @FN) in_features n_classescutoffs div_value head_biasreturnc sn||d}tt|jt|}|t|ksnt|dksnt||dksntt|t|ksnt dd|Drvt d||_ ||_ ||g|_ ||_||_|j d|_t|j d|_|j|j|_t|j |jfd|ji||_t|_x~t|jD]p} t|j |j| d} |j | d|j | } tt|j | fddi|t| | fddi|} |jj| qWdS) N)devicedtyperrcSsg|]}t||kqS)int).0crrE/usr/local/lib64/python3.6/site-packages/torch/nn/modules/adaptive.py sz7AdaptiveLogSoftmaxWithLoss.__init__..zcutoffs should be a sequence of unique, positive integers sorted in an increasing order, where each value is between 1 and n_classes-1ZbiasF)superr__init__listsortedminmaxlensetany ValueErrorrrrrrshortlist_sizeZ n_clustersZ head_sizer headrtailrangerrappend) selfrrrrrrrZfactory_kwargsiZhszZoszZ projection) __class__rrr!ps6      z#AdaptiveLogSoftmaxWithLoss.__init__)rcCs2|jjx"|jD]\}}|j|jqWdS)N)r+reset_parametersr,)r/Zi2hZh2orrrr2s z+AdaptiveLogSoftmaxWithLoss.reset_parameters)inputtargetrcCs|jd|jdkrtdd}|jd}|j|}|j|}dg|j}xtt|dD]}||} ||d} || k|| k@} | jj} | j dkrq\|dkr|j d| || nx|| | } |j d| }|j |d|}|j |d}|jd| |t|dd}|jd| jd}|j d| |jd|| j 7}q\W||krttdj|jd|jj|jj|j|}t|dd}||jd|jdj7}| j}t||S)NrzBInput and target should have the same size in the batch dimension.r)dimzMTarget values should be in [0, {}], but values in range [{}, {}] were found. )size RuntimeErrorZ new_zeros new_emptyrr-r&ZnonzeroZsqueezeZnumelZ index_copy_Z index_selectr,r*Z index_fill_r gather unsqueezeformatrr$itemr%r+Zmeanr )r/r3r4Z used_rowsZ batch_sizerZ gather_indsZ cutoff_valuesr0Zlow_idxZhigh_idxZ target_maskZ row_indicesZrelative_targetZ input_subsetcluster_outputZ cluster_indexcluster_logprobZ local_logprob head_output head_logprobrrrrforwardsF               z"AdaptiveLogSoftmaxWithLoss.forwardc Cs|j|jd|jf}t|dd}|ddd|jf|ddd|jf<xxtt|j|jddD]Z\}\}}|j||}t|dd} | |dd|j|fj d} | |dd||f<qfW|S)za Given input tensor, and output of `self.head`, compute the log of the full distribution rr)r5N) r8r6rr r* enumerateziprr,r:) r/r3r?outr@r0Z start_idxZstop_idxr=r>Zoutput_logprobrrr_get_full_log_probs ((  z-AdaptiveLogSoftmaxWithLoss._get_full_log_prob)r3rcCs|j|}|j||S)a Computes log probabilities for all :math:`\texttt{n\_classes}` Args: input (Tensor): a minibatch of examples Returns: log-probabilities of for each class :math:`c` in range :math:`0 <= c <= \texttt{n\_classes}`, where :math:`\texttt{n\_classes}` is a parameter passed to ``AdaptiveLogSoftmaxWithLoss`` constructor. Shape: - Input: :math:`(N, \texttt{in\_features})` - Output: :math:`(N, \texttt{n\_classes})` )r+rE)r/r3r?rrrlog_probs z#AdaptiveLogSoftmaxWithLoss.log_probcCs|j|}tj|dd}||jk}|j }|r4|S|jrV|j||}tj|ddS|j||||}tj|dd||<|SdS)a This is equivalent to `self.log_pob(input).argmax(dim=1)`, but is more efficient in some cases. Args: input (Tensor): a minibatch of examples Returns: output (Tensor): a class with the highest probability for each example Shape: - Input: :math:`(N, \texttt{in\_features})` - Output: :math:`(N)` r)r5N)r+torchZargmaxr*r(allrE)r/r3r?rZnot_in_shortlistZall_in_shortlistrFrrrpredicts      z"AdaptiveLogSoftmaxWithLoss.predict)rFNN)__name__ __module__ __qualname____doc__rrrrrfloatrboolrr r+rr,rr!r2rr rArErFrI __classcell__rr)r1rrs$ T*7r) collectionsrrGrtypingrrrrr moduler Z functionalr r rrrrrs