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

��c�-�@s,ddlZddlZdd�Zdd�Zdd�ZdS)�NcCs0t��}t�|�||�\}}d}tj|dgdgd�}|j�s$|j}|||7<t�|�||�\}	}
||d|8<t�|�||�\}}
|||7<|	|d|}t|||�tdt|�t||��}
|
dk�rt	d|�t	d	t
|��t	d
|||f�dS|��q6t	d�dS)
u� Gradient check for a function f.
    Arguments:
    f -- a function that takes a single argument and outputs the
         loss and its gradients
    x -- the point (numpy array) to check the gradient at
    gradientText -- a string detailing some context about the gradient computation

    Notes:
    Note that gradient checking is a sanity test that only checks whether the
    gradient and loss values produced by your implementation are consistent with
    each other. Gradient check passing on its own doesn’t guarantee that you
    have the correct gradients. It will pass, for example, if both the loss and
    gradient values produced by your implementation are 0s (as is the case when
    you have not implemented anything). Here is a detailed explanation of what
    gradient check is doing if you would like some further clarification:
    http://ufldl.stanford.edu/tutorial/supervised/DebuggingGradientChecking/. 
    g-C��6?�multi_index�	readwrite)�flags�op_flags��g�h㈵��>zGradient check failed for %s.zAFirst gradient error found at index %s in the vector of gradientsz*Your gradient: %f 	 Numerical gradient: %fNz�Gradient check passed!. Read the docstring of the `gradcheck_naive` method in utils.gradcheck.py to understand what the gradient check does.)�random�getstate�setstate�np�nditer�finishedr�abs�max�print�str�iternext)�f�xZgradientTextZrndstateZfx�grad�h�it�ixZfxh�_ZfxnhZnumgradZreldiff�r�A/Users/yimingwang/Desktop/cs224n/assignment/a2/utils/gradcheck.py�gradcheck_naives2


(
�
rc	
Csrtd�|ddgd�||dd�dd�f|dd�dd�f|�\}}}t�|d�sZJd��gd�gd�gd	�gd�gd�g}gd
�gd�gd�gd
�gd�g}t�||�s�Jd��t�||�s�Jd��td�|ddgd�||dd�dd�f|dd�dd�f|�\}}}t�|d��s"Jd��gd�gd�gd�gd�gd�g}gd�gd�gd�gd�gd�g}t�||��s|Jd��t�||��s�Jd��td�|ddgd�||dd�dd�f|dd�dd�f|�\}}}t�|d��s�Jd��gd�gd�gd�gd�gd�g}gd�gd�gd �gd!�gd"�g}t�||��sHJd��t�||��s^Jd��td#�td$�dS)%NzA======Skip-Gram with naiveSoftmaxLossAndGradient Test Cases======�c���a�b�e�dr!r�g�Q2=U&@�'Your loss does not match expected loss.��r'r')gxST�O��g#�iS���g���ڜ@)g���,�Dڿga�Y���?gٞ0�n��?)gQ'f�&s�?g�{ZMpƿg7�˦�U��)g4頦�?g�WQj�w��g�G�6%QϿ)gyن��?�?g"�"hA��gi��)տ)g�^@�u��g�P~{l�?g�Qr��w�?�9Your gradCenterVecs do not match expected gradCenterVecs.�AYour gradOutsideVectors do not match expected gradOutsideVectors.�The first test passed!r!g��ԯ�#@)g1��ū¿g���w��g���x1�ҿ)g�{��Wkӿg�Bx�Z�?gN�g�?)gGQo_jxڿg��I�G�?g��s�?)g�	3+����go!2�a�?ga����?)g� �<�V�?g҄�n�*ݿg��E��ܿ)gR6Yaй?g'C~��ٰ�gL�r|�����The second test passed!r g1�E��%@)gh��K��gN������gߥ�	��?)g�gd y�g��
�Wd��gu��Z��?)g<���m�?gYt�1�?g���j׿)gJ�Y�{�ɿgN���{tr�g<��\C��?)g�sKV���?g�p'��=?g��.^��u�)g���4HۿgJ������g��
�]{�?�The third test passed!�All 3 tests passed!�rr�allclose)	�skipgram�dummy_tokens�
dummy_vectors�dataset�output_loss�output_gradCenterVecs�output_gradOutsideVectors�expected_gradCenterVecs�expected_gradOutsideVectorsrrr�grad_tests_softmax<s�(�������(�������(�������r9c

Csxtd�|ddddg||dd�dd�f|dd�dd�f||�\}}}t�|d�s\Jd��gd	�gd	�gd
�gd	�gd	�g}gd�gd�gd
�gd�gd�g}	t�||�s�Jd��t�||	�s�Jd��td�|ddgd�||dd�dd�f|dd�dd�f||�\}}}t�|d��s&Jd��gd	�gd	�gd�gd	�gd	�g}gd�gd�gd�gd�gd�g}	t�||��s�Jd��t�||	��s�Jd��td�|ddgd�||dd�dd�f|dd�dd�f||�\}}}t�|d��s�Jd��gd �gd	�gd	�gd	�gd	�g}gd!�gd"�gd#�gd$�gd%�g}	t�||��sNJd��t�||	��sdJd��td&�td'�dS)(Nz5======Skip-Gram with negSamplingLossAndGradient======rrr r!r$g\�)��&0@r%r&)gO9�ß/�gՈ
�1���g��=vzr�?)g
��� �g�u-k�N�?g��"O@)g����Ϳg�~�s��?g��
�_�?)g]��a#ݿg����ͽ�?g�	[`Mm�?)gNtȓ�9Կg�.ߏ�?g���K��?)g�md&j��g>��O*��?g�;R;�@r(r)r*r)r r!rr g�(�6P�<@)g`�zuD��g�����O�g~OG��)g*W��t��g:A䙝�?g!��nt@)g���R���g�q�O�>�?g#Ѳ���	@)gB���r	�g�<N,K�?g�-��@)g�JNX�V�ǵ����?g~ad�qy
@)g���g���В�?g�gv@��@r+rrg��SN@)gQ�A���1�g��v�Ez�gx*�����)g�<5M���guy1���¿g�.L
�?)g+�MΠ�gكA���g�� L���?)g��0#�gfU�8-�˿g��Si�@)g��m(�g���d��g�Ӑ#c&�?)g.���Ta#�gbC?lL�˿g���r�;@r,r-r.)
r0r1r2r3�negSamplingLossAndGradientr4r5r6r7r8rrr�grad_tests_negsamp�s� ������� ��������������r;)�numpyrrrr9r;rrrr�<module>s4M