Created
July 30, 2020 16:28
-
-
Save brockmanmatt/05b8531a015e83764479f14d469cbfa1 to your computer and use it in GitHub Desktop.
explain2SecondGraderLogprob.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "explain2SecondGraderLogprob.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyMTO+zv1xKFpLhmxt5HoWKO", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/brockmanmatt/05b8531a015e83764479f14d469cbfa1/explain2secondgraderlogprob.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "J7wnsgT2kPut", | |
"colab_type": "code", | |
"colab": { | |
"resources": { | |
"http://localhost:8080/nbextensions/google.colab/files.js": { | |
"data": "Ly8gQ29weXJpZ2h0IDIwMTcgR29vZ2xlIExMQwovLwovLyBMaWNlbnNlZCB1bmRlciB0aGUgQXBhY2hlIExpY2Vuc2UsIFZlcnNpb24gMi4wICh0aGUgIkxpY2Vuc2UiKTsKLy8geW91IG1heSBub3QgdXNlIHRoaXMgZmlsZSBleGNlcHQgaW4gY29tcGxpYW5jZSB3aXRoIHRoZSBMaWNlbnNlLgovLyBZb3UgbWF5IG9idGFpbiBhIGNvcHkgb2YgdGhlIExpY2Vuc2UgYXQKLy8KLy8gICAgICBodHRwOi8vd3d3LmFwYWNoZS5vcmcvbGljZW5zZXMvTElDRU5TRS0yLjAKLy8KLy8gV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"headers": [ | |
[ | |
"content-type", | |
"application/javascript" | |
] | |
], | |
"status": 200, | |
"status_text": "" | |
} | |
}, | |
"base_uri": "https://localhost:8080/", | |
"height": 89 | |
}, | |
"outputId": "0ce8afee-d467-42ec-a7ed-c877e06907c6" | |
}, | |
"source": [ | |
"from google.colab import files\n", | |
"uploaded = files.upload()\n", | |
"print(\"done\")" | |
], | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/html": [ | |
"\n", | |
" <input type=\"file\" id=\"files-bbed7c9f-2297-41a6-916b-3a5e692e126d\" name=\"files[]\" multiple disabled\n", | |
" style=\"border:none\" />\n", | |
" <output id=\"result-bbed7c9f-2297-41a6-916b-3a5e692e126d\">\n", | |
" Upload widget is only available when the cell has been executed in the\n", | |
" current browser session. Please rerun this cell to enable.\n", | |
" </output>\n", | |
" <script src=\"/nbextensions/google.colab/files.js\"></script> " | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
} | |
}, | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Saving key.json to key.json\n", | |
"done\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "WHPHrUnhpKnI", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"I'll install the API" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "zq0ltp2xn4yt", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 292 | |
}, | |
"outputId": "9c1c5ea3-6a6c-4695-a480-a029afe41031" | |
}, | |
"source": [ | |
"!pip install openai\n", | |
"import openai, json, pandas as pd" | |
], | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Collecting openai\n", | |
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/a8/65/c7461f4c87984534683f480ea5742777bc39bbf5721123194c2d0347dc1f/openai-0.2.4.tar.gz (157kB)\n", | |
"\r\u001b[K |██ | 10kB 17.7MB/s eta 0:00:01\r\u001b[K |████▏ | 20kB 1.6MB/s eta 0:00:01\r\u001b[K |██████▎ | 30kB 2.1MB/s eta 0:00:01\r\u001b[K |████████▍ | 40kB 2.4MB/s eta 0:00:01\r\u001b[K |██████████▍ | 51kB 1.9MB/s eta 0:00:01\r\u001b[K |████████████▌ | 61kB 2.2MB/s eta 0:00:01\r\u001b[K |██████████████▋ | 71kB 2.4MB/s eta 0:00:01\r\u001b[K |████████████████▊ | 81kB 2.6MB/s eta 0:00:01\r\u001b[K |██████████████████▊ | 92kB 2.7MB/s eta 0:00:01\r\u001b[K |████████████████████▉ | 102kB 2.7MB/s eta 0:00:01\r\u001b[K |███████████████████████ | 112kB 2.7MB/s eta 0:00:01\r\u001b[K |█████████████████████████ | 122kB 2.7MB/s eta 0:00:01\r\u001b[K |███████████████████████████ | 133kB 2.7MB/s eta 0:00:01\r\u001b[K |█████████████████████████████▏ | 143kB 2.7MB/s eta 0:00:01\r\u001b[K |███████████████████████████████▎| 153kB 2.7MB/s eta 0:00:01\r\u001b[K |████████████████████████████████| 163kB 2.7MB/s \n", | |
"\u001b[?25hRequirement already satisfied: requests>=2.20 in /usr/local/lib/python3.6/dist-packages (from openai) (2.23.0)\n", | |
"Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->openai) (2.10)\n", | |
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->openai) (1.24.3)\n", | |
"Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->openai) (3.0.4)\n", | |
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->openai) (2020.6.20)\n", | |
"Building wheels for collected packages: openai\n", | |
" Building wheel for openai (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
" Created wheel for openai: filename=openai-0.2.4-cp36-none-any.whl size=170709 sha256=b718cbd1823fff82bf433d2d9203c13d2064a796477ac8f0dec4a277bac83ebc\n", | |
" Stored in directory: /root/.cache/pip/wheels/74/96/c8/c6e170929c276b836613e1b9985343b501fe455e53d85e7d48\n", | |
"Successfully built openai\n", | |
"Installing collected packages: openai\n", | |
"Successfully installed openai-0.2.4\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Q2yE0jcnpMEV", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Loading in key.json that I uploaded; I do this so I don't need to worry about accidently leaking creds if I share the colab (which I'm 99% sure is just a json file that won't expose them)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "bwNXXwHen5x9", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"openai.api_key = json.load(open(\"key.json\", \"r\"))[\"key\"]" | |
], | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "k67w5H0fpTkT", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Default keyword arguments to pass the aPI" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "e1EwpqqJkTYh", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"#arguments to send the API\n" | |
], | |
"execution_count": 4, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "zZubgPoOpWDH", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Quick wrapper to automatically save prompts and responses sent for later analysis if needed" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "sXTDJx0An9Bl", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"def e2sg(text):\n", | |
" \"\"\"\n", | |
" wrapper for the API to ask explain to 2nd grader\n", | |
" \"\"\"\n", | |
"\n", | |
" kwargs = {\n", | |
"\"engine\":\"davinci\",\n", | |
"\"temperature\":.5,\n", | |
"\"max_tokens\":100,\n", | |
"\"frequency_penalty\":.2,\n", | |
"\"stop\":\"\\n\",\n", | |
"\"logprobs\":1,\n", | |
"\"echo\":True,\n", | |
"}\n", | |
"\n", | |
" prefix = 'My second grader asked me what this passage means:\\n\"\"\"\\n'\n", | |
" suffix = '\\n\"\"\"\\n\\nI rephrased it for him, in plain language a second grader can understand:\\n\"\"\"\\n'\n", | |
"\n", | |
" #[\"choices\"][0][\"text\"] is text\n", | |
" #[\"choices\"][0][\"logprobs\"] is logprobs\n", | |
" r = openai.Completion.create(prompt=\"{}{}{}\".format(prefix, text, suffix), **kwargs)\n", | |
" return r" | |
], | |
"execution_count": 19, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "EdFXafcJpZ3Q", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Test to make sure my query works" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "4SlyKgjyopPn", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"#try gettysburg address\n", | |
"text = \"But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract.\"\n", | |
"r = e2sg(text)" | |
], | |
"execution_count": 20, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "BjJEkhBTJpO6", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 256 | |
}, | |
"outputId": "5cde53dd-e681-46f1-d0c9-9fd2337c805d" | |
}, | |
"source": [ | |
"pd.DataFrame(r[\"choices\"][0][\"logprobs\"]).T" | |
], | |
"execution_count": 21, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>0</th>\n", | |
" <th>1</th>\n", | |
" <th>2</th>\n", | |
" <th>3</th>\n", | |
" <th>4</th>\n", | |
" <th>5</th>\n", | |
" <th>6</th>\n", | |
" <th>7</th>\n", | |
" <th>8</th>\n", | |
" <th>9</th>\n", | |
" <th>10</th>\n", | |
" <th>11</th>\n", | |
" <th>12</th>\n", | |
" <th>13</th>\n", | |
" <th>14</th>\n", | |
" <th>15</th>\n", | |
" <th>16</th>\n", | |
" <th>17</th>\n", | |
" <th>18</th>\n", | |
" <th>19</th>\n", | |
" <th>20</th>\n", | |
" <th>21</th>\n", | |
" <th>22</th>\n", | |
" <th>23</th>\n", | |
" <th>24</th>\n", | |
" <th>25</th>\n", | |
" <th>26</th>\n", | |
" <th>27</th>\n", | |
" <th>28</th>\n", | |
" <th>29</th>\n", | |
" <th>30</th>\n", | |
" <th>31</th>\n", | |
" <th>32</th>\n", | |
" <th>33</th>\n", | |
" <th>34</th>\n", | |
" <th>35</th>\n", | |
" <th>36</th>\n", | |
" <th>37</th>\n", | |
" <th>38</th>\n", | |
" <th>39</th>\n", | |
" <th>...</th>\n", | |
" <th>150</th>\n", | |
" <th>151</th>\n", | |
" <th>152</th>\n", | |
" <th>153</th>\n", | |
" <th>154</th>\n", | |
" <th>155</th>\n", | |
" <th>156</th>\n", | |
" <th>157</th>\n", | |
" <th>158</th>\n", | |
" <th>159</th>\n", | |
" <th>160</th>\n", | |
" <th>161</th>\n", | |
" <th>162</th>\n", | |
" <th>163</th>\n", | |
" <th>164</th>\n", | |
" <th>165</th>\n", | |
" <th>166</th>\n", | |
" <th>167</th>\n", | |
" <th>168</th>\n", | |
" <th>169</th>\n", | |
" <th>170</th>\n", | |
" <th>171</th>\n", | |
" <th>172</th>\n", | |
" <th>173</th>\n", | |
" <th>174</th>\n", | |
" <th>175</th>\n", | |
" <th>176</th>\n", | |
" <th>177</th>\n", | |
" <th>178</th>\n", | |
" <th>179</th>\n", | |
" <th>180</th>\n", | |
" <th>181</th>\n", | |
" <th>182</th>\n", | |
" <th>183</th>\n", | |
" <th>184</th>\n", | |
" <th>185</th>\n", | |
" <th>186</th>\n", | |
" <th>187</th>\n", | |
" <th>188</th>\n", | |
" <th>189</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>tokens</th>\n", | |
" <td>My</td>\n", | |
" <td>second</td>\n", | |
" <td>grad</td>\n", | |
" <td>er</td>\n", | |
" <td>asked</td>\n", | |
" <td>me</td>\n", | |
" <td>what</td>\n", | |
" <td>this</td>\n", | |
" <td>passage</td>\n", | |
" <td>means</td>\n", | |
" <td>:</td>\n", | |
" <td>\\n</td>\n", | |
" <td>\"\"\"</td>\n", | |
" <td>\\n</td>\n", | |
" <td>But</td>\n", | |
" <td>,</td>\n", | |
" <td>in</td>\n", | |
" <td>a</td>\n", | |
" <td>larger</td>\n", | |
" <td>sense</td>\n", | |
" <td>,</td>\n", | |
" <td>we</td>\n", | |
" <td>can</td>\n", | |
" <td>not</td>\n", | |
" <td>dedicate</td>\n", | |
" <td>--</td>\n", | |
" <td>we</td>\n", | |
" <td>can</td>\n", | |
" <td>not</td>\n", | |
" <td>consec</td>\n", | |
" <td>rate</td>\n", | |
" <td>--</td>\n", | |
" <td>we</td>\n", | |
" <td>can</td>\n", | |
" <td>not</td>\n", | |
" <td>hall</td>\n", | |
" <td>ow</td>\n", | |
" <td>--</td>\n", | |
" <td>this</td>\n", | |
" <td>ground</td>\n", | |
" <td>...</td>\n", | |
" <td>their</td>\n", | |
" <td>lives</td>\n", | |
" <td>for</td>\n", | |
" <td>our</td>\n", | |
" <td>country</td>\n", | |
" <td>.</td>\n", | |
" <td>The</td>\n", | |
" <td>soldiers</td>\n", | |
" <td>have</td>\n", | |
" <td>given</td>\n", | |
" <td>their</td>\n", | |
" <td>lives</td>\n", | |
" <td>for</td>\n", | |
" <td>God</td>\n", | |
" <td>and</td>\n", | |
" <td>for</td>\n", | |
" <td>our</td>\n", | |
" <td>freedom</td>\n", | |
" <td>.</td>\n", | |
" <td>We</td>\n", | |
" <td>can</td>\n", | |
" <td>not</td>\n", | |
" <td>give</td>\n", | |
" <td>the</td>\n", | |
" <td>soldiers</td>\n", | |
" <td>back</td>\n", | |
" <td>their</td>\n", | |
" <td>lives</td>\n", | |
" <td>.</td>\n", | |
" <td>The</td>\n", | |
" <td>soldiers</td>\n", | |
" <td>have</td>\n", | |
" <td>given</td>\n", | |
" <td>their</td>\n", | |
" <td>lives</td>\n", | |
" <td>for</td>\n", | |
" <td>us</td>\n", | |
" <td>.\"</td>\n", | |
" <td>\\n</td>\n", | |
" <td>\"\"</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>token_logprobs</th>\n", | |
" <td>NaN</td>\n", | |
" <td>-6.3014</td>\n", | |
" <td>-6.59624</td>\n", | |
" <td>-0.45612</td>\n", | |
" <td>-4.85101</td>\n", | |
" <td>-0.708702</td>\n", | |
" <td>-2.48351</td>\n", | |
" <td>-4.21769</td>\n", | |
" <td>-7.91869</td>\n", | |
" <td>-2.68147</td>\n", | |
" <td>-1.57117</td>\n", | |
" <td>-1.13919</td>\n", | |
" <td>-15.3476</td>\n", | |
" <td>-5.79359</td>\n", | |
" <td>-4.4973</td>\n", | |
" <td>-3.78268</td>\n", | |
" <td>-3.57015</td>\n", | |
" <td>-2.92764</td>\n", | |
" <td>-1.76727</td>\n", | |
" <td>-0.0311699</td>\n", | |
" <td>-0.00574493</td>\n", | |
" <td>-0.0108452</td>\n", | |
" <td>-0.140453</td>\n", | |
" <td>-0.00385475</td>\n", | |
" <td>-0.000936508</td>\n", | |
" <td>-1.73754</td>\n", | |
" <td>-0.0162621</td>\n", | |
" <td>-0.0295334</td>\n", | |
" <td>-0.00147629</td>\n", | |
" <td>-0.00117493</td>\n", | |
" <td>-0.000179291</td>\n", | |
" <td>-0.00283051</td>\n", | |
" <td>-0.0170021</td>\n", | |
" <td>-0.00157547</td>\n", | |
" <td>-0.00542068</td>\n", | |
" <td>-0.00871277</td>\n", | |
" <td>-0.000598907</td>\n", | |
" <td>-0.102959</td>\n", | |
" <td>-0.00331879</td>\n", | |
" <td>-0.00640297</td>\n", | |
" <td>...</td>\n", | |
" <td>-0.964836</td>\n", | |
" <td>-0.348801</td>\n", | |
" <td>-0.716309</td>\n", | |
" <td>-0.330822</td>\n", | |
" <td>-0.000629425</td>\n", | |
" <td>-0.0881691</td>\n", | |
" <td>-0.00575638</td>\n", | |
" <td>-0.000221252</td>\n", | |
" <td>-0.000190735</td>\n", | |
" <td>-0.883978</td>\n", | |
" <td>-1.31788</td>\n", | |
" <td>-0.862667</td>\n", | |
" <td>-2.02481</td>\n", | |
" <td>-2.63062</td>\n", | |
" <td>-1.11817</td>\n", | |
" <td>-1.02544</td>\n", | |
" <td>-2.28532</td>\n", | |
" <td>-1.15166</td>\n", | |
" <td>-0.479481</td>\n", | |
" <td>-1.61015</td>\n", | |
" <td>-0.313656</td>\n", | |
" <td>-0.105412</td>\n", | |
" <td>-1.02492</td>\n", | |
" <td>-0.881683</td>\n", | |
" <td>-0.0368042</td>\n", | |
" <td>-0.286751</td>\n", | |
" <td>-0.135735</td>\n", | |
" <td>-0.0943375</td>\n", | |
" <td>-0.687275</td>\n", | |
" <td>-0.848679</td>\n", | |
" <td>-0.0718842</td>\n", | |
" <td>-0.37537</td>\n", | |
" <td>-0.360333</td>\n", | |
" <td>-0.141285</td>\n", | |
" <td>-0.406536</td>\n", | |
" <td>-2.02509</td>\n", | |
" <td>-1.28326</td>\n", | |
" <td>-0.983147</td>\n", | |
" <td>-0.88813</td>\n", | |
" <td>-1.20859</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>top_logprobs</th>\n", | |
" <td>None</td>\n", | |
" <td>{' favorite': -3.81703}</td>\n", | |
" <td>{' favorite': -3.3462372}</td>\n", | |
" <td>{'er': -0.45611954}</td>\n", | |
" <td>{' is': -1.8822632}</td>\n", | |
" <td>{' me': -0.7087021}</td>\n", | |
" <td>{' to': -1.4835091}</td>\n", | |
" <td>{' I': -1.5301857}</td>\n", | |
" <td>{' was': -1.3093109}</td>\n", | |
" <td>{' was': -0.9939728}</td>\n", | |
" <td>{'.': -0.9149208}</td>\n", | |
" <td>{'\n", | |
"': -1.1391945}</td>\n", | |
" <td>{'\n", | |
"': -0.0038375854}</td>\n", | |
" <td>{'The': -2.6685944}</td>\n", | |
" <td>{'\"': -1.7941704}</td>\n", | |
" <td>{' the': -2.1889343}</td>\n", | |
" <td>{' as': -2.5388985}</td>\n", | |
" <td>{' the': -1.3651352}</td>\n", | |
" <td>{' larger': -1.767273}</td>\n", | |
" <td>{' sense': -0.031169891}</td>\n", | |
" <td>{',': -0.005744934}</td>\n", | |
" <td>{' we': -0.010845184}</td>\n", | |
" <td>{' can': -0.14045334}</td>\n", | |
" <td>{' not': -0.0038547516}</td>\n", | |
" <td>{' dedicate': -0.0009365082}</td>\n", | |
" <td>{'—': -0.956295}</td>\n", | |
" <td>{' we': -0.016262054}</td>\n", | |
" <td>{' can': -0.029533386}</td>\n", | |
" <td>{' not': -0.0014762878}</td>\n", | |
" <td>{' consec': -0.0011749268}</td>\n", | |
" <td>{'rate': -0.00017929077}</td>\n", | |
" <td>{' --': -0.0028305054}</td>\n", | |
" <td>{' we': -0.017002106}</td>\n", | |
" <td>{' can': -0.00157547}</td>\n", | |
" <td>{' not': -0.005420685}</td>\n", | |
" <td>{' hall': -0.008712769}</td>\n", | |
" <td>{'ow': -0.00059890747}</td>\n", | |
" <td>{' --': -0.10295868}</td>\n", | |
" <td>{' this': -0.0033187866}</td>\n", | |
" <td>{' ground': -0.0064029694}</td>\n", | |
" <td>...</td>\n", | |
" <td>{' their': -0.9648361}</td>\n", | |
" <td>{' lives': -0.34880066}</td>\n", | |
" <td>{' for': -0.7163086}</td>\n", | |
" <td>{' our': -0.330822}</td>\n", | |
" <td>{' country': -0.00062942505}</td>\n", | |
" <td>{'.': -0.0881691}</td>\n", | |
" <td>{' The': -0.005756378}</td>\n", | |
" <td>{' soldiers': -0.00022125244}</td>\n", | |
" <td>{' have': -0.00019073486}</td>\n", | |
" <td>{' given': -0.8839779}</td>\n", | |
" <td>{' their': -1.3178768}</td>\n", | |
" <td>{' lives': -0.8626671}</td>\n", | |
" <td>{' for': -2.024807}</td>\n", | |
" <td>{' our': -1.2966366}</td>\n", | |
" <td>{'.': -0.19216919}</td>\n", | |
" <td>{' for': -1.0254402}</td>\n", | |
" <td>{' their': -2.2228203}</td>\n", | |
" <td>{' country': -0.80791473}</td>\n", | |
" <td>{'.': -0.47948074}</td>\n", | |
" <td>{' The': -1.0789032}</td>\n", | |
" <td>{' can': -0.31365585}</td>\n", | |
" <td>{' not': -0.10541153}</td>\n", | |
" <td>{' take': -0.9311676}</td>\n", | |
" <td>{' the': -0.88168335}</td>\n", | |
" <td>{' soldiers': -0.0368042}</td>\n", | |
" <td>{' back': -0.2867508}</td>\n", | |
" <td>{' their': -0.13573456}</td>\n", | |
" <td>{' lives': -0.09433746}</td>\n", | |
" <td>{'.': -0.68727493}</td>\n", | |
" <td>{' The': -0.8486786}</td>\n", | |
" <td>{' soldiers': -0.071884155}</td>\n", | |
" <td>{' have': -0.37537003}</td>\n", | |
" <td>{' given': -0.3603325}</td>\n", | |
" <td>{' their': -0.14128494}</td>\n", | |
" <td>{' lives': -0.4065361}</td>\n", | |
" <td>{' for': -2.0250854}</td>\n", | |
" <td>{' us': -1.2832603}</td>\n", | |
" <td>{'.\"': -0.98314667}</td>\n", | |
" <td>{'\n", | |
"': -0.8881302}</td>\n", | |
" <td>{'\"\"': -1.2085857}</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>text_offset</th>\n", | |
" <td>0</td>\n", | |
" <td>2</td>\n", | |
" <td>9</td>\n", | |
" <td>14</td>\n", | |
" <td>16</td>\n", | |
" <td>22</td>\n", | |
" <td>25</td>\n", | |
" <td>30</td>\n", | |
" <td>35</td>\n", | |
" <td>43</td>\n", | |
" <td>49</td>\n", | |
" <td>50</td>\n", | |
" <td>51</td>\n", | |
" <td>54</td>\n", | |
" <td>55</td>\n", | |
" <td>58</td>\n", | |
" <td>59</td>\n", | |
" <td>62</td>\n", | |
" <td>64</td>\n", | |
" <td>71</td>\n", | |
" <td>77</td>\n", | |
" <td>78</td>\n", | |
" <td>81</td>\n", | |
" <td>85</td>\n", | |
" <td>89</td>\n", | |
" <td>98</td>\n", | |
" <td>101</td>\n", | |
" <td>104</td>\n", | |
" <td>108</td>\n", | |
" <td>112</td>\n", | |
" <td>119</td>\n", | |
" <td>123</td>\n", | |
" <td>126</td>\n", | |
" <td>129</td>\n", | |
" <td>133</td>\n", | |
" <td>137</td>\n", | |
" <td>142</td>\n", | |
" <td>144</td>\n", | |
" <td>147</td>\n", | |
" <td>152</td>\n", | |
" <td>...</td>\n", | |
" <td>621</td>\n", | |
" <td>627</td>\n", | |
" <td>633</td>\n", | |
" <td>637</td>\n", | |
" <td>641</td>\n", | |
" <td>649</td>\n", | |
" <td>650</td>\n", | |
" <td>654</td>\n", | |
" <td>663</td>\n", | |
" <td>668</td>\n", | |
" <td>674</td>\n", | |
" <td>680</td>\n", | |
" <td>686</td>\n", | |
" <td>690</td>\n", | |
" <td>694</td>\n", | |
" <td>698</td>\n", | |
" <td>702</td>\n", | |
" <td>706</td>\n", | |
" <td>714</td>\n", | |
" <td>715</td>\n", | |
" <td>718</td>\n", | |
" <td>722</td>\n", | |
" <td>726</td>\n", | |
" <td>731</td>\n", | |
" <td>735</td>\n", | |
" <td>744</td>\n", | |
" <td>749</td>\n", | |
" <td>755</td>\n", | |
" <td>761</td>\n", | |
" <td>762</td>\n", | |
" <td>766</td>\n", | |
" <td>775</td>\n", | |
" <td>780</td>\n", | |
" <td>786</td>\n", | |
" <td>792</td>\n", | |
" <td>798</td>\n", | |
" <td>802</td>\n", | |
" <td>805</td>\n", | |
" <td>807</td>\n", | |
" <td>807</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>4 rows × 190 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" 0 ... 189\n", | |
"tokens My ... \"\"\n", | |
"token_logprobs NaN ... -1.20859\n", | |
"top_logprobs None ... {'\"\"': -1.2085857}\n", | |
"text_offset 0 ... 807\n", | |
"\n", | |
"[4 rows x 190 columns]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 21 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "yNyZNHetiuBj", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment