📄 test_vector_space.cpython-312-pytest-9.1.1.pyc
/home/palash/git/iron_learn/python_scripts/__pycache__/test_vector_space.cpython-312-pytest-9.1.1.pyc
Language: pyc • Lines: 189
�

��j'�
��ddlZddlmcmZddlZddlmZddl	m
Z
ddlmZ
ddlZddlZej"ej$j'�rdnd�Zgd�Zdj+e�j-�Zeee��Zee�D��cic]\}}||��
c}}Zee�ZgZeD]ZZ e j-�Z!e"dee!�dz
�D]3Z#ee!e#dz
ee!e#dzgZ$ee!e#Z%ejMe$e%f��5�\ejNeD�cgc]}|d��	c}ejP��jSe�Z*ejNeD�cgc]}|d��	c}ejP��jSe�Z+Gd�d	ejX�Z-e-ed
��jSe�Z.ej^�Z0e
jbe.je�d�
�Z3d�Z4e
jj�e
jld��\Z7Z8gd�Z9gd�Z:e"d�D�]�Z;e.jy�e3j{�e0e.e*�e+�Z>e>j�e3j��e;dzdk(s�\e8j��ej��5e.j�j�ZEeEeEj�dd��dzzj��j�ZHe4eH�ZIddd�e9D][ZJeJevs�eeJZKeIeKZLeJdvrdneJdvrdndZMe8j�eLdeLdeMddd �!�e8j�eJeLdeLdfd"d#d$d%�&��]e:D]I\ZPZQZMZRePevs�
eQevs�eIeePeIeeQcZSZTe8j�eSdeTdgeSdeTdgeMeRd'd(�)��Ke8j�d*e;�d+e>j��d,���e8j�dd-d.�/�e8j�d0�e
j�d1����e
j��e
j��ycc}}wcc}wcc}w#1swY��KxYw)2�N�cuda�cpu)!zman is malezwoman is femalezking is malezqueen is femalezpalash is maleztotan is femalezboy is malezgirl is femalezthe man is the kingzthe woman is the queenzpalash is the kingztotan is the queenzpalash is a manztotan is a womanzpalash is husbandz
totan is wifezking loves queenzqueen loves kingzpalash loves totanztotan loves palashzprapti is girlztripti is girlzprapti is princessztripti is princesszprapti is daughterztripti is daughterzprapti and tripti are sisterszpalash father praptizpalash father triptiztotan mother praptiztotan mother triptizking father princesszqueen mother princess� �)�dtypec�&��eZdZd�fd�	Zd�Z�xZS)�UltimateEmbeddingModelc���tt|��tj||�|_tj||�|_y)N)�superr	�__init__�nn�	Embedding�	embedding�Linear�fc_out)�self�
vocab_size�d_model�	__class__s   ��?/home/palash/git/iron_learn/python_scripts/test_vector_space.pyrzUltimateEmbeddingModel.__init__<s5���
�$�d�4�6����j�'�:����i�i���4���c��|j|�}||jdd��dzz}|j|jd���S)N�����T��dim�keepdim�:�0�yE>r)r)r�normr�mean)r�x�embeddeds   r�forwardzUltimateEmbeddingModel.forwardAsF���>�>�!�$���x�}�}��T�}�B�T�I�J���{�{�8�=�=�Q�=�/�0�0r)�)�__name__�
__module__�__qualname__rr"�
__classcell__)rs@rr	r	;s
���5�
1rr	r#)rrg{�G�zt?)�lrc�.�|tj|d��z
}tj|j�}tjj|�\}}tj|�ddd�}tj||dd�|ddf�S)Nr)�axisr�)�npr�cov�T�linalg�eigh�argsort�dot)�X�X_meanr-�eigenvalues�eigenvectors�idxs      r�	apply_pcar8Ksw��
������#�
#�F�
�&�&����
�C� "�	�	���s� 3��K��
�*�*�[�
!�$�B�$�
'�C�
�6�6�&�,�q�#�b�q�'�z�2�3�3r)��
)�figsize)
�man�woman�king�queen�palash�totan�prapti�tripti�princess�girl))r<r>�blue�--)r=r?�redrG)r@r>�purple�-)rAr?rIrJ)r@r<rF�:)rAr=rHrK)rBrD�orangerJ)rCrDrLrJ)rErD�magentarG)r@rB�greenrK)rArCrNrKi��drTrr)r<r>r@rF)r=r?rArHrN��k�)�c�s�
edgecolors�zorder)�rWz
offset points��bold)�xytext�
textcoords�fontsize�
fontweightg�?r+)�color�	linestyle�alpha�	linewidthz2Ultimate Family Semantic Space (PCA 4D->2D)
Epoch z	 | Loss: z.4frGg�������?)r_r`g�������?g{�G�z�?)]�builtins�@py_builtins�_pytest.assertion.rewrite�	assertion�rewrite�
@pytest_ar�torch�torch.nnr
�torch.optim�optim�matplotlib.pyplot�pyplot�plt�math�numpyr,�devicer�is_available�corpus�join�split�words�list�set�vocab�	enumerate�
word_to_ix�lenr�data�sentence�tokens�range�i�context�target�append�tensor�long�to�x_train�y_train�Moduler	�model�CrossEntropyLoss�	criterion�Adam�
parameters�	optimizerr8�ion�subplots�fig�ax�target_words�conn_groups�epoch�train�	zero_grad�loss�backward�step�clear�no_gradr�weight�
raw_embedsrr�norm_embeds�
embeddings_2d�wordr7�vecr^�scatter�annotate�w1�w2�style�v1�v2�plot�	set_title�item�grid�margins�pause�ioff�show)r��w�pairs000r�<module>r�sS����������	�����
�
� 7� 7� 9�f�u�	E��
��<	������� ���S��Z���(��/�
0�t�q�!�a��d�
0�
�
��Z�
�
���'�H�
�^�^�
�F�
�1�c�&�k�A�o�
&�'���f�Q�q�S�k�*�J�v�a��c�{�,C�D���F�1�I�&�����W�f�%�&�'�'��%�,�,�D�1�D��Q��1����
D�
G�
G��
O��
�%�,�,�D�1�D��Q��1����
D�
G�
G��
O��
1�R�Y�Y�
1�	�*�a�@�C�C�F�K���B���!�	��E�J�J�u�'�'�)�e�4�	�4�����	�
�#�,�,�x�
(���R�k�����6�]��E�	�K�K�M�
�����U�7�^�W�-�D��M�M�O�
�N�N���s�{�a��
���
�
�U�]�]�_�	3����/�/�J�%����R���)N�QU�)U�V�[�[�]�c�c�e�K�%�k�2�M�	3�
!�	�D��z�!� ��&��#�C�(��"&�*C�"C��RV�Zu�Ru��|C���
�
�3�q�6�3�q�6�U�c�c�RS�
�T����D�3�q�6�3�q�6�"2�6�o�hj�w}��~�
	�%0�	n� �B��E�5��Z��B�*�$4�&�z�"�~�6�
�j�QS�n�8U���B�����A���1����A���1���e�u�\_�kl��m�	n�
	���J�5�'�QZ�[_�[d�[d�[f�gj�Zk�l�m�
�����C��0�
�
�
�3����	�	�$��?�B	����
�����
��q1��2��1�f	3�	3�s�
P1� P7�P<�$AQ�Q