diff --git a/jupyter/truncated_svd.ipynb b/jupyter/truncated_svd.ipynb index f1ee0d26944751ae05c8ff25830480642b810267..7331d3c54b228f57364c1d27a3c17fbf9b94ecc3 100644 --- a/jupyter/truncated_svd.ipynb +++ b/jupyter/truncated_svd.ipynb @@ -147,6 +147,7 @@ "\n", "\n", "# See Faster-RCNN: https://github.com/rbgirshick/py-faster-rcnn/blob/master/tools/compress_net.py\n", + "# Replaced numpy operations with pytorch operations (so that we can leverage the GPU).\n", "def truncated_svd(W, l):\n", " \"\"\"Compress the weight matrix W of an inner product (fully connected) layer\n", " using truncated SVD.\n", @@ -157,15 +158,14 @@ " Ul, L: matrices such that W \\approx Ul*L\n", " \"\"\"\n", "\n", - " U, s, V = np.linalg.svd(W, full_matrices=False)\n", + " U, s, V = torch.svd(W, some=True)\n", "\n", " Ul = U[:, :l]\n", " sl = s[:l]\n", + " V = V.t()\n", " Vl = V[:l, :]\n", "\n", - " SV = np.dot(np.diag(sl), Vl)\n", - " SV = torch.from_numpy(SV).cuda()\n", - " Ul = torch.from_numpy(Ul).cuda()\n", + " SV = torch.mm(torch.diag(sl), Vl)\n", " return Ul, SV\n", "\n", "\n", @@ -174,7 +174,7 @@ " super(TruncatedSVD,self).__init__()\n", " self.replaced_gemm = replaced_gemm\n", " print(\"W = {}\".format(gemm_weights.shape))\n", - " self.U, self.SV = truncated_svd(gemm_weights.cpu(), int(0.4 * gemm_weights.size(0)))\n", + " self.U, self.SV = truncated_svd(gemm_weights.data, int(0.4 * gemm_weights.size(0)))\n", " print(\"U = {}\".format(self.U.shape))\n", " \n", " self.fc_u = nn.Linear(self.U.size(1), self.U.size(0)).cuda()\n", @@ -214,7 +214,7 @@ "import time\n", "import torchnet.meter as tnt\n", "t0 = time.time()\n", - "test_loader = imagenet_load_data(\"../../datasets/imagenet\", \n", + "test_loader = imagenet_load_data(\"../../data.imagenet\", \n", " batch_size=64, \n", " num_workers=4,\n", " shuffle=False)\n", @@ -228,20 +228,11 @@ " outputs = resnet50(inputs)\n", " classerr.add(outputs.data, target)\n", " if (validation_step+1) % 100 == 0:\n", - " print((validation_step+1) * 512)\n", + " print(\"progress: %d images\" % ((validation_step+1) * 512))\n", " \n", - "print(classerr.value(1), classerr.value(5))\n", + "print(\"Top1: %.2f Top5: %.2f\" % (classerr.value(1), classerr.value(5)))\n", "t2 = time.time()\n", - "print(t2-t0)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "" + "print(\"Duration: \", t2-t0)" ] } ], @@ -254,16 +245,16 @@ "language_info": { "codemirror_mode": { "name": "ipython", - "version": 3.0 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.2" + "version": "3.6.7" } }, "nbformat": 4, - "nbformat_minor": 0 -} \ No newline at end of file + "nbformat_minor": 1 +}