{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "assignment3.ipynb", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "accelerator": "GPU" }, "cells": [ { "cell_type": "code", "metadata": { "id": "Pka5Mc5ROaj7" }, "source": [ "'''\n", "Trains a simple convnet on the MNIST dataset.\n", "Credit: Keras Team\n", "'''\n", "\n", "from __future__ import print_function\n", "import keras\n", "from keras.datasets import mnist\n", "from keras.models import Sequential\n", "from keras.layers import Dense, Dropout, Flatten\n", "from keras.layers import Conv2D, MaxPooling2D" ], "execution_count": 5, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "RBPGZTlvOTRZ" }, "source": [ "# DEFINING *SOME* HYPERPARAMETERS\n", "batch_size = 256\n", "num_classes = 10\n", "epochs = 2 \n", "\n", "# DATA CLEAN-UP\n", "img_rows, img_cols = 28, 28\n", "\n", "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", "\n", "x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)\n", "x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)\n", "input_shape = (img_rows, img_cols, 1)\n", "\n", "x_train = x_train.astype('float32')\n", "x_test = x_test.astype('float32')\n", "x_train /= 255\n", "x_test /= 255\n", "\n", "print('x_train shape:', x_train.shape)\n", "print(x_train.shape[0], 'train samples')\n", "print(x_test.shape[0], 'test samples')\n", "\n", "y_train = keras.utils.to_categorical(y_train, num_classes)\n", "y_test = keras.utils.to_categorical(y_test, num_classes)\n", "\n", "# MODEL DEFINITION (some hardcodede hyperparameters)\n", "model = Sequential()\n", "model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))\n", "model.add(Conv2D(64, (3, 3), activation='relu'))\n", "model.add(MaxPooling2D(pool_size=(2, 2)))\n", "model.add(Dropout(0.9))\n", "model.add(Flatten())\n", "model.add(Dense(128, activation='relu'))\n", "model.add(Dropout(0.9))\n", "model.add(Dense(num_classes, activation='softmax'))\n", "\n", "# MODEL COMPILATION AND TRAINING\n", "model.compile(loss=keras.losses.categorical_crossentropy,\n", " optimizer=keras.optimizers.SGD(lr=0.001),\n", " metrics=['accuracy'])\n", "\n", "model.fit(x_train, y_train,\n", " batch_size=batch_size,\n", " epochs=epochs,\n", " verbose=1,\n", " validation_data=(x_test, y_test))\n", "\n", "# MODEL EVALUATION\n", "score = model.evaluate(x_test, y_test, verbose=0)\n", "\n", "print('Test loss:', score[0])\n", "print('Test accuracy:', score[1])\n" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "eokZIP_eOe5C" }, "source": [ "# if you are running this notebook on your local machine, install with pip\n", "# !pip install pymagnitude\n", "\n", "# if you are using Colab, install magnitude using the following:\n", "! echo \"Installing Magnitude.... (please wait, can take a while)\"\n", "! (curl https://raw.githubusercontent.com/plasticityai/magnitude/master/install-colab.sh | /bin/bash 1>/dev/null 2>/dev/null)\n", "! echo \"Done installing Magnitude.\"" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "UQwjlYsIU9bo" }, "source": [ "!wget http://magnitude.plasticity.ai/word2vec/light/GoogleNews-vectors-negative300.magnitude" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "jdAovzVbOrtP" }, "source": [ "from pymagnitude import *\n", "\n", "file_path = \"GoogleNews-vectors-negative300.magnitude\"\n", "vectors = Magnitude(file_path)\n", "print(vectors.distance(\"cat\", \"dog\"))" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "1ecai0hDOt78" }, "source": [ "1. What is the dimensionality of these word embeddings? Provide an integer answer.\n", "2. What are the top-5 most similar words to picnic (not including picnic itself)?\n", "3. According to the word embeddings, which of these words is not like the others? `[tissue', 'papyrus', 'manila', 'newsprint', 'parchment', 'gazette’]`\n", "4. Solve the following analogy: “leg is to jump as `X` is to throw”.\n", "5. Is the word `alumni` in the vocabulary? What about `alumnus`?\n", "6. How many words are in the vocabulary?" ] } ] }