diff --git a/main.py b/main.py index c0fa98e..5b1e288 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ from source.helper.EvalHelper import EvalHelper from source.helper.FitHelper import FitHelper from source.helper.PredictHelper import PredictHelper +from source.helper.TSNEHelper import TSNEHelper def fit(params): @@ -20,6 +21,10 @@ def eval(params): eval_helper = EvalHelper(params) eval_helper.perform_eval() +def tsne(params): + tsne_obj = TSNEHelper(params) + tsne_obj.perform_tsne() + def z_shot_cls(params): raise NotImplementedError("Not yet implemented.") @@ -37,7 +42,11 @@ def perform_tasks(params): eval(params) if "z-shot-cls" in params.tasks: z_shot_cls(params) + if "tsne" in params.tasks: + tsne(params) + if __name__ == '__main__': perform_tasks() + diff --git a/resource/tsne/BERT_ACM/0.pdf b/resource/tsne/BERT_ACM/0.pdf new file mode 100644 index 0000000..334fa4b Binary files /dev/null and b/resource/tsne/BERT_ACM/0.pdf differ diff --git a/resource/tsne/BERT_ACM/1.pdf b/resource/tsne/BERT_ACM/1.pdf new file mode 100644 index 0000000..cf83c54 Binary files /dev/null and b/resource/tsne/BERT_ACM/1.pdf differ diff --git a/resource/tsne/BERT_ACM/2.pdf b/resource/tsne/BERT_ACM/2.pdf new file mode 100644 index 0000000..50a6918 Binary files /dev/null and b/resource/tsne/BERT_ACM/2.pdf differ diff --git a/resource/tsne/BERT_ACM/3.pdf b/resource/tsne/BERT_ACM/3.pdf new file mode 100644 index 0000000..27d3dbe Binary files /dev/null and b/resource/tsne/BERT_ACM/3.pdf differ diff --git a/resource/tsne/BERT_ACM/4.pdf b/resource/tsne/BERT_ACM/4.pdf new file mode 100644 index 0000000..0340edc Binary files /dev/null and b/resource/tsne/BERT_ACM/4.pdf differ diff --git a/resource/tsne/BERT_ACM/5.pdf b/resource/tsne/BERT_ACM/5.pdf new file mode 100644 index 0000000..178cda5 Binary files /dev/null and b/resource/tsne/BERT_ACM/5.pdf differ diff --git a/resource/tsne/BERT_ACM/6.pdf b/resource/tsne/BERT_ACM/6.pdf new file mode 100644 index 0000000..27973e2 Binary files /dev/null and b/resource/tsne/BERT_ACM/6.pdf differ diff --git a/resource/tsne/BERT_ACM/7.pdf b/resource/tsne/BERT_ACM/7.pdf new file mode 100644 index 0000000..698a410 Binary files /dev/null and b/resource/tsne/BERT_ACM/7.pdf differ diff --git a/resource/tsne/BERT_ACM/8.pdf b/resource/tsne/BERT_ACM/8.pdf new file mode 100644 index 0000000..7744ae3 Binary files /dev/null and b/resource/tsne/BERT_ACM/8.pdf differ diff --git a/resource/tsne/BERT_ACM/9.pdf b/resource/tsne/BERT_ACM/9.pdf new file mode 100644 index 0000000..b3a03b6 Binary files /dev/null and b/resource/tsne/BERT_ACM/9.pdf differ diff --git a/resource/tsne/BERT_AISOPOS/0.pdf b/resource/tsne/BERT_AISOPOS/0.pdf new file mode 100644 index 0000000..da34334 Binary files /dev/null and b/resource/tsne/BERT_AISOPOS/0.pdf differ diff --git a/resource/tsne/BERT_AISOPOS/1.pdf b/resource/tsne/BERT_AISOPOS/1.pdf new file mode 100644 index 0000000..fbf3062 Binary files /dev/null and b/resource/tsne/BERT_AISOPOS/1.pdf differ diff --git a/resource/tsne/BERT_AISOPOS/2.pdf b/resource/tsne/BERT_AISOPOS/2.pdf new file mode 100644 index 0000000..4abbddb Binary files /dev/null and b/resource/tsne/BERT_AISOPOS/2.pdf differ diff --git a/resource/tsne/BERT_AISOPOS/3.pdf b/resource/tsne/BERT_AISOPOS/3.pdf new file mode 100644 index 0000000..29edcfe Binary files /dev/null and b/resource/tsne/BERT_AISOPOS/3.pdf differ diff --git a/resource/tsne/BERT_AISOPOS/4.pdf b/resource/tsne/BERT_AISOPOS/4.pdf new file mode 100644 index 0000000..d86407e Binary files /dev/null and b/resource/tsne/BERT_AISOPOS/4.pdf differ diff --git a/resource/tsne/BERT_AISOPOS/5.pdf b/resource/tsne/BERT_AISOPOS/5.pdf new file mode 100644 index 0000000..0d8404b Binary files /dev/null and b/resource/tsne/BERT_AISOPOS/5.pdf differ diff --git a/resource/tsne/BERT_AISOPOS/6.pdf b/resource/tsne/BERT_AISOPOS/6.pdf new file mode 100644 index 0000000..5df874f Binary files /dev/null and b/resource/tsne/BERT_AISOPOS/6.pdf differ diff --git a/resource/tsne/BERT_AISOPOS/7.pdf b/resource/tsne/BERT_AISOPOS/7.pdf new file mode 100644 index 0000000..87cb528 Binary files /dev/null and b/resource/tsne/BERT_AISOPOS/7.pdf differ diff --git a/resource/tsne/BERT_AISOPOS/8.pdf b/resource/tsne/BERT_AISOPOS/8.pdf new file mode 100644 index 0000000..cb93902 Binary files /dev/null and b/resource/tsne/BERT_AISOPOS/8.pdf differ diff --git a/resource/tsne/BERT_AISOPOS/9.pdf b/resource/tsne/BERT_AISOPOS/9.pdf new file mode 100644 index 0000000..f3798e0 Binary files /dev/null and b/resource/tsne/BERT_AISOPOS/9.pdf differ diff --git a/resource/tsne/BERT_PANGMOVIE/0.pdf b/resource/tsne/BERT_PANGMOVIE/0.pdf new file mode 100644 index 0000000..e8c87bd Binary files /dev/null and b/resource/tsne/BERT_PANGMOVIE/0.pdf differ diff --git a/resource/tsne/BERT_PANGMOVIE/1.pdf b/resource/tsne/BERT_PANGMOVIE/1.pdf new file mode 100644 index 0000000..c952625 Binary files /dev/null and b/resource/tsne/BERT_PANGMOVIE/1.pdf differ diff --git a/resource/tsne/BERT_PANGMOVIE/2.pdf b/resource/tsne/BERT_PANGMOVIE/2.pdf new file mode 100644 index 0000000..d8fba4e Binary files /dev/null and b/resource/tsne/BERT_PANGMOVIE/2.pdf differ diff --git a/resource/tsne/BERT_PANGMOVIE/3.pdf b/resource/tsne/BERT_PANGMOVIE/3.pdf new file mode 100644 index 0000000..0b17276 Binary files /dev/null and b/resource/tsne/BERT_PANGMOVIE/3.pdf differ diff --git a/resource/tsne/BERT_PANGMOVIE/4.pdf b/resource/tsne/BERT_PANGMOVIE/4.pdf new file mode 100644 index 0000000..6ac6023 Binary files /dev/null and b/resource/tsne/BERT_PANGMOVIE/4.pdf differ diff --git a/resource/tsne/BERT_PANGMOVIE/5.pdf b/resource/tsne/BERT_PANGMOVIE/5.pdf new file mode 100644 index 0000000..4b37353 Binary files /dev/null and b/resource/tsne/BERT_PANGMOVIE/5.pdf differ diff --git a/resource/tsne/BERT_PANGMOVIE/6.pdf b/resource/tsne/BERT_PANGMOVIE/6.pdf new file mode 100644 index 0000000..0b63adc Binary files /dev/null and b/resource/tsne/BERT_PANGMOVIE/6.pdf differ diff --git a/resource/tsne/BERT_PANGMOVIE/7.pdf b/resource/tsne/BERT_PANGMOVIE/7.pdf new file mode 100644 index 0000000..2f9feec Binary files /dev/null and b/resource/tsne/BERT_PANGMOVIE/7.pdf differ diff --git a/resource/tsne/BERT_PANGMOVIE/8.pdf b/resource/tsne/BERT_PANGMOVIE/8.pdf new file mode 100644 index 0000000..ae795ac Binary files /dev/null and b/resource/tsne/BERT_PANGMOVIE/8.pdf differ diff --git a/resource/tsne/BERT_PANGMOVIE/9.pdf b/resource/tsne/BERT_PANGMOVIE/9.pdf new file mode 100644 index 0000000..ec478e5 Binary files /dev/null and b/resource/tsne/BERT_PANGMOVIE/9.pdf differ diff --git a/resource/tsne/BERT_WEBKB/0.pdf b/resource/tsne/BERT_WEBKB/0.pdf new file mode 100644 index 0000000..c1ca269 Binary files /dev/null and b/resource/tsne/BERT_WEBKB/0.pdf differ diff --git a/resource/tsne/BERT_WEBKB/1.pdf b/resource/tsne/BERT_WEBKB/1.pdf new file mode 100644 index 0000000..81e311f Binary files /dev/null and b/resource/tsne/BERT_WEBKB/1.pdf differ diff --git a/resource/tsne/BERT_WEBKB/2.pdf b/resource/tsne/BERT_WEBKB/2.pdf new file mode 100644 index 0000000..ec26d67 Binary files /dev/null and b/resource/tsne/BERT_WEBKB/2.pdf differ diff --git a/resource/tsne/BERT_WEBKB/3.pdf b/resource/tsne/BERT_WEBKB/3.pdf new file mode 100644 index 0000000..77eed27 Binary files /dev/null and b/resource/tsne/BERT_WEBKB/3.pdf differ diff --git a/resource/tsne/BERT_WEBKB/4.pdf b/resource/tsne/BERT_WEBKB/4.pdf new file mode 100644 index 0000000..e9aee1c Binary files /dev/null and b/resource/tsne/BERT_WEBKB/4.pdf differ diff --git a/resource/tsne/BERT_WEBKB/5.pdf b/resource/tsne/BERT_WEBKB/5.pdf new file mode 100644 index 0000000..1da74fd Binary files /dev/null and b/resource/tsne/BERT_WEBKB/5.pdf differ diff --git a/resource/tsne/BERT_WEBKB/6.pdf b/resource/tsne/BERT_WEBKB/6.pdf new file mode 100644 index 0000000..a04bfcf Binary files /dev/null and b/resource/tsne/BERT_WEBKB/6.pdf differ diff --git a/resource/tsne/BERT_WEBKB/7.pdf b/resource/tsne/BERT_WEBKB/7.pdf new file mode 100644 index 0000000..1651dfd Binary files /dev/null and b/resource/tsne/BERT_WEBKB/7.pdf differ diff --git a/resource/tsne/BERT_WEBKB/8.pdf b/resource/tsne/BERT_WEBKB/8.pdf new file mode 100644 index 0000000..f6d92d2 Binary files /dev/null and b/resource/tsne/BERT_WEBKB/8.pdf differ diff --git a/resource/tsne/BERT_WEBKB/9.pdf b/resource/tsne/BERT_WEBKB/9.pdf new file mode 100644 index 0000000..e36661c Binary files /dev/null and b/resource/tsne/BERT_WEBKB/9.pdf differ diff --git a/resource/tsne/TSNE-plot-Aisopos.ipynb b/resource/tsne/TSNE-plot-Aisopos.ipynb new file mode 100644 index 0000000..1ba861d --- /dev/null +++ b/resource/tsne/TSNE-plot-Aisopos.ipynb @@ -0,0 +1,412 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "2a71eb98-30f3-4625-b177-bdc1f7e0e7a9", + "metadata": {}, + "outputs": [], + "source": [ + "class PDF(object):\n", + " def __init__(self, pdf, size=(200,200)):\n", + " self.pdf = pdf\n", + " self.size = size\n", + "\n", + " def _repr_html_(self):\n", + " return ''.format(self.pdf, self.size)\n", + "\n", + " def _repr_latex_(self):\n", + " return r'\\includegraphics[width=1.0\\textwidth]{{{0}}}'.format(self.pdf)" + ] + }, + { + "cell_type": "markdown", + "id": "2f6876ca-cb85-4848-a9ab-1079d8782d10", + "metadata": {}, + "source": [ + "# BERT_AISOPOS" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "8cde7623-aed6-4394-8807-d9aaf288fd4e", + "metadata": {}, + "outputs": [], + "source": [ + "dataset=\"BERT_AISOPOS\"" + ] + }, + { + "cell_type": "markdown", + "id": "3e9426e4-680a-4b45-b49e-c0524acd06db", + "metadata": {}, + "source": [ + "## Fold 0" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "7a67af15-f494-458b-9a42-f0fda0a6b93e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_AISOPOS/0.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7ff38c524280>" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/0.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "1fabddae-31f6-4322-95d5-598aa8e5d1ae", + "metadata": {}, + "source": [ + "## Fold 1" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "bce4103c-bf52-4d9e-9ae8-e2d0991a339e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_AISOPOS/1.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7ff38c524940>" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/1.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "05cc5b32-9b80-4fe6-a82d-34e59ecef1fb", + "metadata": {}, + "source": [ + "## Fold 2" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "3480bf11-b43c-4602-bca3-f73823c8984d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_AISOPOS/2.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7ff38c576fa0>" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/2.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "be5d21c4-647f-479b-97c0-d4e15723c766", + "metadata": {}, + "source": [ + "## Fold 3" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "f5e6568e-f5ac-4d6b-a44f-0371421b4dba", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_AISOPOS/3.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7ff38c524b50>" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/3.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "92fda121-5e7e-4372-b03c-1ac9d666ea0d", + "metadata": {}, + "source": [ + "## Fold 4" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "c0a3c56b-1eb0-45fe-b56b-8a28340667b2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_AISOPOS/4.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7ff38c524070>" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/4.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "c95ce2db-c1f4-4022-bf56-e15ebee9eaf8", + "metadata": {}, + "source": [ + "## Fold 5" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "067f27df-b738-4271-ab06-dfdb62aa2c3a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_AISOPOS/5.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7ff38c5240d0>" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/5.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "334c0482-d113-4512-868e-01dfc72e63f0", + "metadata": {}, + "source": [ + "## Fold 6" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "244de334-e150-41c8-b28d-f31e45f266df", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_AISOPOS/6.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7ff38c4c9460>" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/6.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "d70979e2-379b-4a76-91d2-04611b4721f7", + "metadata": {}, + "source": [ + "## Fold 7" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "51a132c6-a48e-464e-bbce-7195cb401655", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_AISOPOS/7.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7ff38c524e80>" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/7.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "0914dbeb-4fbb-4d00-9d36-6cea884a4986", + "metadata": {}, + "source": [ + "## Fold 8" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "b22b8b49-ce0e-4a70-8221-e22010c1ef3c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_AISOPOS/8.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7ff38c4c99a0>" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/8.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "7e4ac68f-078a-4974-86d1-3313a8c521d9", + "metadata": {}, + "source": [ + "## Fold 9" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "bc3ea390-027c-457a-b464-b3903884f476", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_AISOPOS/9.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7ff38c4c9370>" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/9.pdf', (500, 500))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "venv", + "language": "python", + "name": "venv" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/resource/tsne/TSNE-plot-Pang-movie.ipynb b/resource/tsne/TSNE-plot-Pang-movie.ipynb new file mode 100644 index 0000000..aea0631 --- /dev/null +++ b/resource/tsne/TSNE-plot-Pang-movie.ipynb @@ -0,0 +1,412 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "2a71eb98-30f3-4625-b177-bdc1f7e0e7a9", + "metadata": {}, + "outputs": [], + "source": [ + "class PDF(object):\n", + " def __init__(self, pdf, size=(200,200)):\n", + " self.pdf = pdf\n", + " self.size = size\n", + "\n", + " def _repr_html_(self):\n", + " return ''.format(self.pdf, self.size)\n", + "\n", + " def _repr_latex_(self):\n", + " return r'\\includegraphics[width=1.0\\textwidth]{{{0}}}'.format(self.pdf)" + ] + }, + { + "cell_type": "markdown", + "id": "2f6876ca-cb85-4848-a9ab-1079d8782d10", + "metadata": {}, + "source": [ + "# BERT_PANGMOVIE" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "8cde7623-aed6-4394-8807-d9aaf288fd4e", + "metadata": {}, + "outputs": [], + "source": [ + "dataset=\"BERT_PANGMOVIE\"" + ] + }, + { + "cell_type": "markdown", + "id": "3e9426e4-680a-4b45-b49e-c0524acd06db", + "metadata": {}, + "source": [ + "## Fold 0" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "7a67af15-f494-458b-9a42-f0fda0a6b93e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_PANGMOVIE/0.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f1dfc696880>" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/0.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "1fabddae-31f6-4322-95d5-598aa8e5d1ae", + "metadata": {}, + "source": [ + "## Fold 1" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "bce4103c-bf52-4d9e-9ae8-e2d0991a339e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_PANGMOVIE/1.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f1dfc696f40>" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/1.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "05cc5b32-9b80-4fe6-a82d-34e59ecef1fb", + "metadata": {}, + "source": [ + "## Fold 2" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "3480bf11-b43c-4602-bca3-f73823c8984d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_PANGMOVIE/2.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f1dfc696520>" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/2.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "be5d21c4-647f-479b-97c0-d4e15723c766", + "metadata": {}, + "source": [ + "## Fold 3" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "f5e6568e-f5ac-4d6b-a44f-0371421b4dba", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_PANGMOVIE/3.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f1dfc63d190>" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/3.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "92fda121-5e7e-4372-b03c-1ac9d666ea0d", + "metadata": {}, + "source": [ + "## Fold 4" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "c0a3c56b-1eb0-45fe-b56b-8a28340667b2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_PANGMOVIE/4.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f1dfc63d250>" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/4.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "c95ce2db-c1f4-4022-bf56-e15ebee9eaf8", + "metadata": {}, + "source": [ + "## Fold 5" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "067f27df-b738-4271-ab06-dfdb62aa2c3a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_PANGMOVIE/5.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f1dfc63d640>" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/5.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "334c0482-d113-4512-868e-01dfc72e63f0", + "metadata": {}, + "source": [ + "## Fold 6" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "244de334-e150-41c8-b28d-f31e45f266df", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_PANGMOVIE/6.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f1dfc63da60>" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/6.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "d70979e2-379b-4a76-91d2-04611b4721f7", + "metadata": {}, + "source": [ + "## Fold 7" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "51a132c6-a48e-464e-bbce-7195cb401655", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_PANGMOVIE/7.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f1dfc63d550>" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/7.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "0914dbeb-4fbb-4d00-9d36-6cea884a4986", + "metadata": {}, + "source": [ + "## Fold 8" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "b22b8b49-ce0e-4a70-8221-e22010c1ef3c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_PANGMOVIE/8.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f1dfc63dc40>" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/8.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "7e4ac68f-078a-4974-86d1-3313a8c521d9", + "metadata": {}, + "source": [ + "## Fold 9" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "bc3ea390-027c-457a-b464-b3903884f476", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_PANGMOVIE/9.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f1dfc6e0460>" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/9.pdf', (500, 500))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "venv", + "language": "python", + "name": "venv" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/resource/tsne/TSNE-plot-Webkb.ipynb b/resource/tsne/TSNE-plot-Webkb.ipynb new file mode 100644 index 0000000..c251766 --- /dev/null +++ b/resource/tsne/TSNE-plot-Webkb.ipynb @@ -0,0 +1,412 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "2a71eb98-30f3-4625-b177-bdc1f7e0e7a9", + "metadata": {}, + "outputs": [], + "source": [ + "class PDF(object):\n", + " def __init__(self, pdf, size=(200,200)):\n", + " self.pdf = pdf\n", + " self.size = size\n", + "\n", + " def _repr_html_(self):\n", + " return ''.format(self.pdf, self.size)\n", + "\n", + " def _repr_latex_(self):\n", + " return r'\\includegraphics[width=1.0\\textwidth]{{{0}}}'.format(self.pdf)" + ] + }, + { + "cell_type": "markdown", + "id": "2f6876ca-cb85-4848-a9ab-1079d8782d10", + "metadata": {}, + "source": [ + "# BERT_WEBKB" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "8cde7623-aed6-4394-8807-d9aaf288fd4e", + "metadata": {}, + "outputs": [], + "source": [ + "dataset=\"BERT_WEBKB\"" + ] + }, + { + "cell_type": "markdown", + "id": "3e9426e4-680a-4b45-b49e-c0524acd06db", + "metadata": {}, + "source": [ + "## Fold 0" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "7a67af15-f494-458b-9a42-f0fda0a6b93e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_WEBKB/0.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f234c05e370>" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/0.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "1fabddae-31f6-4322-95d5-598aa8e5d1ae", + "metadata": {}, + "source": [ + "## Fold 1" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "bce4103c-bf52-4d9e-9ae8-e2d0991a339e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_WEBKB/1.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f234c0a5f10>" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/1.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "05cc5b32-9b80-4fe6-a82d-34e59ecef1fb", + "metadata": {}, + "source": [ + "## Fold 2" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "3480bf11-b43c-4602-bca3-f73823c8984d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_WEBKB/2.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f234c0a53a0>" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/2.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "be5d21c4-647f-479b-97c0-d4e15723c766", + "metadata": {}, + "source": [ + "## Fold 3" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "f5e6568e-f5ac-4d6b-a44f-0371421b4dba", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_WEBKB/3.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f234c05e5e0>" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/3.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "92fda121-5e7e-4372-b03c-1ac9d666ea0d", + "metadata": {}, + "source": [ + "## Fold 4" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "c0a3c56b-1eb0-45fe-b56b-8a28340667b2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_WEBKB/4.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f234c0a5820>" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/4.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "c95ce2db-c1f4-4022-bf56-e15ebee9eaf8", + "metadata": {}, + "source": [ + "## Fold 5" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "067f27df-b738-4271-ab06-dfdb62aa2c3a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_WEBKB/5.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f234c05e190>" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/5.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "334c0482-d113-4512-868e-01dfc72e63f0", + "metadata": {}, + "source": [ + "## Fold 6" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "244de334-e150-41c8-b28d-f31e45f266df", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_WEBKB/6.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f234c05e790>" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/6.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "d70979e2-379b-4a76-91d2-04611b4721f7", + "metadata": {}, + "source": [ + "## Fold 7" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "51a132c6-a48e-464e-bbce-7195cb401655", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_WEBKB/7.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f234c05e880>" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/7.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "0914dbeb-4fbb-4d00-9d36-6cea884a4986", + "metadata": {}, + "source": [ + "## Fold 8" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "b22b8b49-ce0e-4a70-8221-e22010c1ef3c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_WEBKB/8.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f234c05e940>" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/8.pdf', (500, 500))" + ] + }, + { + "cell_type": "markdown", + "id": "7e4ac68f-078a-4974-86d1-3313a8c521d9", + "metadata": {}, + "source": [ + "## Fold 9" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "bc3ea390-027c-457a-b464-b3903884f476", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/latex": [ + "\\includegraphics[width=1.0\\textwidth]{BERT_WEBKB/9.pdf}" + ], + "text/plain": [ + "<__main__.PDF at 0x7f234c05ed60>" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "PDF(f'{dataset}/9.pdf', (500, 500))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "venv", + "language": "python", + "name": "venv" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/run.sh b/run.sh index 39050e1..4244c3f 100644 --- a/run.sh +++ b/run.sh @@ -4,7 +4,7 @@ export PYTHONPATH=$PATHONPATH:~/projects/TeCBench/ # WEBKB python main.py \ - tasks=[fit,predict,eval] \ + tasks=[tsne] \ model=BERT \ data=WEBKB \ - data.num_workers=12 \ No newline at end of file + data.num_workers=12 diff --git a/settings/data/AISOPOS.yaml b/settings/data/AISOPOS.yaml new file mode 100644 index 0000000..3b36e05 --- /dev/null +++ b/settings/data/AISOPOS.yaml @@ -0,0 +1,12 @@ +name: AISOPOS + +dir: resource/dataset/aisopos_ntua_2L/ + +folds: [0,1,2,3,4,5,6,7,8,9] + +max_length: 256 + +num_classes: 2 + +batch_size: 32 +num_workers: 12 diff --git a/settings/data/PANGMOVIE.yaml b/settings/data/PANGMOVIE.yaml new file mode 100644 index 0000000..6d624d6 --- /dev/null +++ b/settings/data/PANGMOVIE.yaml @@ -0,0 +1,12 @@ +name: PANGMOVIE + +dir: resource/dataset/pang_movie_2L/ + +folds: [0,1,2,3,4,5,6,7,8,9] + +max_length: 256 + +num_classes: 2 + +batch_size: 32 +num_workers: 12 diff --git a/settings/data/VADERMOVIE.yaml b/settings/data/VADERMOVIE.yaml new file mode 100644 index 0000000..37a7bf1 --- /dev/null +++ b/settings/data/VADERMOVIE.yaml @@ -0,0 +1,12 @@ +name: VADERMOVIE + +dir: resource/dataset/vader_movie_2L/ + +folds: [0,1,2,3,4,5,6,7,8,9] + +max_length: 256 + +num_classes: 2 + +batch_size: 32 +num_workers: 12 diff --git a/settings/model/BERT.yaml b/settings/model/BERT.yaml index df00c93..0f03594 100644 --- a/settings/model/BERT.yaml +++ b/settings/model/BERT.yaml @@ -16,6 +16,9 @@ base_lr: 1e-7 max_lr: 1e-3 weight_decay: 1e-2 +n_warmup_steps: 100 +n_training_steps: 1000 + dropout: 0.1 tokenizer: diff --git a/settings/settings.yaml b/settings/settings.yaml index c9c5c1b..5a80d1b 100644 --- a/settings/settings.yaml +++ b/settings/settings.yaml @@ -1,6 +1,6 @@ defaults: - model: BERT - - data: ACM + - data: WEBKB log: dir: resource/log/ @@ -13,15 +13,23 @@ prediction: fold_id: -1 write_interval: batch +tsne: + dir: resource/tsne/${model.name}_${data.name}/ + fold_id: 0 + +pca: + dir: resource/pca/${model.name}_${data.name}/ + fold_id: 0 + stat: dir: resource/stat/ tasks: ["fit","predict", "eval"] trainer: - max_epochs: 16 + max_epochs: 25 gpus: 1 - patience: 7 + patience: 20 min_delta: 0.01 precision: 32 fast_dev_run: False diff --git a/source/helper/PCAHelper.py b/source/helper/PCAHelper.py new file mode 100644 index 0000000..7f3cb7e --- /dev/null +++ b/source/helper/PCAHelper.py @@ -0,0 +1,93 @@ +from pathlib import Path + +import numpy as np + +import pickle + +import torch +from tqdm import tqdm +import seaborn as sns +import matplotlib.pyplot as plt +from sklearn.decomposition import PCA + + +class PCAHelper: + def __init__(self, params): + self.params = params + sns.set_theme(style="darkgrid") + + def _load_ids(self, ids_path): + with open(ids_path, "rb") as ids_file: + ids = pickle.load(ids_file) + + return ids + + return None + + def load_predictions(self, fold_id): + predictions_paths = sorted( + Path(f"{self.params.prediction.dir}fold_{fold_id}/").glob("*.prd") + ) + + train_ids = self._load_ids( + f"{self.params.data.dir}fold_{fold_id}/train.pkl" + ) + + test_ids = self._load_ids( + f"{self.params.data.dir}fold_{fold_id}/test.pkl" + ) + + if test_ids is None: + raise Exception("No test ids") + + trainings = [] + for path in tqdm(predictions_paths, desc="Loading predictions"): + trainings.extend( # only generates tsne over test split + filter(lambda prediction: prediction["idx"] in train_ids, torch.load(path)) + ) + + predictions = [] + for path in tqdm(predictions_paths, desc="Loading predictions"): + predictions.extend( # only generates tsne over test split + filter(lambda prediction: prediction["idx"] in test_ids, torch.load(path)) + ) + + return trainings, predictions + + def pca(self, train_rpr, test_rpr): + pca_obj = PCA(n_components=2, + svd_solver="randomized", + random_state=42).fit(train_rpr) + + return pca_obj.transform(test_rpr) + + def perform_tsne(self): + + for fold_id in self.params.data.folds: + + trainings, predictions = self.load_predictions(fold_id=fold_id) + + train_rprs = [] + for training in trainings: + train_rprs.append(training["rpr"]) + + test_rprs = [] + for prediction in predictions: + test_rprs.append(prediction["rpr"]) + + train_rprs = np.asarray(train_rprs, dtype=np.float64) + test_rprs = np.asarray(test_rprs, dtype=np.float64) + + pca = self.pca(train_rprs, test_rprs) + + sns.scatterplot( + pca[:, 0], + pca[:, 1], + # hue="cls" insert class + ) + Path(self.params.pca.dir).mkdir(parents=True, exist_ok=True) + plt.savefig( + f"{self.params.pca.dir}{fold_id}.pdf", + dpi=300 + ) + diff --git a/source/helper/PcaHelper.py b/source/helper/PcaHelper.py new file mode 100644 index 0000000..7f3cb7e --- /dev/null +++ b/source/helper/PcaHelper.py @@ -0,0 +1,93 @@ +from pathlib import Path + +import numpy as np + +import pickle + +import torch +from tqdm import tqdm +import seaborn as sns +import matplotlib.pyplot as plt +from sklearn.decomposition import PCA + + +class PCAHelper: + def __init__(self, params): + self.params = params + sns.set_theme(style="darkgrid") + + def _load_ids(self, ids_path): + with open(ids_path, "rb") as ids_file: + ids = pickle.load(ids_file) + + return ids + + return None + + def load_predictions(self, fold_id): + predictions_paths = sorted( + Path(f"{self.params.prediction.dir}fold_{fold_id}/").glob("*.prd") + ) + + train_ids = self._load_ids( + f"{self.params.data.dir}fold_{fold_id}/train.pkl" + ) + + test_ids = self._load_ids( + f"{self.params.data.dir}fold_{fold_id}/test.pkl" + ) + + if test_ids is None: + raise Exception("No test ids") + + trainings = [] + for path in tqdm(predictions_paths, desc="Loading predictions"): + trainings.extend( # only generates tsne over test split + filter(lambda prediction: prediction["idx"] in train_ids, torch.load(path)) + ) + + predictions = [] + for path in tqdm(predictions_paths, desc="Loading predictions"): + predictions.extend( # only generates tsne over test split + filter(lambda prediction: prediction["idx"] in test_ids, torch.load(path)) + ) + + return trainings, predictions + + def pca(self, train_rpr, test_rpr): + pca_obj = PCA(n_components=2, + svd_solver="randomized", + random_state=42).fit(train_rpr) + + return pca_obj.transform(test_rpr) + + def perform_tsne(self): + + for fold_id in self.params.data.folds: + + trainings, predictions = self.load_predictions(fold_id=fold_id) + + train_rprs = [] + for training in trainings: + train_rprs.append(training["rpr"]) + + test_rprs = [] + for prediction in predictions: + test_rprs.append(prediction["rpr"]) + + train_rprs = np.asarray(train_rprs, dtype=np.float64) + test_rprs = np.asarray(test_rprs, dtype=np.float64) + + pca = self.pca(train_rprs, test_rprs) + + sns.scatterplot( + pca[:, 0], + pca[:, 1], + # hue="cls" insert class + ) + Path(self.params.pca.dir).mkdir(parents=True, exist_ok=True) + plt.savefig( + f"{self.params.pca.dir}{fold_id}.pdf", + dpi=300 + ) + diff --git a/source/helper/TSNEHelper.py b/source/helper/TSNEHelper.py new file mode 100644 index 0000000..6dcb62c --- /dev/null +++ b/source/helper/TSNEHelper.py @@ -0,0 +1,102 @@ +from pathlib import Path + +import numpy as np + +import pickle + +import torch +from tqdm import tqdm +import seaborn as sns +import matplotlib.pyplot as plt +from openTSNE import TSNE + +class TSNEHelper: + def __init__(self, params): + self.params = params + sns.set_theme(style="darkgrid") + + def _load_ids(self, ids_path): + with open(ids_path, "rb") as ids_file: + ids = pickle.load(ids_file) + + return ids + + return None + + def load_predictions(self, fold_id): + predictions_paths = sorted( + Path(f"{self.params.prediction.dir}fold_{fold_id}/").glob("*.prd") + ) + + train_ids = self._load_ids( + f"{self.params.data.dir}fold_{fold_id}/train.pkl" + ) + + test_ids = self._load_ids( + f"{self.params.data.dir}fold_{fold_id}/test.pkl" + ) + + if test_ids is None: + raise Exception("No test ids") + + trainings = [] + for path in tqdm(predictions_paths, desc="Loading predictions"): + trainings.extend( # only generates tsne over test split + filter(lambda prediction: prediction["idx"] in train_ids, torch.load(path)) + ) + + predictions = [] + for path in tqdm(predictions_paths, desc="Loading predictions"): + predictions.extend( # only generates tsne over test split + filter(lambda prediction: prediction["idx"] in test_ids, torch.load(path)) + ) + + return trainings, predictions + + def tsne(self, train_rpr, test_rpr): + tsne_obj = TSNE( + perplexity=30, + metric="euclidean", + n_jobs=8, + random_state=42, + verbose=True, + ).fit(train_rpr) + return tsne_obj.transform(test_rpr) + + + def perform_tsne(self): + + for fold_id in self.params.data.folds: + + trainings, predictions = self.load_predictions(fold_id=fold_id) + + train_rprs = [] + for training in trainings: + train_rprs.append(training["rpr"]) + + test_rprs = [] + test_labels = [] + for prediction in predictions: + test_rprs.append(prediction["rpr"]) + test_labels.append(prediction["true_cls"]) + + train_rprs = np.asarray(train_rprs, dtype=np.float64) + test_rprs = np.asarray(test_rprs, dtype=np.float64) + + tsne = self.tsne(train_rprs, test_rprs) + + + sns.scatterplot( + tsne[:, 0], + tsne[:, 1], + hue=test_labels + ) + + Path(self.params.tsne.dir).mkdir(parents=True, exist_ok=True) + plt.savefig( + f"{self.params.tsne.dir}{fold_id}.pdf", + dpi=300 + ) + plt.clf() + + diff --git a/source/model/TeCModel.py b/source/model/TeCModel.py index fcbee4d..4bd2697 100644 --- a/source/model/TeCModel.py +++ b/source/model/TeCModel.py @@ -5,7 +5,7 @@ from hydra.utils import instantiate from torch import nn, Tensor from torchmetrics import MetricCollection, F1 -from transformers import get_scheduler +from transformers import get_scheduler, get_linear_schedule_with_warmup class TeCModel(pl.LightningModule): @@ -83,32 +83,24 @@ def predict_step(self, batch, batch_idx, dataloader_idx=None): } def configure_optimizers(self): - # optimizer optimizer = torch.optim.AdamW( - self.parameters(), - lr=self.hparams.lr, - betas=(0.9, 0.999), - eps=1e-08, - weight_decay=self.hparams.weight_decay, - amsgrad=True) - - # scheduler - step_size_up = round(0.03 * self.num_training_steps) - scheduler = get_scheduler( - "linear", - optimizer=optimizer, - num_warmup_steps=round(0.03 * self.num_training_steps), - num_training_steps=self.num_training_steps + self.encoder.parameters(), + lr=self.hparams.lr ) - # scheduler = torch.optim.lr_scheduler.CyclicLR( - # optimizer, - # mode='triangular2', - # base_lr=self.hparams.base_lr, - # max_lr=self.hparams.max_lr, - # step_size_up=step_size_up, - # cycle_momentum=False) - - return {"optimizer": optimizer, "lr_scheduler": scheduler} + scheduler = get_linear_schedule_with_warmup( + optimizer, + num_warmup_steps=self.hparams.n_warmup_steps, + num_training_steps=self.hparams.n_training_steps + ) + + return dict( + optimizer=optimizer, + lr_scheduler=dict( + scheduler=scheduler, + interval='step' + ) + ) + @property def num_training_steps(self) -> int: