Skip to content

Commit

Permalink
Replace explicit timers with magic %time in Soft Cosine Measure notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
Witiko committed Jan 28, 2018
1 parent 08dea4e commit 8af5f67
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions docs/notebooks/soft_cosine_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
"metadata": {},
"outputs": [],
"source": [
"from time import time\n",
"\n",
"# Initialize logging.\n",
"import logging\n",
"logging.basicConfig(format='%(asctime)s | %(levelname)s : %(message)s')"
Expand Down Expand Up @@ -140,7 +138,7 @@
}
],
"source": [
"start = time()\n",
"%%time\n",
"import os\n",
"\n",
"from gensim.models import KeyedVectors\n",
Expand All @@ -149,9 +147,7 @@
" \n",
"model = KeyedVectors.load_word2vec_format('/data/GoogleNews-vectors-negative300.bin.gz', binary=True)\n",
"similarity_matrix = model.similarity_matrix(dictionary)\n",
"del model\n",
"\n",
"print('Cell took %.2f seconds to run.' % (time() - start))"
"del model"
]
},
{
Expand Down Expand Up @@ -278,8 +274,7 @@
}
],
"source": [
"start = time()\n",
"\n",
"%%time\n",
"import json\n",
"\n",
"# Business IDs of the restaurants.\n",
Expand Down Expand Up @@ -312,8 +307,6 @@
" # Add to corpus for similarity queries.\n",
" scm_corpus.append(text)\n",
" documents.append(json_line['text'])"
"\n",
"print('Cell took %.2f seconds to run.' %(time() - start))"
]
},
{
Expand All @@ -340,6 +333,7 @@
}
],
"source": [
"%%time\n",
"from matplotlib import cycler, pyplot as plt\n",
"%matplotlib inline\n",
"\n",
Expand Down Expand Up @@ -387,8 +381,7 @@
}
],
"source": [
"start = time()\n",
"\n",
"%%time\n",
"from gensim.models import Word2Vec\n",
"\n",
"# Train Word2Vec on all the restaurants.\n",
Expand All @@ -401,9 +394,7 @@
"dictionary = corpora.Dictionary(scm_corpus)\n",
"scm_corpus = [dictionary.doc2bow(document) for document in scm_corpus]\n",
"similarity_matrix = model.wv.similarity_matrix(dictionary)\n",
"instance = SoftCosineSimilarity(scm_corpus, similarity_matrix, num_best=num_best)\n",
"\n",
"print('Cell took %.2f seconds to run.' %(time() - start))"
"instance = SoftCosineSimilarity(scm_corpus, similarity_matrix, num_best=num_best)"
]
},
{
Expand Down Expand Up @@ -431,14 +422,11 @@
}
],
"source": [
"start = time()\n",
"\n",
"%%time\n",
"sent = 'Yummy! Great view of the Bellagio Fountain show.'\n",
"query = dictionary.doc2bow(preprocess(sent))\n",
"\n",
"sims = instance[query] # A query is simply a \"look-up\" in the similarity class.\n",
"\n",
"print('Cell took %.2f seconds to run.' %(time() - start))"
"sims = instance[query] # A query is simply a \"look-up\" in the similarity class."
]
},
{
Expand Down Expand Up @@ -559,8 +547,7 @@
}
],
"source": [
"start = time()\n",
"\n",
"%%time\n",
"sent = 'Great view of the Bellagio Fountain show.'\n",
"query = dictionary.doc2bow(preprocess(sent))\n",
"\n",
Expand All @@ -571,9 +558,7 @@
"for i in range(num_best):\n",
" print()\n",
" print('sim = %.4f' % sims[i][1])\n",
" print(documents[sims[i][0]])\n",
"\n",
"print('Cell took %.2f seconds to run.' %(time() - start))"
" print(documents[sims[i][0]])"
]
},
{
Expand Down

0 comments on commit 8af5f67

Please sign in to comment.