Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gromov hw09 #281

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.DS_Store

Binary file not shown.
Empty file added 11. Texts/hw.ipynb
Empty file.
1,024 changes: 876 additions & 148 deletions 11. Texts/inclass_nlp.ipynb

Large diffs are not rendered by default.

162 changes: 112 additions & 50 deletions 11. Texts/word2vec.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "YyV0dhXdk4EO"
},
"outputs": [],
"source": [
"import io\n",
"import numpy as np\n",
Expand All @@ -18,104 +23,161 @@
" tokens = line.rstrip().split(' ')\n",
" data[tokens[0]] = np.array(list(map(float, tokens[1:])))\n",
" return data"
],
"metadata": {
"id": "YyV0dhXdk4EO"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "code",
"source": [
"!wget https://dl.fbaipublicfiles.com/fasttext/vectors-english/crawl-300d-2M.vec.zip -O crawl-300d-2M.vec.zip\n",
"!unzip crawl-300d-2M.vec.zip"
],
"execution_count": 2,
"metadata": {
"id": "0G2o41vBkSRf"
},
"execution_count": null,
"outputs": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--2022-05-25 10:22:14-- https://dl.fbaipublicfiles.com/fasttext/vectors-english/crawl-300d-2M.vec.zip\n",
"Resolving dl.fbaipublicfiles.com (dl.fbaipublicfiles.com)... 188.114.99.144\n",
"Connecting to dl.fbaipublicfiles.com (dl.fbaipublicfiles.com)|188.114.99.144|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 1523785255 (1,4G) [application/zip]\n",
"Saving to: ‘crawl-300d-2M.vec.zip’\n",
"\n",
"crawl-300d-2M.vec.z 100%[===================>] 1,42G 17,0MB/s in 90s \n",
"\n",
"2022-05-25 10:23:45 (16,1 MB/s) - ‘crawl-300d-2M.vec.zip’ saved [1523785255/1523785255]\n",
"\n",
"Archive: crawl-300d-2M.vec.zip\n",
" inflating: crawl-300d-2M.vec \n"
]
}
],
"source": [
"!wget https://dl.fbaipublicfiles.com/fasttext/vectors-english/crawl-300d-2M.vec.zip -O crawl-300d-2M.vec.zip\n",
"!unzip crawl-300d-2M.vec.zip"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {
"id": "N4SSonGyMwkL"
},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 100000/100000 [00:04<00:00, 22501.92it/s]\n"
]
}
],
"source": [
"vecs = load_vectors('crawl-300d-2M.vec', 100000)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "i4XaW6iejmzG"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('Paris', 'France', 'Parisian', 'paris', 'Lyon', 'London', 'PARIS', 'French', 'Lille', 'Marseille', 'Toulouse', 'Bordeaux', 'Marseilles', 'Strasbourg', 'Berlin', 'Le', 'Versailles', 'Nantes', 'Brussels', 'Grenoble')\n",
"('brother', 'sister', 'cousin', 'brothers', 'brother-in-law', 'uncle', 'nephew', 'father', 'son', 'sister-in-law', 'aunt', 'sisters', 'daughter', 'niece', 'dad', 'cousins', 'Brother', 'mother', 'siblings', 'grandfather')\n"
]
}
],
"source": [
"def get_k_nearest_neighbors(vec, k):\n",
" return list(zip(*sorted(list(map(lambda key: (np.linalg.norm(vec - vecs[key]), key), vecs.keys())))))[1][:k]\n",
"\n",
"print(get_k_nearest_neighbors(vecs['Paris'], 20))\n",
"print(get_k_nearest_neighbors(vecs['brother'], 20))"
],
"metadata": {
"id": "i4XaW6iejmzG"
},
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "code",
"source": [
"get_k_nearest_neighbors(vecs['Paris'] - vecs['France'] + vecs['Germany'], 1)"
],
"execution_count": 5,
"metadata": {
"id": "W3HYdGDblVu6"
},
"execution_count": null,
"outputs": []
"outputs": [
{
"data": {
"text/plain": [
"('Berlin',)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_k_nearest_neighbors(vecs['Paris'] - vecs['France'] + vecs['Germany'], 1)"
]
},
{
"cell_type": "code",
"source": [
"get_k_nearest_neighbors(vecs['brother'] - vecs['man'] + vecs['woman'], 1)"
],
"execution_count": 6,
"metadata": {
"id": "zAz9XlnLlYUv"
},
"execution_count": null,
"outputs": []
"outputs": [
{
"data": {
"text/plain": [
"('sister',)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_k_nearest_neighbors(vecs['brother'] - vecs['man'] + vecs['woman'], 1)"
]
},
{
"cell_type": "code",
"source": [
"get_k_nearest_neighbors(vecs['king'] - vecs['man'] + vecs['woman'], 5)"
],
"execution_count": 7,
"metadata": {
"id": "AkgfxFDNlZkF"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
""
"outputs": [
{
"data": {
"text/plain": [
"('king', 'queen', 'King', 'kings', 'Queen')"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"metadata": {
"id": "8uwmZYPVlbmz"
},
"execution_count": null,
"outputs": []
"source": [
"get_k_nearest_neighbors(vecs['king'] - vecs['man'] + vecs['woman'], 5)"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "word2vec.ipynb",
"provenance": [],
"collapsed_sections": []
"provenance": []
},
"interpreter": {
"hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3.9.12 64-bit",
"language": "python",
"name": "python3"
},
Expand All @@ -129,9 +191,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
}
Loading