Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • main
1 result

Target

Select target project
No results found
Select Git revision
  • main
1 result
Show changes

Commits on Source 6

1000 files
+ 2294
174
Compare changes
  • Side-by-side
  • Inline

Files

Test.ipynb

0 → 100644
+176 −0
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

## Test Pytorch

%% Cell type:code id: tags:

``` python
import torch

N, D = 14, 10
x = torch.randn(N, D).type(torch.FloatTensor)
print(x)

from torchvision import models

alexnet = models.alexnet()
print(alexnet)
```

%% Output

    tensor([[ 0.6650,  2.0888, -0.3361, -0.6212,  0.6276, -0.3091,  1.6601, -0.1404,
              0.0224, -1.1360],
            [-0.8311, -1.6385,  0.3011,  1.2989,  0.2328, -0.0061,  0.4039,  0.3956,
             -0.2067, -0.8877],
            [-0.2899, -1.7310, -0.4318,  1.3837,  3.1771,  1.7354,  0.3680, -0.0743,
             -0.1610,  0.8281],
            [-0.2591, -2.0913,  0.8630, -0.3533,  0.9323, -1.4520,  0.4476, -0.2588,
             -0.0963,  1.8449],
            [ 0.4599,  0.3258,  0.7780,  0.6943, -0.4343, -0.0536, -0.1049,  0.2867,
              0.5493, -1.2934],
            [-0.6990,  0.0783, -0.8745,  1.2521,  1.5363,  0.8770, -0.5319, -0.2629,
              0.7732, -0.5001],
            [-0.2902, -0.8901,  0.1904,  0.7456,  0.5802,  0.0443, -1.2447,  2.1954,
              0.5382,  0.2219],
            [ 1.0372, -0.7516,  0.7940,  0.8207,  0.6601,  0.0317,  0.1410, -1.7062,
             -0.6549,  0.6287],
            [ 0.6680,  1.0136, -0.0813, -1.4382,  0.4640,  1.2923, -1.0299,  0.5684,
              1.6626, -1.1921],
            [-1.1864, -0.6625, -1.0846,  0.7550, -0.8748,  0.2835, -1.4264,  1.0279,
             -0.6046,  0.6298],
            [-1.9253, -0.4960, -0.8379,  1.8726, -2.0282,  0.0869,  1.2508,  0.0390,
             -1.7213,  0.3269],
            [ 1.7196, -1.6188, -0.4604, -1.0196, -0.8883,  1.2987,  0.4795,  0.5581,
             -1.0138, -0.2184],
            [-0.6600,  0.5816, -0.6574,  0.4684,  1.2546, -0.4140, -0.2636,  0.4267,
              0.1736, -1.5019],
            [-0.8852,  0.6677, -1.3074, -1.2241, -1.4054,  0.0919,  1.5832,  1.4357,
             -1.9016,  0.7274]])
    AlexNet(
      (features): Sequential(
        (0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))
        (1): ReLU(inplace=True)
        (2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
        (3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
        (4): ReLU(inplace=True)
        (5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
        (6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (7): ReLU(inplace=True)
        (8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (9): ReLU(inplace=True)
        (10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        (11): ReLU(inplace=True)
        (12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
      )
      (avgpool): AdaptiveAvgPool2d(output_size=(6, 6))
      (classifier): Sequential(
        (0): Dropout(p=0.5, inplace=False)
        (1): Linear(in_features=9216, out_features=4096, bias=True)
        (2): ReLU(inplace=True)
        (3): Dropout(p=0.5, inplace=False)
        (4): Linear(in_features=4096, out_features=4096, bias=True)
        (5): ReLU(inplace=True)
        (6): Linear(in_features=4096, out_features=1000, bias=True)
      )
    )

%% Cell type:markdown id: tags:

## Exercise 1: CNN on CIFAR10

%% Cell type:markdown id: tags:

See if cuda is available ==> it is not

%% Cell type:code id: tags:

``` python
import torch

# check if CUDA is available
train_on_gpu = torch.cuda.is_available()

if not train_on_gpu:
    print("CUDA is not available.  Training on CPU ...")
else:
    print("CUDA is available!  Training on GPU ...")
```

%% Output

    CUDA is not available.  Training on CPU ...

%% Cell type:markdown id: tags:

Load the CIFAR10 dataset

%% Cell type:code id: tags:

``` python
import numpy as np
from torchvision import datasets, transforms
from torch.utils.data.sampler import SubsetRandomSampler

# number of subprocesses to use for data loading
num_workers = 0
# how many samples per batch to load
batch_size = 20
# percentage of training set to use as validation
valid_size = 0.2

# convert data to a normalized torch.FloatTensor
transform = transforms.Compose(
    [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
)

# choose the training and test datasets
train_data = datasets.CIFAR10("data", train=True, download=True, transform=transform)
test_data = datasets.CIFAR10("data", train=False, download=True, transform=transform)

# obtain training indices that will be used for validation
num_train = len(train_data)
indices = list(range(num_train))
np.random.shuffle(indices)
split = int(np.floor(valid_size * num_train))
train_idx, valid_idx = indices[split:], indices[:split]

# define samplers for obtaining training and validation batches
train_sampler = SubsetRandomSampler(train_idx)
valid_sampler = SubsetRandomSampler(valid_idx)

# prepare data loaders (combine dataset and sampler)
train_loader = torch.utils.data.DataLoader(
    train_data, batch_size=batch_size, sampler=train_sampler, num_workers=num_workers
)
valid_loader = torch.utils.data.DataLoader(
    train_data, batch_size=batch_size, sampler=valid_sampler, num_workers=num_workers
)
test_loader = torch.utils.data.DataLoader(
    test_data, batch_size=batch_size, num_workers=num_workers
)

# specify the image classes
classes = [
    "airplane",
    "automobile",
    "bird",
    "cat",
    "deer",
    "dog",
    "frog",
    "horse",
    "ship",
    "truck",
]
```

%% Output

    Files already downloaded and verified
    Files already downloaded and verified

%% Cell type:code id: tags:

``` python
```
+4 −0
Original line number Diff line number Diff line
[ZoneTransfer]
ZoneId=3
ReferrerUrl=https://www.google.com/
HostUrl=https://upload.wikimedia.org/wikipedia/commons/4/4d/Apis_mellifera_Western_honey_bee.jpg