|
276 | 276 | " model.to(device)\n", |
277 | 277 | " gd = optim.Adamax(model.parameters(), lr=0.002)\n", |
278 | 278 | " dataloader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True)\n", |
279 | | - " losses = []\n", |
| 279 | + " \n", |
280 | 280 | " for epoch in range(num_epoch):\n", |
| 281 | + " losses = []\n", |
| 282 | + " \n", |
281 | 283 | " for i, (batch) in enumerate(dataloader):\n", |
282 | 284 | " gd.zero_grad()\n", |
283 | 285 | " loss = model.batch_loss(batch.cuda())\n", |
|
613 | 615 | }, |
614 | 616 | "outputs": [], |
615 | 617 | "source": [ |
616 | | - "\n", |
617 | 618 | "theoretical_std = (X[:100].std()**2 + 0.5**2)**.5\n", |
618 | 619 | "our_std = apply_gaussian_noise(X[:100],sigma=0.5).std()\n", |
619 | 620 | "assert abs(theoretical_std - our_std) < 0.01, \"Standard deviation does not match it's required value. Make sure you use sigma as std.\"\n", |
|
655 | 656 | "outputs": [], |
656 | 657 | "source": [ |
657 | 658 | "def train_noise(model, dataset, num_epoch=50):\n", |
658 | | - " #<Your code: define train function for denoising autoencoder as train function above>" |
659 | | - ] |
660 | | - }, |
661 | | - { |
662 | | - "cell_type": "code", |
663 | | - "execution_count": null, |
664 | | - "metadata": { |
665 | | - "colab": {}, |
666 | | - "colab_type": "code", |
667 | | - "id": "MKQK4HJL_eX2" |
668 | | - }, |
669 | | - "outputs": [], |
670 | | - "source": [ |
671 | | - "X_train_noise = apply_gaussian_noise(X_train)\n", |
672 | | - "X_test_noise = apply_gaussian_noise(X_test)" |
| 659 | + " #<Your code: define train function for denoising autoencoder as train function above>\n", |
| 660 | + " #<Think carefully, what should be ground-truth image for computing loss function>" |
673 | 661 | ] |
674 | 662 | }, |
675 | 663 | { |
676 | | - "cell_type": "code", |
677 | | - "execution_count": null, |
678 | | - "metadata": { |
679 | | - "colab": {}, |
680 | | - "colab_type": "code", |
681 | | - "id": "8ClYTlpa_eX4" |
682 | | - }, |
683 | | - "outputs": [], |
| 664 | + "cell_type": "markdown", |
| 665 | + "metadata": {}, |
684 | 666 | "source": [ |
685 | | - "X_train_tensor_n = torch.from_numpy(X_train_noise).type(torch.DoubleTensor)\n", |
686 | | - "X_test_tensor_n = torch.Tensor(X_test_noise).type(torch.DoubleTensor)" |
| 667 | + "__Note:__ You may change the way the training with noise is done, if you want. For example, you may change Dataloader or batch_loss function in model and leave train function unchanged." |
687 | 668 | ] |
688 | 669 | }, |
689 | 670 | { |
|
701 | 682 | "outputs": [], |
702 | 683 | "source": [ |
703 | 684 | "aenc = pca_autoencoder()\n", |
704 | | - "train(aenc, X_train_tensor_n, 50)" |
| 685 | + "train_noise(aenc, X_train_tensor, 50)" |
705 | 686 | ] |
706 | 687 | }, |
707 | 688 | { |
|
723 | 704 | "id": "HVj0NPXV3liL" |
724 | 705 | }, |
725 | 706 | "source": [ |
726 | | - "** Let's evaluate!!! **" |
| 707 | + "**Let's evaluate!!!**" |
727 | 708 | ] |
728 | 709 | }, |
729 | 710 | { |
|
842 | 823 | "def get_similar(image, n_neighbors=5):\n", |
843 | 824 | " assert len(image.shape)==3,\"image must be [batch,height,width,3]\"\n", |
844 | 825 | "\n", |
845 | | - " code = aenc.enc(image.cuda(device)).detach().cpu().numpy()\n", |
| 826 | + " code = #<Your code: encode image into latent code>\n", |
846 | 827 | " \n", |
847 | 828 | " (distances,),(idx,) = #<Your code: using lshf.kneighbors find nearest neighbors>\n", |
848 | 829 | " \n", |
|
1023 | 1004 | "name": "python", |
1024 | 1005 | "nbconvert_exporter": "python", |
1025 | 1006 | "pygments_lexer": "ipython3", |
1026 | | - "version": "3.6.2" |
| 1007 | + "version": "3.6.9" |
1027 | 1008 | } |
1028 | 1009 | }, |
1029 | 1010 | "nbformat": 4, |
|
0 commit comments