From 567c5bab12e2f3a022fc2e477574ba671f41f114 Mon Sep 17 00:00:00 2001 From: Leonardo Emili Date: Mon, 30 Mar 2020 16:27:53 +0200 Subject: [PATCH] Allow testing using CPU #17 --- src/wikification_nn.ipynb | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/wikification_nn.ipynb b/src/wikification_nn.ipynb index b7c102a2..ef4ff2c9 100644 --- a/src/wikification_nn.ipynb +++ b/src/wikification_nn.ipynb @@ -69,10 +69,10 @@ "metadata": { "id": "mMAhu2EIlQ40", "colab_type": "code", - "outputId": "12fa9346-1126-4ae4-efa4-51f704700c44", + "outputId": "433215b9-260e-4adf-d8e9-1161c66d8d24", "colab": { "base_uri": "https://localhost:8080/", - "height": 156 + "height": 68 } }, "source": [ @@ -99,18 +99,14 @@ "from google.colab import drive\n", "drive.mount('/content/drive')" ], - "execution_count": 2, + "execution_count": 1, "outputs": [ { "output_type": "stream", "text": [ "[nltk_data] Downloading package punkt to /root/nltk_data...\n", "[nltk_data] Package punkt is already up-to-date!\n", - "Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly\n", - "\n", - "Enter your authorization code:\n", - "··········\n", - "Mounted at /content/drive\n" + "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n" ], "name": "stdout" } @@ -150,6 +146,7 @@ " val_slice = 0.20\n", " inner_sep = '_'\n", " outer_sep = '|'\n", + " # label entities using links that appear at least a certain number of times in the training set\n", " link_cutoff = 50\n", "\n", " def __init__(self, dataset):\n", @@ -708,7 +705,7 @@ "metadata": { "id": "t9ZVZE-J7cDB", "colab_type": "code", - "outputId": "a3e5cff7-ca16-4a17-cbf9-89dd0346d6d5", + "outputId": "457b41d1-147f-427d-929d-93136bacd0af", "colab": { "base_uri": "https://localhost:8080/", "height": 34 @@ -739,13 +736,13 @@ " device=\"cuda\"\n", ")\n", "\n", - "if not torch.cuda.is_available():\n", + "if args.device == \"cpu\" or not torch.cuda.is_available():\n", " print(\"Running model using CPU\")\n", " args.device = 'cpu'\n", "else:\n", " print(\"Running model on GPU:\", torch.cuda.get_device_name(0), \"x\" + str(torch.cuda.device_count()))" ], - "execution_count": 9, + "execution_count": 7, "outputs": [ { "output_type": "stream", @@ -908,7 +905,7 @@ "metadata": { "id": "YHjabSqcDTMF", "colab_type": "code", - "outputId": "825ff553-9a0d-4860-fc65-cfecf860106f", + "outputId": "f9dab219-f3bb-43f7-ad81-12cbbba2215f", "colab": { "base_uri": "https://localhost:8080/", "height": 782 @@ -919,7 +916,7 @@ "if args.save_model:\n", " save_model(model, train_state, vocabulary, args)" ], - "execution_count": 12, + "execution_count": 9, "outputs": [ { "output_type": "display_data", @@ -963,8 +960,8 @@ "colab": {} }, "source": [ - "# Load the model which was previously trained\n", - "state = torch.load(args.save_dir + args.model_state_file)\n", + "# Load the model which was previously trained on a GPU either using CPU or GPU\n", + "state = torch.load(args.save_dir + args.model_state_file, map_location=torch.device(args.device)) if args.device == \"cpu\" else torch.load(args.save_dir + args.model_state_file)\n", "vocabulary = state['vocabulary']\n", "model = BiLSTM(vocab_size=vocabulary.source_size(), embedding_dim=args.embedding_dim, hidden_dim=args.hidden_dim, target_size=vocabulary.target_size(), batch_size=args.inference_batch_size, device=args.device).to(args.device)\n", "model.load_state_dict(state['state_dict'])\n", @@ -997,11 +994,11 @@ "metadata": { "id": "jDMKjba6qVeb", "colab_type": "code", + "outputId": "cdc595e4-99c0-4a6b-d969-07b9d3221f8f", "colab": { "base_uri": "https://localhost:8080/", "height": 57 - }, - "outputId": "7f25aee9-b821-47b8-a967-e56904983a4c" + } }, "source": [ "# Just an example sample sentence randomly picked from the test set\n", @@ -1013,13 +1010,13 @@ "y = annotate(sample_sentence, stopwords, args)\n", "display(HTML(y))" ], - "execution_count": 15, + "execution_count": 39, "outputs": [ { "output_type": "display_data", "data": { "text/html": [ - "

As a result , the british sphere of influence had effectively expanded from the indian subcontinent into tibet , although tibet remained nominally under the sovereignty of the qing dynasty of china .

" + "

A huguenot and officer under admiral gaspard de coligny , ribault led an expedition to the new world in 1562 that founded the outpost of charlesfort on parris island in present-day south carolina .

" ], "text/plain": [ ""