Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yiming-wange
GitHub Repository: yiming-wange/cs224n-2023-solution
Path: blob/main/a2/utils/__pycache__/utils.cpython-39.pyc
1003 views
a

��c�@sddlZdd�Zdd�ZdS)�NcCs8|jd}|t�tj|ddd���|df�d}|S)zx Row normalization function

    Implement a function that normalizes each row of a matrix to have
    unit length.
    r����axisg����KH�9)�shape�np�sqrt�sum�reshape)�x�N�r
�=/Users/yimingwang/Desktop/cs224n/assignment/a2/utils/utils.py�
normalizeRowss
*rcCs�|j}t|j�dkrltj|dd�}||�|jddf�8}t�|�}tj|dd�}||�|jddf�}n.t�|�}||8}t�|�}t�|�}||}|j|ks�J�|S)a>Compute the softmax function for each row of the input x.
    It is crucial that this function is optimized for speed because
    it will be used frequently in later code. 

    Arguments:
    x -- A D dimensional vector or N x D dimensional numpy matrix.
    Return:
    x -- You are allowed to modify x in-place
    rrr)r�lenr�maxr
�expr	)r�
orig_shape�tmpr
r
r�softmaxs




r)�numpyrrrr
r
r
r�<module>s