{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pickle\n", "from gensim.models import Word2Vec" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Configuration\n", "\n", "*models_filename:* The complete path to the pickeld word2vec models.\n", "\n", "*word_pairs_filename:* The complete path to the list of word pairs used for evaluation. This needs to be a **.csv** file.\n", "\n", "*selected_model_filename*: The filename for the best performing model which will be used for the subsequent classification. You may use the **.p** extension indicating a pickled file, but you are free to use whatever you like." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "models_filename = \"models.p\"\n", "word_pairs_filename = \"ready_to_use/word_pairs/French.csv\"\n", "selected_model_filename = \"best_model.p\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Model Evaluation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load models" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"loading models\")\n", "with open(models_filename, \"rb\") as handle:\n", " models = pickle.load(handle)\n", "print(\"loaded {} models\".format(len(models)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Evaluate models" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "best_sim = 0\n", "best_pars = None\n", "for p, m in models.items():\n", " similarities = m.wv.evaluate_word_pairs(word_pairs_filename, delimiter=\",\")\n", " if similarities[0][0] > best_sim:\n", " best_sim = similarities[0][0]\n", " best_pars = p\n", "print(\"found best model with parameters: {}\".format(best_pars))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Save best performing model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with open(selected_model_filename, \"wb\") as handle:\n", " pickle.dump(models[best_pars], handle)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }