├── data └── imgs │ ├── Slide2.JPG │ └── dfhead.png ├── LICENSE ├── README.md └── Sentiment_Analysis_torchtext.ipynb /data/imgs/Slide2.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpanwar08/sentiment-analysis-torchtext/HEAD/data/imgs/Slide2.JPG -------------------------------------------------------------------------------- /data/imgs/dfhead.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hpanwar08/sentiment-analysis-torchtext/HEAD/data/imgs/dfhead.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Himanshu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sentiment Analysis in Torch Text 2 | Sentiment analysis is a classification task where each sample is assigned a positive or negative label. 3 | 4 | This repo contains the code for the this [blog](https://medium.com/@sonicboom8/sentiment-analysis-torchtext-55fb57b1fab8). 5 | 6 | ## Typical components of classification task in NLP 7 | 1. Preprocessing and tokenization 8 | 2. Generating vocabulary of unique tokens and converting words to indices 9 | 3. Loading pretrained vectors e.g. Glove, Word2vec, Fasttext 10 | 4. Padding text with zeros in case of variable lengths 11 | 5. Dataloading and batching 12 | 6. Model creation and training 13 | 14 | ## Why use torchtext 15 | Torchtext provide set of classes that are useful in NLP tasks. These classes takes care of first 5 points above with very minimal code. 16 | 17 | ## Prerequisites 18 | * Python 3.6 19 | * [Pytorch 0.4](http://pytorch.org/) 20 | * [TorchText 0.2.3](https://github.com/pytorch/text) 21 | * Understanding of GRU/LSTM [1] 22 | 23 | ## What is covered in the [notebook](Sentiment%20analysis%20pytorch.ipynb) 24 | 25 | 1. Train validation split 26 | 2. Define how to process data 27 | 3. Create torchtext dataset 28 | 4. Load pretrained word vectors and building vocabulary 29 | 5. Loading the data in batches 30 | 6. Simple GRU model 31 | 6. GRU model with concat pooling 32 | 7. Training 33 | 34 | 35 | ## Data Overview 36 | 37 |  38 | 39 | ## Concat Pooling model architecture [2] 40 |  41 | 42 | ## References 43 | [1] https://colah.github.io/posts/2015-08-Understanding-LSTMs/ 44 | [2] https://arxiv.org/abs/1801.06146 45 | -------------------------------------------------------------------------------- /Sentiment_Analysis_torchtext.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Sentiment Analysis in torchtext" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Imports" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "%matplotlib inline\n", 24 | "import os, sys\n", 25 | "import re\n", 26 | "import string\n", 27 | "import pathlib\n", 28 | "import random\n", 29 | "from collections import Counter, OrderedDict\n", 30 | "import numpy as np\n", 31 | "import pandas as pd\n", 32 | "import matplotlib.pyplot as plt\n", 33 | "import seaborn as sns\n", 34 | "import spacy\n", 35 | "from tqdm import tqdm, tqdm_notebook, tnrange\n", 36 | "tqdm.pandas(desc='Progress')\n", 37 | "\n", 38 | "import torch\n", 39 | "import torch.nn as nn\n", 40 | "import torch.optim as optim\n", 41 | "from torch.autograd import Variable\n", 42 | "import torch.nn.functional as F\n", 43 | "from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence\n", 44 | "\n", 45 | "import torchtext\n", 46 | "from torchtext import data\n", 47 | "from torchtext import vocab\n", 48 | "\n", 49 | "from sklearn.model_selection import StratifiedShuffleSplit, train_test_split\n", 50 | "from sklearn.metrics import accuracy_score\n", 51 | "\n", 52 | "from IPython.core.interactiveshell import InteractiveShell\n", 53 | "InteractiveShell.ast_node_interactivity='all'\n", 54 | "\n", 55 | "import warnings\n", 56 | "warnings.filterwarnings('ignore')\n", 57 | "\n", 58 | "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 48, 64 | "metadata": {}, 65 | "outputs": [ 66 | { 67 | "name": "stdout", 68 | "output_type": "stream", 69 | "text": [ 70 | "Python version: 3.6.4 | packaged by conda-forge | (default, Dec 23 2017, 16:31:06) \n", 71 | "[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)]\n", 72 | "Pandas version: 0.22.0\n", 73 | "Pytorch version: 0.4.0\n", 74 | "Torch Text version: 0.2.3\n", 75 | "Spacy version: 2.0.8\n" 76 | ] 77 | } 78 | ], 79 | "source": [ 80 | "print('Python version:',sys.version)\n", 81 | "print('Pandas version:',pd.__version__)\n", 82 | "print('Pytorch version:', torch.__version__)\n", 83 | "print('Torch Text version:', torchtext.__version__)\n", 84 | "print('Spacy version:', spacy.__version__)" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "### Load data" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 2, 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [ 100 | "data_root = pathlib.Path('./data')" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 3, 106 | "metadata": {}, 107 | "outputs": [ 108 | { 109 | "name": "stderr", 110 | "output_type": "stream", 111 | "text": [ 112 | "b'Skipping line 8836: expected 4 fields, saw 5\\n'\n", 113 | "b'Skipping line 535882: expected 4 fields, saw 7\\n'\n" 114 | ] 115 | }, 116 | { 117 | "data": { 118 | "text/plain": [ 119 | "(1578612, 4)" 120 | ] 121 | }, 122 | "execution_count": 3, 123 | "metadata": {}, 124 | "output_type": "execute_result" 125 | }, 126 | { 127 | "data": { 128 | "text/html": [ 129 | "
| \n", 147 | " | ItemID | \n", 148 | "Sentiment | \n", 149 | "SentimentSource | \n", 150 | "SentimentText | \n", 151 | "
|---|---|---|---|---|
| 0 | \n", 156 | "1 | \n", 157 | "0 | \n", 158 | "Sentiment140 | \n", 159 | "is so sad for my APL frie... | \n", 160 | "
| 1 | \n", 163 | "2 | \n", 164 | "0 | \n", 165 | "Sentiment140 | \n", 166 | "I missed the New Moon trail... | \n", 167 | "
| 2 | \n", 170 | "3 | \n", 171 | "1 | \n", 172 | "Sentiment140 | \n", 173 | "omg its already 7:30 :O | \n", 174 | "
| 3 | \n", 177 | "4 | \n", 178 | "0 | \n", 179 | "Sentiment140 | \n", 180 | ".. Omgaga. Im sooo im gunna CRy. I'... | \n", 181 | "
| 4 | \n", 184 | "5 | \n", 185 | "0 | \n", 186 | "Sentiment140 | \n", 187 | "i think mi bf is cheating on me!!! ... | \n", 188 | "
| \n", 329 | " | ItemID | \n", 330 | "Sentiment | \n", 331 | "SentimentSource | \n", 332 | "SentimentText | \n", 333 | "
|---|---|---|---|---|
| 0 | \n", 338 | "363919 | \n", 339 | "1 | \n", 340 | "Sentiment140 | \n", 341 | "@p3cia hihi.. already looked | \n", 342 | "
| 1 | \n", 345 | "1002689 | \n", 346 | "1 | \n", 347 | "Sentiment140 | \n", 348 | "@lizzylou62 Good luck with the exams! | \n", 349 | "
| 2 | \n", 352 | "1257543 | \n", 353 | "0 | \n", 354 | "Sentiment140 | \n", 355 | "The krispy kreme in CT is so closed | \n", 356 | "
| 3 | \n", 359 | "495896 | \n", 360 | "1 | \n", 361 | "Sentiment140 | \n", 362 | "@TomJ93 because of what @_nanu_ said | \n", 363 | "
| 4 | \n", 366 | "445470 | \n", 367 | "0 | \n", 368 | "Sentiment140 | \n", 369 | "@TellYaFriday I have nothing else to do...i'm... | \n", 370 | "
| \n", 450 | " | ItemID | \n", 451 | "Sentiment | \n", 452 | "SentimentSource | \n", 453 | "SentimentText | \n", 454 | "
|---|---|---|---|---|
| 0 | \n", 459 | "1432717 | \n", 460 | "1 | \n", 461 | "Sentiment140 | \n", 462 | "http://www.popsugar.com/2999655 keep voting fo... | \n", 463 | "
| 1 | \n", 466 | "815480 | \n", 467 | "1 | \n", 468 | "Sentiment140 | \n", 469 | "I follow @actionchick because she always has ... | \n", 470 | "
| 2 | \n", 473 | "1143701 | \n", 474 | "1 | \n", 475 | "Sentiment140 | \n", 476 | "Slow This Dance Now | \n", 477 | "
| 3 | \n", 480 | "1044045 | \n", 481 | "0 | \n", 482 | "Sentiment140 | \n", 483 | "no win on the ipod for tonight | \n", 484 | "
| 4 | \n", 487 | "979854 | \n", 488 | "0 | \n", 489 | "Sentiment140 | \n", 490 | "@LegendaryWriter tell me about it | \n", 491 | "
Failed to display Jupyter Widget of type HBox.
\n", 1506 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 1507 | " that the widgets JavaScript is still loading. If this message persists, it\n", 1508 | " likely means that the widgets JavaScript library is either not installed or\n", 1509 | " not enabled. See the Jupyter\n", 1510 | " Widgets Documentation for setup instructions.\n", 1511 | "
\n", 1512 | "\n", 1513 | " If you're reading this message in another frontend (for example, a static\n", 1514 | " rendering on GitHub or NBViewer),\n", 1515 | " it may mean that your frontend doesn't currently support widgets.\n", 1516 | "
\n" 1517 | ], 1518 | "text/plain": [ 1519 | "HBox(children=(IntProgress(value=0, max=5), HTML(value='')))" 1520 | ] 1521 | }, 1522 | "metadata": {}, 1523 | "output_type": "display_data" 1524 | }, 1525 | { 1526 | "data": { 1527 | "application/vnd.jupyter.widget-view+json": { 1528 | "model_id": "cb08b1c6c1604ee5b6e1f3737748b129", 1529 | "version_major": 2, 1530 | "version_minor": 0 1531 | }, 1532 | "text/html": [ 1533 | "Failed to display Jupyter Widget of type HBox.
\n", 1535 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 1536 | " that the widgets JavaScript is still loading. If this message persists, it\n", 1537 | " likely means that the widgets JavaScript library is either not installed or\n", 1538 | " not enabled. See the Jupyter\n", 1539 | " Widgets Documentation for setup instructions.\n", 1540 | "
\n", 1541 | "\n", 1542 | " If you're reading this message in another frontend (for example, a static\n", 1543 | " rendering on GitHub or NBViewer),\n", 1544 | " it may mean that your frontend doesn't currently support widgets.\n", 1545 | "
\n" 1546 | ], 1547 | "text/plain": [ 1548 | "HBox(children=(IntProgress(value=0, max=2467), HTML(value='')))" 1549 | ] 1550 | }, 1551 | "metadata": {}, 1552 | "output_type": "display_data" 1553 | }, 1554 | { 1555 | "data": { 1556 | "application/vnd.jupyter.widget-view+json": { 1557 | "model_id": "5e2c250c21d94c14b6390f9510269423", 1558 | "version_major": 2, 1559 | "version_minor": 0 1560 | }, 1561 | "text/html": [ 1562 | "Failed to display Jupyter Widget of type HBox.
\n", 1564 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 1565 | " that the widgets JavaScript is still loading. If this message persists, it\n", 1566 | " likely means that the widgets JavaScript library is either not installed or\n", 1567 | " not enabled. See the Jupyter\n", 1568 | " Widgets Documentation for setup instructions.\n", 1569 | "
\n", 1570 | "\n", 1571 | " If you're reading this message in another frontend (for example, a static\n", 1572 | " rendering on GitHub or NBViewer),\n", 1573 | " it may mean that your frontend doesn't currently support widgets.\n", 1574 | "
\n" 1575 | ], 1576 | "text/plain": [ 1577 | "HBox(children=(IntProgress(value=0, max=309), HTML(value='')))" 1578 | ] 1579 | }, 1580 | "metadata": {}, 1581 | "output_type": "display_data" 1582 | }, 1583 | { 1584 | "name": "stdout", 1585 | "output_type": "stream", 1586 | "text": [ 1587 | "Epoch 0: train_loss: 0.4463 train_acc: 0.7892 | val_loss: 0.4154 val_acc: 0.8077\n" 1588 | ] 1589 | }, 1590 | { 1591 | "data": { 1592 | "application/vnd.jupyter.widget-view+json": { 1593 | "model_id": "6da1ef4b2d8f485cbd563fe7c736cce8", 1594 | "version_major": 2, 1595 | "version_minor": 0 1596 | }, 1597 | "text/html": [ 1598 | "Failed to display Jupyter Widget of type HBox.
\n", 1600 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 1601 | " that the widgets JavaScript is still loading. If this message persists, it\n", 1602 | " likely means that the widgets JavaScript library is either not installed or\n", 1603 | " not enabled. See the Jupyter\n", 1604 | " Widgets Documentation for setup instructions.\n", 1605 | "
\n", 1606 | "\n", 1607 | " If you're reading this message in another frontend (for example, a static\n", 1608 | " rendering on GitHub or NBViewer),\n", 1609 | " it may mean that your frontend doesn't currently support widgets.\n", 1610 | "
\n" 1611 | ], 1612 | "text/plain": [ 1613 | "HBox(children=(IntProgress(value=0, max=2467), HTML(value='')))" 1614 | ] 1615 | }, 1616 | "metadata": {}, 1617 | "output_type": "display_data" 1618 | }, 1619 | { 1620 | "data": { 1621 | "application/vnd.jupyter.widget-view+json": { 1622 | "model_id": "1b6fe4fffa884a2b83b18c1bbf580d8b", 1623 | "version_major": 2, 1624 | "version_minor": 0 1625 | }, 1626 | "text/html": [ 1627 | "Failed to display Jupyter Widget of type HBox.
\n", 1629 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 1630 | " that the widgets JavaScript is still loading. If this message persists, it\n", 1631 | " likely means that the widgets JavaScript library is either not installed or\n", 1632 | " not enabled. See the Jupyter\n", 1633 | " Widgets Documentation for setup instructions.\n", 1634 | "
\n", 1635 | "\n", 1636 | " If you're reading this message in another frontend (for example, a static\n", 1637 | " rendering on GitHub or NBViewer),\n", 1638 | " it may mean that your frontend doesn't currently support widgets.\n", 1639 | "
\n" 1640 | ], 1641 | "text/plain": [ 1642 | "HBox(children=(IntProgress(value=0, max=309), HTML(value='')))" 1643 | ] 1644 | }, 1645 | "metadata": {}, 1646 | "output_type": "display_data" 1647 | }, 1648 | { 1649 | "name": "stdout", 1650 | "output_type": "stream", 1651 | "text": [ 1652 | "Epoch 1: train_loss: 0.4071 train_acc: 0.8130 | val_loss: 0.4001 val_acc: 0.8178\n" 1653 | ] 1654 | }, 1655 | { 1656 | "data": { 1657 | "application/vnd.jupyter.widget-view+json": { 1658 | "model_id": "71cef71e4cec4327865021d7be8f22c7", 1659 | "version_major": 2, 1660 | "version_minor": 0 1661 | }, 1662 | "text/html": [ 1663 | "Failed to display Jupyter Widget of type HBox.
\n", 1665 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 1666 | " that the widgets JavaScript is still loading. If this message persists, it\n", 1667 | " likely means that the widgets JavaScript library is either not installed or\n", 1668 | " not enabled. See the Jupyter\n", 1669 | " Widgets Documentation for setup instructions.\n", 1670 | "
\n", 1671 | "\n", 1672 | " If you're reading this message in another frontend (for example, a static\n", 1673 | " rendering on GitHub or NBViewer),\n", 1674 | " it may mean that your frontend doesn't currently support widgets.\n", 1675 | "
\n" 1676 | ], 1677 | "text/plain": [ 1678 | "HBox(children=(IntProgress(value=0, max=2467), HTML(value='')))" 1679 | ] 1680 | }, 1681 | "metadata": {}, 1682 | "output_type": "display_data" 1683 | }, 1684 | { 1685 | "data": { 1686 | "application/vnd.jupyter.widget-view+json": { 1687 | "model_id": "499de9c307ae429dbb79c7ec89a3cd17", 1688 | "version_major": 2, 1689 | "version_minor": 0 1690 | }, 1691 | "text/html": [ 1692 | "Failed to display Jupyter Widget of type HBox.
\n", 1694 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 1695 | " that the widgets JavaScript is still loading. If this message persists, it\n", 1696 | " likely means that the widgets JavaScript library is either not installed or\n", 1697 | " not enabled. See the Jupyter\n", 1698 | " Widgets Documentation for setup instructions.\n", 1699 | "
\n", 1700 | "\n", 1701 | " If you're reading this message in another frontend (for example, a static\n", 1702 | " rendering on GitHub or NBViewer),\n", 1703 | " it may mean that your frontend doesn't currently support widgets.\n", 1704 | "
\n" 1705 | ], 1706 | "text/plain": [ 1707 | "HBox(children=(IntProgress(value=0, max=309), HTML(value='')))" 1708 | ] 1709 | }, 1710 | "metadata": {}, 1711 | "output_type": "display_data" 1712 | }, 1713 | { 1714 | "name": "stdout", 1715 | "output_type": "stream", 1716 | "text": [ 1717 | "Epoch 2: train_loss: 0.3952 train_acc: 0.8199 | val_loss: 0.4060 val_acc: 0.8146\n" 1718 | ] 1719 | }, 1720 | { 1721 | "data": { 1722 | "application/vnd.jupyter.widget-view+json": { 1723 | "model_id": "d2ba9f170596427fbdf673c2da483037", 1724 | "version_major": 2, 1725 | "version_minor": 0 1726 | }, 1727 | "text/html": [ 1728 | "Failed to display Jupyter Widget of type HBox.
\n", 1730 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 1731 | " that the widgets JavaScript is still loading. If this message persists, it\n", 1732 | " likely means that the widgets JavaScript library is either not installed or\n", 1733 | " not enabled. See the Jupyter\n", 1734 | " Widgets Documentation for setup instructions.\n", 1735 | "
\n", 1736 | "\n", 1737 | " If you're reading this message in another frontend (for example, a static\n", 1738 | " rendering on GitHub or NBViewer),\n", 1739 | " it may mean that your frontend doesn't currently support widgets.\n", 1740 | "
\n" 1741 | ], 1742 | "text/plain": [ 1743 | "HBox(children=(IntProgress(value=0, max=2467), HTML(value='')))" 1744 | ] 1745 | }, 1746 | "metadata": {}, 1747 | "output_type": "display_data" 1748 | }, 1749 | { 1750 | "data": { 1751 | "application/vnd.jupyter.widget-view+json": { 1752 | "model_id": "c50af3074c3442b0818944adb72eae87", 1753 | "version_major": 2, 1754 | "version_minor": 0 1755 | }, 1756 | "text/html": [ 1757 | "Failed to display Jupyter Widget of type HBox.
\n", 1759 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 1760 | " that the widgets JavaScript is still loading. If this message persists, it\n", 1761 | " likely means that the widgets JavaScript library is either not installed or\n", 1762 | " not enabled. See the Jupyter\n", 1763 | " Widgets Documentation for setup instructions.\n", 1764 | "
\n", 1765 | "\n", 1766 | " If you're reading this message in another frontend (for example, a static\n", 1767 | " rendering on GitHub or NBViewer),\n", 1768 | " it may mean that your frontend doesn't currently support widgets.\n", 1769 | "
\n" 1770 | ], 1771 | "text/plain": [ 1772 | "HBox(children=(IntProgress(value=0, max=309), HTML(value='')))" 1773 | ] 1774 | }, 1775 | "metadata": {}, 1776 | "output_type": "display_data" 1777 | }, 1778 | { 1779 | "name": "stdout", 1780 | "output_type": "stream", 1781 | "text": [ 1782 | "Epoch 3: train_loss: 0.3877 train_acc: 0.8240 | val_loss: 0.3876 val_acc: 0.8248\n" 1783 | ] 1784 | }, 1785 | { 1786 | "data": { 1787 | "application/vnd.jupyter.widget-view+json": { 1788 | "model_id": "1e2643812f914dcfaa447c5df332dace", 1789 | "version_major": 2, 1790 | "version_minor": 0 1791 | }, 1792 | "text/html": [ 1793 | "Failed to display Jupyter Widget of type HBox.
\n", 1795 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 1796 | " that the widgets JavaScript is still loading. If this message persists, it\n", 1797 | " likely means that the widgets JavaScript library is either not installed or\n", 1798 | " not enabled. See the Jupyter\n", 1799 | " Widgets Documentation for setup instructions.\n", 1800 | "
\n", 1801 | "\n", 1802 | " If you're reading this message in another frontend (for example, a static\n", 1803 | " rendering on GitHub or NBViewer),\n", 1804 | " it may mean that your frontend doesn't currently support widgets.\n", 1805 | "
\n" 1806 | ], 1807 | "text/plain": [ 1808 | "HBox(children=(IntProgress(value=0, max=2467), HTML(value='')))" 1809 | ] 1810 | }, 1811 | "metadata": {}, 1812 | "output_type": "display_data" 1813 | }, 1814 | { 1815 | "data": { 1816 | "application/vnd.jupyter.widget-view+json": { 1817 | "model_id": "bf93640eae874cbea9a154690a4fa9ed", 1818 | "version_major": 2, 1819 | "version_minor": 0 1820 | }, 1821 | "text/html": [ 1822 | "Failed to display Jupyter Widget of type HBox.
\n", 1824 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 1825 | " that the widgets JavaScript is still loading. If this message persists, it\n", 1826 | " likely means that the widgets JavaScript library is either not installed or\n", 1827 | " not enabled. See the Jupyter\n", 1828 | " Widgets Documentation for setup instructions.\n", 1829 | "
\n", 1830 | "\n", 1831 | " If you're reading this message in another frontend (for example, a static\n", 1832 | " rendering on GitHub or NBViewer),\n", 1833 | " it may mean that your frontend doesn't currently support widgets.\n", 1834 | "
\n" 1835 | ], 1836 | "text/plain": [ 1837 | "HBox(children=(IntProgress(value=0, max=309), HTML(value='')))" 1838 | ] 1839 | }, 1840 | "metadata": {}, 1841 | "output_type": "display_data" 1842 | }, 1843 | { 1844 | "name": "stdout", 1845 | "output_type": "stream", 1846 | "text": [ 1847 | "Epoch 4: train_loss: 0.3822 train_acc: 0.8270 | val_loss: 0.3861 val_acc: 0.8256\n", 1848 | "\n" 1849 | ] 1850 | } 1851 | ], 1852 | "source": [ 1853 | "m = SimpleGRU(vocab_size, embedding_dim, n_hidden, n_out, trainds.fields['SentimentText'].vocab.vectors).to(device)\n", 1854 | "opt = optim.Adam(filter(lambda p: p.requires_grad, m.parameters()), 1e-3)\n", 1855 | "\n", 1856 | "fit(model=m, train_dl=train_batch_it, val_dl=val_batch_it, loss_fn=F.nll_loss, opt=opt, epochs=5)" 1857 | ] 1858 | }, 1859 | { 1860 | "cell_type": "markdown", 1861 | "metadata": {}, 1862 | "source": [ 1863 | "##### Train Concat Pooling model" 1864 | ] 1865 | }, 1866 | { 1867 | "cell_type": "code", 1868 | "execution_count": 46, 1869 | "metadata": {}, 1870 | "outputs": [ 1871 | { 1872 | "data": { 1873 | "application/vnd.jupyter.widget-view+json": { 1874 | "model_id": "66ccb790ac2e4a6c8032a3a0df71bace", 1875 | "version_major": 2, 1876 | "version_minor": 0 1877 | }, 1878 | "text/html": [ 1879 | "Failed to display Jupyter Widget of type HBox.
\n", 1881 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 1882 | " that the widgets JavaScript is still loading. If this message persists, it\n", 1883 | " likely means that the widgets JavaScript library is either not installed or\n", 1884 | " not enabled. See the Jupyter\n", 1885 | " Widgets Documentation for setup instructions.\n", 1886 | "
\n", 1887 | "\n", 1888 | " If you're reading this message in another frontend (for example, a static\n", 1889 | " rendering on GitHub or NBViewer),\n", 1890 | " it may mean that your frontend doesn't currently support widgets.\n", 1891 | "
\n" 1892 | ], 1893 | "text/plain": [ 1894 | "HBox(children=(IntProgress(value=0, max=5), HTML(value='')))" 1895 | ] 1896 | }, 1897 | "metadata": {}, 1898 | "output_type": "display_data" 1899 | }, 1900 | { 1901 | "data": { 1902 | "application/vnd.jupyter.widget-view+json": { 1903 | "model_id": "5ef3cfad0728452f88009d677b482d87", 1904 | "version_major": 2, 1905 | "version_minor": 0 1906 | }, 1907 | "text/html": [ 1908 | "Failed to display Jupyter Widget of type HBox.
\n", 1910 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 1911 | " that the widgets JavaScript is still loading. If this message persists, it\n", 1912 | " likely means that the widgets JavaScript library is either not installed or\n", 1913 | " not enabled. See the Jupyter\n", 1914 | " Widgets Documentation for setup instructions.\n", 1915 | "
\n", 1916 | "\n", 1917 | " If you're reading this message in another frontend (for example, a static\n", 1918 | " rendering on GitHub or NBViewer),\n", 1919 | " it may mean that your frontend doesn't currently support widgets.\n", 1920 | "
\n" 1921 | ], 1922 | "text/plain": [ 1923 | "HBox(children=(IntProgress(value=0, max=2467), HTML(value='')))" 1924 | ] 1925 | }, 1926 | "metadata": {}, 1927 | "output_type": "display_data" 1928 | }, 1929 | { 1930 | "data": { 1931 | "application/vnd.jupyter.widget-view+json": { 1932 | "model_id": "9243767da3034e80b730c782435e6421", 1933 | "version_major": 2, 1934 | "version_minor": 0 1935 | }, 1936 | "text/html": [ 1937 | "Failed to display Jupyter Widget of type HBox.
\n", 1939 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 1940 | " that the widgets JavaScript is still loading. If this message persists, it\n", 1941 | " likely means that the widgets JavaScript library is either not installed or\n", 1942 | " not enabled. See the Jupyter\n", 1943 | " Widgets Documentation for setup instructions.\n", 1944 | "
\n", 1945 | "\n", 1946 | " If you're reading this message in another frontend (for example, a static\n", 1947 | " rendering on GitHub or NBViewer),\n", 1948 | " it may mean that your frontend doesn't currently support widgets.\n", 1949 | "
\n" 1950 | ], 1951 | "text/plain": [ 1952 | "HBox(children=(IntProgress(value=0, max=309), HTML(value='')))" 1953 | ] 1954 | }, 1955 | "metadata": {}, 1956 | "output_type": "display_data" 1957 | }, 1958 | { 1959 | "name": "stdout", 1960 | "output_type": "stream", 1961 | "text": [ 1962 | "Epoch 0: train_loss: 0.4349 train_acc: 0.7959 | val_loss: 0.4036 val_acc: 0.8153\n" 1963 | ] 1964 | }, 1965 | { 1966 | "data": { 1967 | "application/vnd.jupyter.widget-view+json": { 1968 | "model_id": "7efe0017eda740f589749a31671674e5", 1969 | "version_major": 2, 1970 | "version_minor": 0 1971 | }, 1972 | "text/html": [ 1973 | "Failed to display Jupyter Widget of type HBox.
\n", 1975 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 1976 | " that the widgets JavaScript is still loading. If this message persists, it\n", 1977 | " likely means that the widgets JavaScript library is either not installed or\n", 1978 | " not enabled. See the Jupyter\n", 1979 | " Widgets Documentation for setup instructions.\n", 1980 | "
\n", 1981 | "\n", 1982 | " If you're reading this message in another frontend (for example, a static\n", 1983 | " rendering on GitHub or NBViewer),\n", 1984 | " it may mean that your frontend doesn't currently support widgets.\n", 1985 | "
\n" 1986 | ], 1987 | "text/plain": [ 1988 | "HBox(children=(IntProgress(value=0, max=2467), HTML(value='')))" 1989 | ] 1990 | }, 1991 | "metadata": {}, 1992 | "output_type": "display_data" 1993 | }, 1994 | { 1995 | "data": { 1996 | "application/vnd.jupyter.widget-view+json": { 1997 | "model_id": "3b9ef438202f4a61b4216c90866a8210", 1998 | "version_major": 2, 1999 | "version_minor": 0 2000 | }, 2001 | "text/html": [ 2002 | "Failed to display Jupyter Widget of type HBox.
\n", 2004 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 2005 | " that the widgets JavaScript is still loading. If this message persists, it\n", 2006 | " likely means that the widgets JavaScript library is either not installed or\n", 2007 | " not enabled. See the Jupyter\n", 2008 | " Widgets Documentation for setup instructions.\n", 2009 | "
\n", 2010 | "\n", 2011 | " If you're reading this message in another frontend (for example, a static\n", 2012 | " rendering on GitHub or NBViewer),\n", 2013 | " it may mean that your frontend doesn't currently support widgets.\n", 2014 | "
\n" 2015 | ], 2016 | "text/plain": [ 2017 | "HBox(children=(IntProgress(value=0, max=309), HTML(value='')))" 2018 | ] 2019 | }, 2020 | "metadata": {}, 2021 | "output_type": "display_data" 2022 | }, 2023 | { 2024 | "name": "stdout", 2025 | "output_type": "stream", 2026 | "text": [ 2027 | "Epoch 1: train_loss: 0.3975 train_acc: 0.8189 | val_loss: 0.3913 val_acc: 0.8227\n" 2028 | ] 2029 | }, 2030 | { 2031 | "data": { 2032 | "application/vnd.jupyter.widget-view+json": { 2033 | "model_id": "b5fef79a65304fac8c381c4708cdde51", 2034 | "version_major": 2, 2035 | "version_minor": 0 2036 | }, 2037 | "text/html": [ 2038 | "Failed to display Jupyter Widget of type HBox.
\n", 2040 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 2041 | " that the widgets JavaScript is still loading. If this message persists, it\n", 2042 | " likely means that the widgets JavaScript library is either not installed or\n", 2043 | " not enabled. See the Jupyter\n", 2044 | " Widgets Documentation for setup instructions.\n", 2045 | "
\n", 2046 | "\n", 2047 | " If you're reading this message in another frontend (for example, a static\n", 2048 | " rendering on GitHub or NBViewer),\n", 2049 | " it may mean that your frontend doesn't currently support widgets.\n", 2050 | "
\n" 2051 | ], 2052 | "text/plain": [ 2053 | "HBox(children=(IntProgress(value=0, max=2467), HTML(value='')))" 2054 | ] 2055 | }, 2056 | "metadata": {}, 2057 | "output_type": "display_data" 2058 | }, 2059 | { 2060 | "data": { 2061 | "application/vnd.jupyter.widget-view+json": { 2062 | "model_id": "23985883852c4784aadd4db473436f78", 2063 | "version_major": 2, 2064 | "version_minor": 0 2065 | }, 2066 | "text/html": [ 2067 | "Failed to display Jupyter Widget of type HBox.
\n", 2069 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 2070 | " that the widgets JavaScript is still loading. If this message persists, it\n", 2071 | " likely means that the widgets JavaScript library is either not installed or\n", 2072 | " not enabled. See the Jupyter\n", 2073 | " Widgets Documentation for setup instructions.\n", 2074 | "
\n", 2075 | "\n", 2076 | " If you're reading this message in another frontend (for example, a static\n", 2077 | " rendering on GitHub or NBViewer),\n", 2078 | " it may mean that your frontend doesn't currently support widgets.\n", 2079 | "
\n" 2080 | ], 2081 | "text/plain": [ 2082 | "HBox(children=(IntProgress(value=0, max=309), HTML(value='')))" 2083 | ] 2084 | }, 2085 | "metadata": {}, 2086 | "output_type": "display_data" 2087 | }, 2088 | { 2089 | "name": "stdout", 2090 | "output_type": "stream", 2091 | "text": [ 2092 | "Epoch 2: train_loss: 0.3853 train_acc: 0.8257 | val_loss: 0.3877 val_acc: 0.8250\n" 2093 | ] 2094 | }, 2095 | { 2096 | "data": { 2097 | "application/vnd.jupyter.widget-view+json": { 2098 | "model_id": "1220e354f91944f0b676d51ab018c564", 2099 | "version_major": 2, 2100 | "version_minor": 0 2101 | }, 2102 | "text/html": [ 2103 | "Failed to display Jupyter Widget of type HBox.
\n", 2105 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 2106 | " that the widgets JavaScript is still loading. If this message persists, it\n", 2107 | " likely means that the widgets JavaScript library is either not installed or\n", 2108 | " not enabled. See the Jupyter\n", 2109 | " Widgets Documentation for setup instructions.\n", 2110 | "
\n", 2111 | "\n", 2112 | " If you're reading this message in another frontend (for example, a static\n", 2113 | " rendering on GitHub or NBViewer),\n", 2114 | " it may mean that your frontend doesn't currently support widgets.\n", 2115 | "
\n" 2116 | ], 2117 | "text/plain": [ 2118 | "HBox(children=(IntProgress(value=0, max=2467), HTML(value='')))" 2119 | ] 2120 | }, 2121 | "metadata": {}, 2122 | "output_type": "display_data" 2123 | }, 2124 | { 2125 | "data": { 2126 | "application/vnd.jupyter.widget-view+json": { 2127 | "model_id": "ffd489350f1b47bea4aa191dd29a66cd", 2128 | "version_major": 2, 2129 | "version_minor": 0 2130 | }, 2131 | "text/html": [ 2132 | "Failed to display Jupyter Widget of type HBox.
\n", 2134 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 2135 | " that the widgets JavaScript is still loading. If this message persists, it\n", 2136 | " likely means that the widgets JavaScript library is either not installed or\n", 2137 | " not enabled. See the Jupyter\n", 2138 | " Widgets Documentation for setup instructions.\n", 2139 | "
\n", 2140 | "\n", 2141 | " If you're reading this message in another frontend (for example, a static\n", 2142 | " rendering on GitHub or NBViewer),\n", 2143 | " it may mean that your frontend doesn't currently support widgets.\n", 2144 | "
\n" 2145 | ], 2146 | "text/plain": [ 2147 | "HBox(children=(IntProgress(value=0, max=309), HTML(value='')))" 2148 | ] 2149 | }, 2150 | "metadata": {}, 2151 | "output_type": "display_data" 2152 | }, 2153 | { 2154 | "name": "stdout", 2155 | "output_type": "stream", 2156 | "text": [ 2157 | "Epoch 3: train_loss: 0.3777 train_acc: 0.8300 | val_loss: 0.3822 val_acc: 0.8283\n" 2158 | ] 2159 | }, 2160 | { 2161 | "data": { 2162 | "application/vnd.jupyter.widget-view+json": { 2163 | "model_id": "c06d5486bfab4167a57cb91b4a42c20c", 2164 | "version_major": 2, 2165 | "version_minor": 0 2166 | }, 2167 | "text/html": [ 2168 | "Failed to display Jupyter Widget of type HBox.
\n", 2170 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 2171 | " that the widgets JavaScript is still loading. If this message persists, it\n", 2172 | " likely means that the widgets JavaScript library is either not installed or\n", 2173 | " not enabled. See the Jupyter\n", 2174 | " Widgets Documentation for setup instructions.\n", 2175 | "
\n", 2176 | "\n", 2177 | " If you're reading this message in another frontend (for example, a static\n", 2178 | " rendering on GitHub or NBViewer),\n", 2179 | " it may mean that your frontend doesn't currently support widgets.\n", 2180 | "
\n" 2181 | ], 2182 | "text/plain": [ 2183 | "HBox(children=(IntProgress(value=0, max=2467), HTML(value='')))" 2184 | ] 2185 | }, 2186 | "metadata": {}, 2187 | "output_type": "display_data" 2188 | }, 2189 | { 2190 | "data": { 2191 | "application/vnd.jupyter.widget-view+json": { 2192 | "model_id": "314e0deb0730405c926399a01991ec09", 2193 | "version_major": 2, 2194 | "version_minor": 0 2195 | }, 2196 | "text/html": [ 2197 | "Failed to display Jupyter Widget of type HBox.
\n", 2199 | " If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n", 2200 | " that the widgets JavaScript is still loading. If this message persists, it\n", 2201 | " likely means that the widgets JavaScript library is either not installed or\n", 2202 | " not enabled. See the Jupyter\n", 2203 | " Widgets Documentation for setup instructions.\n", 2204 | "
\n", 2205 | "\n", 2206 | " If you're reading this message in another frontend (for example, a static\n", 2207 | " rendering on GitHub or NBViewer),\n", 2208 | " it may mean that your frontend doesn't currently support widgets.\n", 2209 | "
\n" 2210 | ], 2211 | "text/plain": [ 2212 | "HBox(children=(IntProgress(value=0, max=309), HTML(value='')))" 2213 | ] 2214 | }, 2215 | "metadata": {}, 2216 | "output_type": "display_data" 2217 | }, 2218 | { 2219 | "name": "stdout", 2220 | "output_type": "stream", 2221 | "text": [ 2222 | "Epoch 4: train_loss: 0.3715 train_acc: 0.8332 | val_loss: 0.3804 val_acc: 0.8289\n", 2223 | "\n" 2224 | ] 2225 | } 2226 | ], 2227 | "source": [ 2228 | "m = ConcatPoolingGRUAdaptive(vocab_size, embedding_dim, n_hidden, n_out, trainds.fields['SentimentText'].vocab.vectors).to(device)\n", 2229 | "opt = optim.Adam(filter(lambda p: p.requires_grad, m.parameters()), 1e-3)\n", 2230 | "\n", 2231 | "fit(model=m, train_dl=train_batch_it, val_dl=val_batch_it, loss_fn=F.nll_loss, opt=opt, epochs=5)" 2232 | ] 2233 | } 2234 | ], 2235 | "metadata": { 2236 | "kernelspec": { 2237 | "display_name": "Python 3", 2238 | "language": "python", 2239 | "name": "python3" 2240 | }, 2241 | "language_info": { 2242 | "codemirror_mode": { 2243 | "name": "ipython", 2244 | "version": 3 2245 | }, 2246 | "file_extension": ".py", 2247 | "mimetype": "text/x-python", 2248 | "name": "python", 2249 | "nbconvert_exporter": "python", 2250 | "pygments_lexer": "ipython3", 2251 | "version": "3.6.4" 2252 | } 2253 | }, 2254 | "nbformat": 4, 2255 | "nbformat_minor": 2 2256 | } 2257 | --------------------------------------------------------------------------------