{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# GPT2 LM" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "This tutorial is available as an IPython notebook at [Malaya/example/gpt2-lm](https://github.com/huseinzol05/Malaya/tree/master/example/gpt2-lm).\n", " \n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "os.environ['CUDA_VISIBLE_DEVICES'] = ''" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/husein/dev/malaya/malaya/tokenizer.py:202: FutureWarning: Possible nested set at position 3361\n", " self.tok = re.compile(r'({})'.format('|'.join(pipeline)))\n", "/home/husein/dev/malaya/malaya/tokenizer.py:202: FutureWarning: Possible nested set at position 3879\n", " self.tok = re.compile(r'({})'.format('|'.join(pipeline)))\n" ] } ], "source": [ "import malaya" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dependency\n", "\n", "Make sure you already installed,\n", "\n", "```bash\n", "pip3 install transformers\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List available GPT2 models" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Size (MB)
mesolitica/gpt2-117m-bahasa-cased454
\n", "
" ], "text/plain": [ " Size (MB)\n", "mesolitica/gpt2-117m-bahasa-cased 454" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "malaya.language_model.available_gpt2()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load GPT2 LM model\n", "\n", "```python\n", "def gpt2(model: str = 'mesolitica/gpt2-117m-bahasa-cased', force_check: bool = True, **kwargs):\n", " \"\"\"\n", " Load GPT2 language model.\n", "\n", " Parameters\n", " ----------\n", " model: str, optional (default='mesolitica/gpt2-117m-bahasa-cased')\n", " Check available models at `malaya.language_model.available_gpt2()`.\n", " force_check: bool, optional (default=True)\n", " Force check model one of malaya model.\n", " Set to False if you have your own huggingface model.\n", "\n", " Returns\n", " -------\n", " result: malaya.torch_model.gpt2_lm.LM class\n", " \"\"\"\n", "```\n", "\n", "If you have other models from huggingface and want to load it on `malaya.torch_model.gpt2_lm.LM`, set `force_check=False`." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "model = malaya.language_model.gpt2()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-51.384037494659424" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.score('saya suke awak')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-46.20505475997925" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.score('saya suka awak')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-48.355817794799805" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.score('najib razak')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-52.79337692260742" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.score('najib comel')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }